LLVM  15.0.0git
Core.cpp
Go to the documentation of this file.
1 //===-- Core.cpp ----------------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file implements the common infrastructure (including the C bindings)
10 // for libLLVMCore.a, which implements the LLVM intermediate representation.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "llvm-c/Core.h"
15 #include "llvm/IR/Attributes.h"
16 #include "llvm/IR/BasicBlock.h"
17 #include "llvm/IR/Constants.h"
19 #include "llvm/IR/DerivedTypes.h"
20 #include "llvm/IR/DiagnosticInfo.h"
22 #include "llvm/IR/GlobalAlias.h"
23 #include "llvm/IR/GlobalVariable.h"
24 #include "llvm/IR/IRBuilder.h"
25 #include "llvm/IR/InlineAsm.h"
26 #include "llvm/IR/IntrinsicInst.h"
27 #include "llvm/IR/LLVMContext.h"
29 #include "llvm/IR/Module.h"
30 #include "llvm/InitializePasses.h"
31 #include "llvm/PassRegistry.h"
32 #include "llvm/Support/Debug.h"
37 #include "llvm/Support/Threading.h"
39 #include <cassert>
40 #include <cstdlib>
41 #include <cstring>
42 #include <system_error>
43 
44 using namespace llvm;
45 
46 #define DEBUG_TYPE "ir"
47 
54 }
55 
58 }
59 
60 void LLVMShutdown() {
61  llvm_shutdown();
62 }
63 
64 /*===-- Error handling ----------------------------------------------------===*/
65 
66 char *LLVMCreateMessage(const char *Message) {
67  return strdup(Message);
68 }
69 
70 void LLVMDisposeMessage(char *Message) {
71  free(Message);
72 }
73 
74 
75 /*===-- Operations on contexts --------------------------------------------===*/
76 
78 
80  return wrap(new LLVMContext());
81 }
82 
84 
86  LLVMDiagnosticHandler Handler,
87  void *DiagnosticContext) {
88  unwrap(C)->setDiagnosticHandlerCallBack(
90  Handler),
91  DiagnosticContext);
92 }
93 
95  return LLVM_EXTENSION reinterpret_cast<LLVMDiagnosticHandler>(
96  unwrap(C)->getDiagnosticHandlerCallBack());
97 }
98 
100  return unwrap(C)->getDiagnosticContext();
101 }
102 
104  void *OpaqueHandle) {
105  auto YieldCallback =
106  LLVM_EXTENSION reinterpret_cast<LLVMContext::YieldCallbackTy>(Callback);
107  unwrap(C)->setYieldCallback(YieldCallback, OpaqueHandle);
108 }
109 
111  return unwrap(C)->shouldDiscardValueNames();
112 }
113 
115  unwrap(C)->setDiscardValueNames(Discard);
116 }
117 
119  unwrap(C)->setOpaquePointers(OpaquePointers);
120 }
121 
123  delete unwrap(C);
124 }
125 
127  unsigned SLen) {
128  return unwrap(C)->getMDKindID(StringRef(Name, SLen));
129 }
130 
131 unsigned LLVMGetMDKindID(const char *Name, unsigned SLen) {
133 }
134 
135 unsigned LLVMGetEnumAttributeKindForName(const char *Name, size_t SLen) {
137 }
138 
140  return Attribute::AttrKind::EndAttrKinds;
141 }
142 
144  uint64_t Val) {
145  auto &Ctx = *unwrap(C);
146  auto AttrKind = (Attribute::AttrKind)KindID;
147 
148  if (AttrKind == Attribute::AttrKind::ByVal) {
149  // After r362128, byval attributes need to have a type attribute. Provide a
150  // NULL one until a proper API is added for this.
151  return wrap(Attribute::getWithByValType(Ctx, nullptr));
152  }
153 
154  if (AttrKind == Attribute::AttrKind::StructRet) {
155  // Same as byval.
156  return wrap(Attribute::getWithStructRetType(Ctx, nullptr));
157  }
158 
159  return wrap(Attribute::get(Ctx, AttrKind, Val));
160 }
161 
163  return unwrap(A).getKindAsEnum();
164 }
165 
167  auto Attr = unwrap(A);
168  if (Attr.isEnumAttribute())
169  return 0;
170  return Attr.getValueAsInt();
171 }
172 
174  LLVMTypeRef type_ref) {
175  auto &Ctx = *unwrap(C);
176  auto AttrKind = (Attribute::AttrKind)KindID;
177  return wrap(Attribute::get(Ctx, AttrKind, unwrap(type_ref)));
178 }
179 
181  auto Attr = unwrap(A);
182  return wrap(Attr.getValueAsType());
183 }
184 
186  const char *K, unsigned KLength,
187  const char *V, unsigned VLength) {
188  return wrap(Attribute::get(*unwrap(C), StringRef(K, KLength),
189  StringRef(V, VLength)));
190 }
191 
193  unsigned *Length) {
194  auto S = unwrap(A).getKindAsString();
195  *Length = S.size();
196  return S.data();
197 }
198 
200  unsigned *Length) {
201  auto S = unwrap(A).getValueAsString();
202  *Length = S.size();
203  return S.data();
204 }
205 
207  auto Attr = unwrap(A);
208  return Attr.isEnumAttribute() || Attr.isIntAttribute();
209 }
210 
212  return unwrap(A).isStringAttribute();
213 }
214 
216  return unwrap(A).isTypeAttribute();
217 }
218 
220  std::string MsgStorage;
221  raw_string_ostream Stream(MsgStorage);
223 
224  unwrap(DI)->print(DP);
225  Stream.flush();
226 
227  return LLVMCreateMessage(MsgStorage.c_str());
228 }
229 
231  LLVMDiagnosticSeverity severity;
232 
233  switch(unwrap(DI)->getSeverity()) {
234  default:
235  severity = LLVMDSError;
236  break;
237  case DS_Warning:
238  severity = LLVMDSWarning;
239  break;
240  case DS_Remark:
241  severity = LLVMDSRemark;
242  break;
243  case DS_Note:
244  severity = LLVMDSNote;
245  break;
246  }
247 
248  return severity;
249 }
250 
251 /*===-- Operations on modules ---------------------------------------------===*/
252 
254  return wrap(new Module(ModuleID, *GlobalContext));
255 }
256 
258  LLVMContextRef C) {
259  return wrap(new Module(ModuleID, *unwrap(C)));
260 }
261 
263  delete unwrap(M);
264 }
265 
266 const char *LLVMGetModuleIdentifier(LLVMModuleRef M, size_t *Len) {
267  auto &Str = unwrap(M)->getModuleIdentifier();
268  *Len = Str.length();
269  return Str.c_str();
270 }
271 
272 void LLVMSetModuleIdentifier(LLVMModuleRef M, const char *Ident, size_t Len) {
273  unwrap(M)->setModuleIdentifier(StringRef(Ident, Len));
274 }
275 
276 const char *LLVMGetSourceFileName(LLVMModuleRef M, size_t *Len) {
277  auto &Str = unwrap(M)->getSourceFileName();
278  *Len = Str.length();
279  return Str.c_str();
280 }
281 
282 void LLVMSetSourceFileName(LLVMModuleRef M, const char *Name, size_t Len) {
283  unwrap(M)->setSourceFileName(StringRef(Name, Len));
284 }
285 
286 /*--.. Data layout .........................................................--*/
288  return unwrap(M)->getDataLayoutStr().c_str();
289 }
290 
292  return LLVMGetDataLayoutStr(M);
293 }
294 
295 void LLVMSetDataLayout(LLVMModuleRef M, const char *DataLayoutStr) {
296  unwrap(M)->setDataLayout(DataLayoutStr);
297 }
298 
299 /*--.. Target triple .......................................................--*/
300 const char * LLVMGetTarget(LLVMModuleRef M) {
301  return unwrap(M)->getTargetTriple().c_str();
302 }
303 
304 void LLVMSetTarget(LLVMModuleRef M, const char *Triple) {
305  unwrap(M)->setTargetTriple(Triple);
306 }
307 
308 /*--.. Module flags ........................................................--*/
311  const char *Key;
312  size_t KeyLen;
314 };
315 
318  switch (Behavior) {
322  return Module::ModFlagBehavior::Warning;
324  return Module::ModFlagBehavior::Require;
326  return Module::ModFlagBehavior::Override;
328  return Module::ModFlagBehavior::Append;
330  return Module::ModFlagBehavior::AppendUnique;
331  }
332  llvm_unreachable("Unknown LLVMModuleFlagBehavior");
333 }
334 
337  switch (Behavior) {
340  case Module::ModFlagBehavior::Warning:
342  case Module::ModFlagBehavior::Require:
344  case Module::ModFlagBehavior::Override:
346  case Module::ModFlagBehavior::Append:
348  case Module::ModFlagBehavior::AppendUnique:
350  default:
351  llvm_unreachable("Unhandled Flag Behavior");
352  }
353 }
354 
357  unwrap(M)->getModuleFlagsMetadata(MFEs);
358 
359  LLVMOpaqueModuleFlagEntry *Result = static_cast<LLVMOpaqueModuleFlagEntry *>(
360  safe_malloc(MFEs.size() * sizeof(LLVMOpaqueModuleFlagEntry)));
361  for (unsigned i = 0; i < MFEs.size(); ++i) {
362  const auto &ModuleFlag = MFEs[i];
363  Result[i].Behavior = map_from_llvmModFlagBehavior(ModuleFlag.Behavior);
364  Result[i].Key = ModuleFlag.Key->getString().data();
365  Result[i].KeyLen = ModuleFlag.Key->getString().size();
366  Result[i].Metadata = wrap(ModuleFlag.Val);
367  }
368  *Len = MFEs.size();
369  return Result;
370 }
371 
373  free(Entries);
374 }
375 
378  unsigned Index) {
380  static_cast<LLVMOpaqueModuleFlagEntry>(Entries[Index]);
381  return MFE.Behavior;
382 }
383 
385  unsigned Index, size_t *Len) {
387  static_cast<LLVMOpaqueModuleFlagEntry>(Entries[Index]);
388  *Len = MFE.KeyLen;
389  return MFE.Key;
390 }
391 
393  unsigned Index) {
395  static_cast<LLVMOpaqueModuleFlagEntry>(Entries[Index]);
396  return MFE.Metadata;
397 }
398 
400  const char *Key, size_t KeyLen) {
401  return wrap(unwrap(M)->getModuleFlag({Key, KeyLen}));
402 }
403 
405  const char *Key, size_t KeyLen,
406  LLVMMetadataRef Val) {
407  unwrap(M)->addModuleFlag(map_to_llvmModFlagBehavior(Behavior),
408  {Key, KeyLen}, unwrap(Val));
409 }
410 
411 /*--.. Printing modules ....................................................--*/
412 
414  unwrap(M)->print(errs(), nullptr,
415  /*ShouldPreserveUseListOrder=*/false, /*IsForDebug=*/true);
416 }
417 
419  char **ErrorMessage) {
420  std::error_code EC;
421  raw_fd_ostream dest(Filename, EC, sys::fs::OF_TextWithCRLF);
422  if (EC) {
423  *ErrorMessage = strdup(EC.message().c_str());
424  return true;
425  }
426 
427  unwrap(M)->print(dest, nullptr);
428 
429  dest.close();
430 
431  if (dest.has_error()) {
432  std::string E = "Error printing to file: " + dest.error().message();
433  *ErrorMessage = strdup(E.c_str());
434  return true;
435  }
436 
437  return false;
438 }
439 
441  std::string buf;
442  raw_string_ostream os(buf);
443 
444  unwrap(M)->print(os, nullptr);
445  os.flush();
446 
447  return strdup(buf.c_str());
448 }
449 
450 /*--.. Operations on inline assembler ......................................--*/
451 void LLVMSetModuleInlineAsm2(LLVMModuleRef M, const char *Asm, size_t Len) {
452  unwrap(M)->setModuleInlineAsm(StringRef(Asm, Len));
453 }
454 
456  unwrap(M)->setModuleInlineAsm(StringRef(Asm));
457 }
458 
459 void LLVMAppendModuleInlineAsm(LLVMModuleRef M, const char *Asm, size_t Len) {
460  unwrap(M)->appendModuleInlineAsm(StringRef(Asm, Len));
461 }
462 
463 const char *LLVMGetModuleInlineAsm(LLVMModuleRef M, size_t *Len) {
464  auto &Str = unwrap(M)->getModuleInlineAsm();
465  *Len = Str.length();
466  return Str.c_str();
467 }
468 
470  size_t AsmStringSize, char *Constraints,
471  size_t ConstraintsSize, LLVMBool HasSideEffects,
472  LLVMBool IsAlignStack,
473  LLVMInlineAsmDialect Dialect, LLVMBool CanThrow) {
475  switch (Dialect) {
477  AD = InlineAsm::AD_ATT;
478  break;
480  AD = InlineAsm::AD_Intel;
481  break;
482  }
483  return wrap(InlineAsm::get(unwrap<FunctionType>(Ty),
484  StringRef(AsmString, AsmStringSize),
485  StringRef(Constraints, ConstraintsSize),
486  HasSideEffects, IsAlignStack, AD, CanThrow));
487 }
488 
489 /*--.. Operations on module contexts ......................................--*/
491  return wrap(&unwrap(M)->getContext());
492 }
493 
494 
495 /*===-- Operations on types -----------------------------------------------===*/
496 
497 /*--.. Operations on all types (mostly) ....................................--*/
498 
500  switch (unwrap(Ty)->getTypeID()) {
501  case Type::VoidTyID:
502  return LLVMVoidTypeKind;
503  case Type::HalfTyID:
504  return LLVMHalfTypeKind;
505  case Type::BFloatTyID:
506  return LLVMBFloatTypeKind;
507  case Type::FloatTyID:
508  return LLVMFloatTypeKind;
509  case Type::DoubleTyID:
510  return LLVMDoubleTypeKind;
511  case Type::X86_FP80TyID:
512  return LLVMX86_FP80TypeKind;
513  case Type::FP128TyID:
514  return LLVMFP128TypeKind;
515  case Type::PPC_FP128TyID:
516  return LLVMPPC_FP128TypeKind;
517  case Type::LabelTyID:
518  return LLVMLabelTypeKind;
519  case Type::MetadataTyID:
520  return LLVMMetadataTypeKind;
521  case Type::IntegerTyID:
522  return LLVMIntegerTypeKind;
523  case Type::FunctionTyID:
524  return LLVMFunctionTypeKind;
525  case Type::StructTyID:
526  return LLVMStructTypeKind;
527  case Type::ArrayTyID:
528  return LLVMArrayTypeKind;
529  case Type::PointerTyID:
530  return LLVMPointerTypeKind;
532  return LLVMVectorTypeKind;
533  case Type::X86_MMXTyID:
534  return LLVMX86_MMXTypeKind;
535  case Type::X86_AMXTyID:
536  return LLVMX86_AMXTypeKind;
537  case Type::TokenTyID:
538  return LLVMTokenTypeKind;
542  llvm_unreachable("DXIL pointers are unsupported via the C API");
543  }
544  llvm_unreachable("Unhandled TypeID.");
545 }
546 
548 {
549  return unwrap(Ty)->isSized();
550 }
551 
553  return wrap(&unwrap(Ty)->getContext());
554 }
555 
557  return unwrap(Ty)->print(errs(), /*IsForDebug=*/true);
558 }
559 
561  std::string buf;
562  raw_string_ostream os(buf);
563 
564  if (unwrap(Ty))
565  unwrap(Ty)->print(os);
566  else
567  os << "Printing <null> Type";
568 
569  os.flush();
570 
571  return strdup(buf.c_str());
572 }
573 
574 /*--.. Operations on integer types .........................................--*/
575 
577  return (LLVMTypeRef) Type::getInt1Ty(*unwrap(C));
578 }
580  return (LLVMTypeRef) Type::getInt8Ty(*unwrap(C));
581 }
583  return (LLVMTypeRef) Type::getInt16Ty(*unwrap(C));
584 }
586  return (LLVMTypeRef) Type::getInt32Ty(*unwrap(C));
587 }
589  return (LLVMTypeRef) Type::getInt64Ty(*unwrap(C));
590 }
593 }
595  return wrap(IntegerType::get(*unwrap(C), NumBits));
596 }
597 
600 }
603 }
606 }
609 }
612 }
615 }
616 LLVMTypeRef LLVMIntType(unsigned NumBits) {
617  return LLVMIntTypeInContext(LLVMGetGlobalContext(), NumBits);
618 }
619 
620 unsigned LLVMGetIntTypeWidth(LLVMTypeRef IntegerTy) {
621  return unwrap<IntegerType>(IntegerTy)->getBitWidth();
622 }
623 
624 /*--.. Operations on real types ............................................--*/
625 
627  return (LLVMTypeRef) Type::getHalfTy(*unwrap(C));
628 }
631 }
633  return (LLVMTypeRef) Type::getFloatTy(*unwrap(C));
634 }
637 }
640 }
642  return (LLVMTypeRef) Type::getFP128Ty(*unwrap(C));
643 }
646 }
649 }
652 }
653 
656 }
659 }
662 }
665 }
668 }
671 }
674 }
677 }
680 }
681 
682 /*--.. Operations on function types ........................................--*/
683 
685  LLVMTypeRef *ParamTypes, unsigned ParamCount,
686  LLVMBool IsVarArg) {
687  ArrayRef<Type*> Tys(unwrap(ParamTypes), ParamCount);
688  return wrap(FunctionType::get(unwrap(ReturnType), Tys, IsVarArg != 0));
689 }
690 
692  return unwrap<FunctionType>(FunctionTy)->isVarArg();
693 }
694 
696  return wrap(unwrap<FunctionType>(FunctionTy)->getReturnType());
697 }
698 
699 unsigned LLVMCountParamTypes(LLVMTypeRef FunctionTy) {
700  return unwrap<FunctionType>(FunctionTy)->getNumParams();
701 }
702 
703 void LLVMGetParamTypes(LLVMTypeRef FunctionTy, LLVMTypeRef *Dest) {
704  FunctionType *Ty = unwrap<FunctionType>(FunctionTy);
705  for (Type *T : Ty->params())
706  *Dest++ = wrap(T);
707 }
708 
709 /*--.. Operations on struct types ..........................................--*/
710 
712  unsigned ElementCount, LLVMBool Packed) {
713  ArrayRef<Type*> Tys(unwrap(ElementTypes), ElementCount);
714  return wrap(StructType::get(*unwrap(C), Tys, Packed != 0));
715 }
716 
718  unsigned ElementCount, LLVMBool Packed) {
719  return LLVMStructTypeInContext(LLVMGetGlobalContext(), ElementTypes,
720  ElementCount, Packed);
721 }
722 
724 {
725  return wrap(StructType::create(*unwrap(C), Name));
726 }
727 
729 {
730  StructType *Type = unwrap<StructType>(Ty);
731  if (!Type->hasName())
732  return nullptr;
733  return Type->getName().data();
734 }
735 
736 void LLVMStructSetBody(LLVMTypeRef StructTy, LLVMTypeRef *ElementTypes,
737  unsigned ElementCount, LLVMBool Packed) {
738  ArrayRef<Type*> Tys(unwrap(ElementTypes), ElementCount);
739  unwrap<StructType>(StructTy)->setBody(Tys, Packed != 0);
740 }
741 
743  return unwrap<StructType>(StructTy)->getNumElements();
744 }
745 
747  StructType *Ty = unwrap<StructType>(StructTy);
748  for (Type *T : Ty->elements())
749  *Dest++ = wrap(T);
750 }
751 
753  StructType *Ty = unwrap<StructType>(StructTy);
754  return wrap(Ty->getTypeAtIndex(i));
755 }
756 
758  return unwrap<StructType>(StructTy)->isPacked();
759 }
760 
762  return unwrap<StructType>(StructTy)->isOpaque();
763 }
764 
766  return unwrap<StructType>(StructTy)->isLiteral();
767 }
768 
770  return wrap(StructType::getTypeByName(unwrap(M)->getContext(), Name));
771 }
772 
775 }
776 
777 /*--.. Operations on array, pointer, and vector types (sequence types) .....--*/
778 
780  int i = 0;
781  for (auto *T : unwrap(Tp)->subtypes()) {
782  Arr[i] = wrap(T);
783  i++;
784  }
785 }
786 
788  return wrap(ArrayType::get(unwrap(ElementType), ElementCount));
789 }
790 
792  return wrap(PointerType::get(unwrap(ElementType), AddressSpace));
793 }
794 
796  return unwrap(Ty)->isOpaquePointerTy();
797 }
798 
800  return wrap(FixedVectorType::get(unwrap(ElementType), ElementCount));
801 }
802 
804  unsigned ElementCount) {
805  return wrap(ScalableVectorType::get(unwrap(ElementType), ElementCount));
806 }
807 
809  auto *Ty = unwrap<Type>(WrappedTy);
810  if (auto *PTy = dyn_cast<PointerType>(Ty))
811  return wrap(PTy->getNonOpaquePointerElementType());
812  if (auto *ATy = dyn_cast<ArrayType>(Ty))
813  return wrap(ATy->getElementType());
814  return wrap(cast<VectorType>(Ty)->getElementType());
815 }
816 
818  return unwrap(Tp)->getNumContainedTypes();
819 }
820 
821 unsigned LLVMGetArrayLength(LLVMTypeRef ArrayTy) {
822  return unwrap<ArrayType>(ArrayTy)->getNumElements();
823 }
824 
826  return unwrap<PointerType>(PointerTy)->getAddressSpace();
827 }
828 
829 unsigned LLVMGetVectorSize(LLVMTypeRef VectorTy) {
830  return unwrap<VectorType>(VectorTy)->getElementCount().getKnownMinValue();
831 }
832 
833 /*--.. Operations on other types ...........................................--*/
834 
837 }
838 
840  return wrap(Type::getVoidTy(*unwrap(C)));
841 }
843  return wrap(Type::getLabelTy(*unwrap(C)));
844 }
846  return wrap(Type::getTokenTy(*unwrap(C)));
847 }
849  return wrap(Type::getMetadataTy(*unwrap(C)));
850 }
851 
854 }
857 }
858 
859 /*===-- Operations on values ----------------------------------------------===*/
860 
861 /*--.. Operations on all values ............................................--*/
862 
864  return wrap(unwrap(Val)->getType());
865 }
866 
868  switch(unwrap(Val)->getValueID()) {
869 #define LLVM_C_API 1
870 #define HANDLE_VALUE(Name) \
871  case Value::Name##Val: \
872  return LLVM##Name##ValueKind;
873 #include "llvm/IR/Value.def"
874  default:
876  }
877 }
878 
879 const char *LLVMGetValueName2(LLVMValueRef Val, size_t *Length) {
880  auto *V = unwrap(Val);
881  *Length = V->getName().size();
882  return V->getName().data();
883 }
884 
885 void LLVMSetValueName2(LLVMValueRef Val, const char *Name, size_t NameLen) {
886  unwrap(Val)->setName(StringRef(Name, NameLen));
887 }
888 
889 const char *LLVMGetValueName(LLVMValueRef Val) {
890  return unwrap(Val)->getName().data();
891 }
892 
893 void LLVMSetValueName(LLVMValueRef Val, const char *Name) {
894  unwrap(Val)->setName(Name);
895 }
896 
898  unwrap(Val)->print(errs(), /*IsForDebug=*/true);
899 }
900 
902  std::string buf;
903  raw_string_ostream os(buf);
904 
905  if (unwrap(Val))
906  unwrap(Val)->print(os);
907  else
908  os << "Printing <null> Value";
909 
910  os.flush();
911 
912  return strdup(buf.c_str());
913 }
914 
916  unwrap(OldVal)->replaceAllUsesWith(unwrap(NewVal));
917 }
918 
920  return unwrap<Instruction>(Inst)->hasMetadata();
921 }
922 
923 LLVMValueRef LLVMGetMetadata(LLVMValueRef Inst, unsigned KindID) {
924  auto *I = unwrap<Instruction>(Inst);
925  assert(I && "Expected instruction");
926  if (auto *MD = I->getMetadata(KindID))
927  return wrap(MetadataAsValue::get(I->getContext(), MD));
928  return nullptr;
929 }
930 
931 // MetadataAsValue uses a canonical format which strips the actual MDNode for
932 // MDNode with just a single constant value, storing just a ConstantAsMetadata
933 // This undoes this canonicalization, reconstructing the MDNode.
935  Metadata *MD = MAV->getMetadata();
936  assert((isa<MDNode>(MD) || isa<ConstantAsMetadata>(MD)) &&
937  "Expected a metadata node or a canonicalized constant");
938 
939  if (MDNode *N = dyn_cast<MDNode>(MD))
940  return N;
941 
942  return MDNode::get(MAV->getContext(), MD);
943 }
944 
945 void LLVMSetMetadata(LLVMValueRef Inst, unsigned KindID, LLVMValueRef Val) {
946  MDNode *N = Val ? extractMDNode(unwrap<MetadataAsValue>(Val)) : nullptr;
947 
948  unwrap<Instruction>(Inst)->setMetadata(KindID, N);
949 }
950 
952  unsigned Kind;
954 };
955 
957 static LLVMValueMetadataEntry *
958 llvm_getMetadata(size_t *NumEntries,
959  llvm::function_ref<void(MetadataEntries &)> AccessMD) {
961  AccessMD(MVEs);
962 
964  static_cast<LLVMOpaqueValueMetadataEntry *>(
965  safe_malloc(MVEs.size() * sizeof(LLVMOpaqueValueMetadataEntry)));
966  for (unsigned i = 0; i < MVEs.size(); ++i) {
967  const auto &ModuleFlag = MVEs[i];
968  Result[i].Kind = ModuleFlag.first;
969  Result[i].Metadata = wrap(ModuleFlag.second);
970  }
971  *NumEntries = MVEs.size();
972  return Result;
973 }
974 
977  size_t *NumEntries) {
978  return llvm_getMetadata(NumEntries, [&Value](MetadataEntries &Entries) {
979  Entries.clear();
980  unwrap<Instruction>(Value)->getAllMetadata(Entries);
981  });
982 }
983 
984 /*--.. Conversion functions ................................................--*/
985 
986 #define LLVM_DEFINE_VALUE_CAST(name) \
987  LLVMValueRef LLVMIsA##name(LLVMValueRef Val) { \
988  return wrap(static_cast<Value*>(dyn_cast_or_null<name>(unwrap(Val)))); \
989  }
990 
992 
994  if (auto *MD = dyn_cast_or_null<MetadataAsValue>(unwrap(Val)))
995  if (isa<MDNode>(MD->getMetadata()) ||
996  isa<ValueAsMetadata>(MD->getMetadata()))
997  return Val;
998  return nullptr;
999 }
1000 
1002  if (auto *MD = dyn_cast_or_null<MetadataAsValue>(unwrap(Val)))
1003  if (isa<MDString>(MD->getMetadata()))
1004  return Val;
1005  return nullptr;
1006 }
1007 
1008 /*--.. Operations on Uses ..................................................--*/
1010  Value *V = unwrap(Val);
1012  if (I == V->use_end())
1013  return nullptr;
1014  return wrap(&*I);
1015 }
1016 
1018  Use *Next = unwrap(U)->getNext();
1019  if (Next)
1020  return wrap(Next);
1021  return nullptr;
1022 }
1023 
1025  return wrap(unwrap(U)->getUser());
1026 }
1027 
1029  return wrap(unwrap(U)->get());
1030 }
1031 
1032 /*--.. Operations on Users .................................................--*/
1033 
1035  unsigned Index) {
1036  Metadata *Op = N->getOperand(Index);
1037  if (!Op)
1038  return nullptr;
1039  if (auto *C = dyn_cast<ConstantAsMetadata>(Op))
1040  return wrap(C->getValue());
1041  return wrap(MetadataAsValue::get(Context, Op));
1042 }
1043 
1045  Value *V = unwrap(Val);
1046  if (auto *MD = dyn_cast<MetadataAsValue>(V)) {
1047  if (auto *L = dyn_cast<ValueAsMetadata>(MD->getMetadata())) {
1048  assert(Index == 0 && "Function-local metadata can only have one operand");
1049  return wrap(L->getValue());
1050  }
1051  return getMDNodeOperandImpl(V->getContext(),
1052  cast<MDNode>(MD->getMetadata()), Index);
1053  }
1054 
1055  return wrap(cast<User>(V)->getOperand(Index));
1056 }
1057 
1059  Value *V = unwrap(Val);
1060  return wrap(&cast<User>(V)->getOperandUse(Index));
1061 }
1062 
1064  unwrap<User>(Val)->setOperand(Index, unwrap(Op));
1065 }
1066 
1068  Value *V = unwrap(Val);
1069  if (isa<MetadataAsValue>(V))
1070  return LLVMGetMDNodeNumOperands(Val);
1071 
1072  return cast<User>(V)->getNumOperands();
1073 }
1074 
1075 /*--.. Operations on constants of any type .................................--*/
1076 
1078  return wrap(Constant::getNullValue(unwrap(Ty)));
1079 }
1080 
1082  return wrap(Constant::getAllOnesValue(unwrap(Ty)));
1083 }
1084 
1086  return wrap(UndefValue::get(unwrap(Ty)));
1087 }
1088 
1090  return wrap(PoisonValue::get(unwrap(Ty)));
1091 }
1092 
1094  return isa<Constant>(unwrap(Ty));
1095 }
1096 
1098  if (Constant *C = dyn_cast<Constant>(unwrap(Val)))
1099  return C->isNullValue();
1100  return false;
1101 }
1102 
1104  return isa<UndefValue>(unwrap(Val));
1105 }
1106 
1108  return isa<PoisonValue>(unwrap(Val));
1109 }
1110 
1112  return wrap(ConstantPointerNull::get(unwrap<PointerType>(Ty)));
1113 }
1114 
1115 /*--.. Operations on metadata nodes ........................................--*/
1116 
1118  size_t SLen) {
1119  return wrap(MDString::get(*unwrap(C), StringRef(Str, SLen)));
1120 }
1121 
1123  size_t Count) {
1124  return wrap(MDNode::get(*unwrap(C), ArrayRef<Metadata*>(unwrap(MDs), Count)));
1125 }
1126 
1128  unsigned SLen) {
1129  LLVMContext &Context = *unwrap(C);
1130  return wrap(MetadataAsValue::get(
1131  Context, MDString::get(Context, StringRef(Str, SLen))));
1132 }
1133 
1134 LLVMValueRef LLVMMDString(const char *Str, unsigned SLen) {
1135  return LLVMMDStringInContext(LLVMGetGlobalContext(), Str, SLen);
1136 }
1137 
1139  unsigned Count) {
1140  LLVMContext &Context = *unwrap(C);
1142  for (auto *OV : makeArrayRef(Vals, Count)) {
1143  Value *V = unwrap(OV);
1144  Metadata *MD;
1145  if (!V)
1146  MD = nullptr;
1147  else if (auto *C = dyn_cast<Constant>(V))
1148  MD = ConstantAsMetadata::get(C);
1149  else if (auto *MDV = dyn_cast<MetadataAsValue>(V)) {
1150  MD = MDV->getMetadata();
1151  assert(!isa<LocalAsMetadata>(MD) && "Unexpected function-local metadata "
1152  "outside of direct argument to call");
1153  } else {
1154  // This is function-local metadata. Pretend to make an MDNode.
1155  assert(Count == 1 &&
1156  "Expected only one operand to function-local metadata");
1158  }
1159 
1160  MDs.push_back(MD);
1161  }
1163 }
1164 
1165 LLVMValueRef LLVMMDNode(LLVMValueRef *Vals, unsigned Count) {
1166  return LLVMMDNodeInContext(LLVMGetGlobalContext(), Vals, Count);
1167 }
1168 
1170  return wrap(MetadataAsValue::get(*unwrap(C), unwrap(MD)));
1171 }
1172 
1174  auto *V = unwrap(Val);
1175  if (auto *C = dyn_cast<Constant>(V))
1176  return wrap(ConstantAsMetadata::get(C));
1177  if (auto *MAV = dyn_cast<MetadataAsValue>(V))
1178  return wrap(MAV->getMetadata());
1179  return wrap(ValueAsMetadata::get(V));
1180 }
1181 
1182 const char *LLVMGetMDString(LLVMValueRef V, unsigned *Length) {
1183  if (const auto *MD = dyn_cast<MetadataAsValue>(unwrap(V)))
1184  if (const MDString *S = dyn_cast<MDString>(MD->getMetadata())) {
1185  *Length = S->getString().size();
1186  return S->getString().data();
1187  }
1188  *Length = 0;
1189  return nullptr;
1190 }
1191 
1193  auto *MD = cast<MetadataAsValue>(unwrap(V));
1194  if (isa<ValueAsMetadata>(MD->getMetadata()))
1195  return 1;
1196  return cast<MDNode>(MD->getMetadata())->getNumOperands();
1197 }
1198 
1200  Module *Mod = unwrap(M);
1202  if (I == Mod->named_metadata_end())
1203  return nullptr;
1204  return wrap(&*I);
1205 }
1206 
1208  Module *Mod = unwrap(M);
1210  if (I == Mod->named_metadata_begin())
1211  return nullptr;
1212  return wrap(&*--I);
1213 }
1214 
1216  NamedMDNode *NamedNode = unwrap<NamedMDNode>(NMD);
1218  if (++I == NamedNode->getParent()->named_metadata_end())
1219  return nullptr;
1220  return wrap(&*I);
1221 }
1222 
1224  NamedMDNode *NamedNode = unwrap<NamedMDNode>(NMD);
1226  if (I == NamedNode->getParent()->named_metadata_begin())
1227  return nullptr;
1228  return wrap(&*--I);
1229 }
1230 
1232  const char *Name, size_t NameLen) {
1233  return wrap(unwrap(M)->getNamedMetadata(StringRef(Name, NameLen)));
1234 }
1235 
1237  const char *Name, size_t NameLen) {
1238  return wrap(unwrap(M)->getOrInsertNamedMetadata({Name, NameLen}));
1239 }
1240 
1241 const char *LLVMGetNamedMetadataName(LLVMNamedMDNodeRef NMD, size_t *NameLen) {
1242  NamedMDNode *NamedNode = unwrap<NamedMDNode>(NMD);
1243  *NameLen = NamedNode->getName().size();
1244  return NamedNode->getName().data();
1245 }
1246 
1248  auto *MD = cast<MetadataAsValue>(unwrap(V));
1249  if (auto *MDV = dyn_cast<ValueAsMetadata>(MD->getMetadata())) {
1250  *Dest = wrap(MDV->getValue());
1251  return;
1252  }
1253  const auto *N = cast<MDNode>(MD->getMetadata());
1254  const unsigned numOperands = N->getNumOperands();
1255  LLVMContext &Context = unwrap(V)->getContext();
1256  for (unsigned i = 0; i < numOperands; i++)
1257  Dest[i] = getMDNodeOperandImpl(Context, N, i);
1258 }
1259 
1261  if (NamedMDNode *N = unwrap(M)->getNamedMetadata(Name)) {
1262  return N->getNumOperands();
1263  }
1264  return 0;
1265 }
1266 
1268  LLVMValueRef *Dest) {
1269  NamedMDNode *N = unwrap(M)->getNamedMetadata(Name);
1270  if (!N)
1271  return;
1272  LLVMContext &Context = unwrap(M)->getContext();
1273  for (unsigned i=0;i<N->getNumOperands();i++)
1274  Dest[i] = wrap(MetadataAsValue::get(Context, N->getOperand(i)));
1275 }
1276 
1278  LLVMValueRef Val) {
1279  NamedMDNode *N = unwrap(M)->getOrInsertNamedMetadata(Name);
1280  if (!N)
1281  return;
1282  if (!Val)
1283  return;
1284  N->addOperand(extractMDNode(unwrap<MetadataAsValue>(Val)));
1285 }
1286 
1287 const char *LLVMGetDebugLocDirectory(LLVMValueRef Val, unsigned *Length) {
1288  if (!Length) return nullptr;
1289  StringRef S;
1290  if (const auto *I = dyn_cast<Instruction>(unwrap(Val))) {
1291  if (const auto &DL = I->getDebugLoc()) {
1292  S = DL->getDirectory();
1293  }
1294  } else if (const auto *GV = dyn_cast<GlobalVariable>(unwrap(Val))) {
1296  GV->getDebugInfo(GVEs);
1297  if (GVEs.size())
1298  if (const DIGlobalVariable *DGV = GVEs[0]->getVariable())
1299  S = DGV->getDirectory();
1300  } else if (const auto *F = dyn_cast<Function>(unwrap(Val))) {
1301  if (const DISubprogram *DSP = F->getSubprogram())
1302  S = DSP->getDirectory();
1303  } else {
1304  assert(0 && "Expected Instruction, GlobalVariable or Function");
1305  return nullptr;
1306  }
1307  *Length = S.size();
1308  return S.data();
1309 }
1310 
1311 const char *LLVMGetDebugLocFilename(LLVMValueRef Val, unsigned *Length) {
1312  if (!Length) return nullptr;
1313  StringRef S;
1314  if (const auto *I = dyn_cast<Instruction>(unwrap(Val))) {
1315  if (const auto &DL = I->getDebugLoc()) {
1316  S = DL->getFilename();
1317  }
1318  } else if (const auto *GV = dyn_cast<GlobalVariable>(unwrap(Val))) {
1320  GV->getDebugInfo(GVEs);
1321  if (GVEs.size())
1322  if (const DIGlobalVariable *DGV = GVEs[0]->getVariable())
1323  S = DGV->getFilename();
1324  } else if (const auto *F = dyn_cast<Function>(unwrap(Val))) {
1325  if (const DISubprogram *DSP = F->getSubprogram())
1326  S = DSP->getFilename();
1327  } else {
1328  assert(0 && "Expected Instruction, GlobalVariable or Function");
1329  return nullptr;
1330  }
1331  *Length = S.size();
1332  return S.data();
1333 }
1334 
1336  unsigned L = 0;
1337  if (const auto *I = dyn_cast<Instruction>(unwrap(Val))) {
1338  if (const auto &DL = I->getDebugLoc()) {
1339  L = DL->getLine();
1340  }
1341  } else if (const auto *GV = dyn_cast<GlobalVariable>(unwrap(Val))) {
1343  GV->getDebugInfo(GVEs);
1344  if (GVEs.size())
1345  if (const DIGlobalVariable *DGV = GVEs[0]->getVariable())
1346  L = DGV->getLine();
1347  } else if (const auto *F = dyn_cast<Function>(unwrap(Val))) {
1348  if (const DISubprogram *DSP = F->getSubprogram())
1349  L = DSP->getLine();
1350  } else {
1351  assert(0 && "Expected Instruction, GlobalVariable or Function");
1352  return -1;
1353  }
1354  return L;
1355 }
1356 
1358  unsigned C = 0;
1359  if (const auto *I = dyn_cast<Instruction>(unwrap(Val)))
1360  if (const auto &DL = I->getDebugLoc())
1361  C = DL->getColumn();
1362  return C;
1363 }
1364 
1365 /*--.. Operations on scalar constants ......................................--*/
1366 
1367 LLVMValueRef LLVMConstInt(LLVMTypeRef IntTy, unsigned long long N,
1368  LLVMBool SignExtend) {
1369  return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), N, SignExtend != 0));
1370 }
1371 
1373  unsigned NumWords,
1374  const uint64_t Words[]) {
1375  IntegerType *Ty = unwrap<IntegerType>(IntTy);
1376  return wrap(ConstantInt::get(Ty->getContext(),
1377  APInt(Ty->getBitWidth(),
1378  makeArrayRef(Words, NumWords))));
1379 }
1380 
1382  uint8_t Radix) {
1383  return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), StringRef(Str),
1384  Radix));
1385 }
1386 
1388  unsigned SLen, uint8_t Radix) {
1389  return wrap(ConstantInt::get(unwrap<IntegerType>(IntTy), StringRef(Str, SLen),
1390  Radix));
1391 }
1392 
1394  return wrap(ConstantFP::get(unwrap(RealTy), N));
1395 }
1396 
1398  return wrap(ConstantFP::get(unwrap(RealTy), StringRef(Text)));
1399 }
1400 
1402  unsigned SLen) {
1403  return wrap(ConstantFP::get(unwrap(RealTy), StringRef(Str, SLen)));
1404 }
1405 
1406 unsigned long long LLVMConstIntGetZExtValue(LLVMValueRef ConstantVal) {
1407  return unwrap<ConstantInt>(ConstantVal)->getZExtValue();
1408 }
1409 
1410 long long LLVMConstIntGetSExtValue(LLVMValueRef ConstantVal) {
1411  return unwrap<ConstantInt>(ConstantVal)->getSExtValue();
1412 }
1413 
1414 double LLVMConstRealGetDouble(LLVMValueRef ConstantVal, LLVMBool *LosesInfo) {
1415  ConstantFP *cFP = unwrap<ConstantFP>(ConstantVal) ;
1416  Type *Ty = cFP->getType();
1417 
1418  if (Ty->isHalfTy() || Ty->isBFloatTy() || Ty->isFloatTy() ||
1419  Ty->isDoubleTy()) {
1420  *LosesInfo = false;
1421  return cFP->getValueAPF().convertToDouble();
1422  }
1423 
1424  bool APFLosesInfo;
1425  APFloat APF = cFP->getValueAPF();
1427  *LosesInfo = APFLosesInfo;
1428  return APF.convertToDouble();
1429 }
1430 
1431 /*--.. Operations on composite constants ...................................--*/
1432 
1434  unsigned Length,
1435  LLVMBool DontNullTerminate) {
1436  /* Inverted the sense of AddNull because ', 0)' is a
1437  better mnemonic for null termination than ', 1)'. */
1438  return wrap(ConstantDataArray::getString(*unwrap(C), StringRef(Str, Length),
1439  DontNullTerminate == 0));
1440 }
1441 
1442 LLVMValueRef LLVMConstString(const char *Str, unsigned Length,
1443  LLVMBool DontNullTerminate) {
1444  return LLVMConstStringInContext(LLVMGetGlobalContext(), Str, Length,
1445  DontNullTerminate);
1446 }
1447 
1449  return wrap(unwrap<Constant>(C)->getAggregateElement(Idx));
1450 }
1451 
1453  return wrap(unwrap<ConstantDataSequential>(C)->getElementAsConstant(idx));
1454 }
1455 
1457  return unwrap<ConstantDataSequential>(C)->isString();
1458 }
1459 
1460 const char *LLVMGetAsString(LLVMValueRef C, size_t *Length) {
1461  StringRef Str = unwrap<ConstantDataSequential>(C)->getAsString();
1462  *Length = Str.size();
1463  return Str.data();
1464 }
1465 
1467  LLVMValueRef *ConstantVals, unsigned Length) {
1468  ArrayRef<Constant*> V(unwrap<Constant>(ConstantVals, Length), Length);
1469  return wrap(ConstantArray::get(ArrayType::get(unwrap(ElementTy), Length), V));
1470 }
1471 
1473  LLVMValueRef *ConstantVals,
1474  unsigned Count, LLVMBool Packed) {
1475  Constant **Elements = unwrap<Constant>(ConstantVals, Count);
1476  return wrap(ConstantStruct::getAnon(*unwrap(C), makeArrayRef(Elements, Count),
1477  Packed != 0));
1478 }
1479 
1480 LLVMValueRef LLVMConstStruct(LLVMValueRef *ConstantVals, unsigned Count,
1481  LLVMBool Packed) {
1482  return LLVMConstStructInContext(LLVMGetGlobalContext(), ConstantVals, Count,
1483  Packed);
1484 }
1485 
1487  LLVMValueRef *ConstantVals,
1488  unsigned Count) {
1489  Constant **Elements = unwrap<Constant>(ConstantVals, Count);
1490  StructType *Ty = cast<StructType>(unwrap(StructTy));
1491 
1492  return wrap(ConstantStruct::get(Ty, makeArrayRef(Elements, Count)));
1493 }
1494 
1495 LLVMValueRef LLVMConstVector(LLVMValueRef *ScalarConstantVals, unsigned Size) {
1497  unwrap<Constant>(ScalarConstantVals, Size), Size)));
1498 }
1499 
1500 /*-- Opcode mapping */
1501 
1502 static LLVMOpcode map_to_llvmopcode(int opcode)
1503 {
1504  switch (opcode) {
1505  default: llvm_unreachable("Unhandled Opcode.");
1506 #define HANDLE_INST(num, opc, clas) case num: return LLVM##opc;
1507 #include "llvm/IR/Instruction.def"
1508 #undef HANDLE_INST
1509  }
1510 }
1511 
1513 {
1514  switch (code) {
1515 #define HANDLE_INST(num, opc, clas) case LLVM##opc: return num;
1516 #include "llvm/IR/Instruction.def"
1517 #undef HANDLE_INST
1518  }
1519  llvm_unreachable("Unhandled Opcode.");
1520 }
1521 
1522 /*--.. Constant expressions ................................................--*/
1523 
1525  return map_to_llvmopcode(unwrap<ConstantExpr>(ConstantVal)->getOpcode());
1526 }
1527 
1529  return wrap(ConstantExpr::getAlignOf(unwrap(Ty)));
1530 }
1531 
1533  return wrap(ConstantExpr::getSizeOf(unwrap(Ty)));
1534 }
1535 
1537  return wrap(ConstantExpr::getNeg(unwrap<Constant>(ConstantVal)));
1538 }
1539 
1541  return wrap(ConstantExpr::getNSWNeg(unwrap<Constant>(ConstantVal)));
1542 }
1543 
1545  return wrap(ConstantExpr::getNUWNeg(unwrap<Constant>(ConstantVal)));
1546 }
1547 
1548 
1550  return wrap(ConstantExpr::getFNeg(unwrap<Constant>(ConstantVal)));
1551 }
1552 
1554  return wrap(ConstantExpr::getNot(unwrap<Constant>(ConstantVal)));
1555 }
1556 
1558  return wrap(ConstantExpr::getAdd(unwrap<Constant>(LHSConstant),
1559  unwrap<Constant>(RHSConstant)));
1560 }
1561 
1563  LLVMValueRef RHSConstant) {
1564  return wrap(ConstantExpr::getNSWAdd(unwrap<Constant>(LHSConstant),
1565  unwrap<Constant>(RHSConstant)));
1566 }
1567 
1569  LLVMValueRef RHSConstant) {
1570  return wrap(ConstantExpr::getNUWAdd(unwrap<Constant>(LHSConstant),
1571  unwrap<Constant>(RHSConstant)));
1572 }
1573 
1575  return wrap(ConstantExpr::getFAdd(unwrap<Constant>(LHSConstant),
1576  unwrap<Constant>(RHSConstant)));
1577 }
1578 
1580  return wrap(ConstantExpr::getSub(unwrap<Constant>(LHSConstant),
1581  unwrap<Constant>(RHSConstant)));
1582 }
1583 
1585  LLVMValueRef RHSConstant) {
1586  return wrap(ConstantExpr::getNSWSub(unwrap<Constant>(LHSConstant),
1587  unwrap<Constant>(RHSConstant)));
1588 }
1589 
1591  LLVMValueRef RHSConstant) {
1592  return wrap(ConstantExpr::getNUWSub(unwrap<Constant>(LHSConstant),
1593  unwrap<Constant>(RHSConstant)));
1594 }
1595 
1597  return wrap(ConstantExpr::getFSub(unwrap<Constant>(LHSConstant),
1598  unwrap<Constant>(RHSConstant)));
1599 }
1600 
1602  return wrap(ConstantExpr::getMul(unwrap<Constant>(LHSConstant),
1603  unwrap<Constant>(RHSConstant)));
1604 }
1605 
1607  LLVMValueRef RHSConstant) {
1608  return wrap(ConstantExpr::getNSWMul(unwrap<Constant>(LHSConstant),
1609  unwrap<Constant>(RHSConstant)));
1610 }
1611 
1613  LLVMValueRef RHSConstant) {
1614  return wrap(ConstantExpr::getNUWMul(unwrap<Constant>(LHSConstant),
1615  unwrap<Constant>(RHSConstant)));
1616 }
1617 
1619  return wrap(ConstantExpr::getFMul(unwrap<Constant>(LHSConstant),
1620  unwrap<Constant>(RHSConstant)));
1621 }
1622 
1624  return wrap(ConstantExpr::getUDiv(unwrap<Constant>(LHSConstant),
1625  unwrap<Constant>(RHSConstant)));
1626 }
1627 
1629  LLVMValueRef RHSConstant) {
1630  return wrap(ConstantExpr::getExactUDiv(unwrap<Constant>(LHSConstant),
1631  unwrap<Constant>(RHSConstant)));
1632 }
1633 
1635  return wrap(ConstantExpr::getSDiv(unwrap<Constant>(LHSConstant),
1636  unwrap<Constant>(RHSConstant)));
1637 }
1638 
1640  LLVMValueRef RHSConstant) {
1641  return wrap(ConstantExpr::getExactSDiv(unwrap<Constant>(LHSConstant),
1642  unwrap<Constant>(RHSConstant)));
1643 }
1644 
1646  return wrap(ConstantExpr::getFDiv(unwrap<Constant>(LHSConstant),
1647  unwrap<Constant>(RHSConstant)));
1648 }
1649 
1651  return wrap(ConstantExpr::getURem(unwrap<Constant>(LHSConstant),
1652  unwrap<Constant>(RHSConstant)));
1653 }
1654 
1656  return wrap(ConstantExpr::getSRem(unwrap<Constant>(LHSConstant),
1657  unwrap<Constant>(RHSConstant)));
1658 }
1659 
1661  return wrap(ConstantExpr::getFRem(unwrap<Constant>(LHSConstant),
1662  unwrap<Constant>(RHSConstant)));
1663 }
1664 
1666  return wrap(ConstantExpr::getAnd(unwrap<Constant>(LHSConstant),
1667  unwrap<Constant>(RHSConstant)));
1668 }
1669 
1671  return wrap(ConstantExpr::getOr(unwrap<Constant>(LHSConstant),
1672  unwrap<Constant>(RHSConstant)));
1673 }
1674 
1676  return wrap(ConstantExpr::getXor(unwrap<Constant>(LHSConstant),
1677  unwrap<Constant>(RHSConstant)));
1678 }
1679 
1681  LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1683  unwrap<Constant>(LHSConstant),
1684  unwrap<Constant>(RHSConstant)));
1685 }
1686 
1688  LLVMValueRef LHSConstant, LLVMValueRef RHSConstant) {
1690  unwrap<Constant>(LHSConstant),
1691  unwrap<Constant>(RHSConstant)));
1692 }
1693 
1695  return wrap(ConstantExpr::getShl(unwrap<Constant>(LHSConstant),
1696  unwrap<Constant>(RHSConstant)));
1697 }
1698 
1700  return wrap(ConstantExpr::getLShr(unwrap<Constant>(LHSConstant),
1701  unwrap<Constant>(RHSConstant)));
1702 }
1703 
1705  return wrap(ConstantExpr::getAShr(unwrap<Constant>(LHSConstant),
1706  unwrap<Constant>(RHSConstant)));
1707 }
1708 
1710  LLVMValueRef *ConstantIndices, unsigned NumIndices) {
1711  ArrayRef<Constant *> IdxList(unwrap<Constant>(ConstantIndices, NumIndices),
1712  NumIndices);
1713  Constant *Val = unwrap<Constant>(ConstantVal);
1715  return wrap(ConstantExpr::getGetElementPtr(Ty, Val, IdxList));
1716 }
1717 
1719  LLVMValueRef *ConstantIndices, unsigned NumIndices) {
1720  ArrayRef<Constant *> IdxList(unwrap<Constant>(ConstantIndices, NumIndices),
1721  NumIndices);
1722  Constant *Val = unwrap<Constant>(ConstantVal);
1723  return wrap(ConstantExpr::getGetElementPtr(unwrap(Ty), Val, IdxList));
1724 }
1725 
1727  LLVMValueRef *ConstantIndices,
1728  unsigned NumIndices) {
1729  ArrayRef<Constant *> IdxList(unwrap<Constant>(ConstantIndices, NumIndices),
1730  NumIndices);
1731  Constant *Val = unwrap<Constant>(ConstantVal);
1733  return wrap(ConstantExpr::getInBoundsGetElementPtr(Ty, Val, IdxList));
1734 }
1735 
1737  LLVMValueRef *ConstantIndices,
1738  unsigned NumIndices) {
1739  ArrayRef<Constant *> IdxList(unwrap<Constant>(ConstantIndices, NumIndices),
1740  NumIndices);
1741  Constant *Val = unwrap<Constant>(ConstantVal);
1742  return wrap(ConstantExpr::getInBoundsGetElementPtr(unwrap(Ty), Val, IdxList));
1743 }
1744 
1746  return wrap(ConstantExpr::getTrunc(unwrap<Constant>(ConstantVal),
1747  unwrap(ToType)));
1748 }
1749 
1751  return wrap(ConstantExpr::getSExt(unwrap<Constant>(ConstantVal),
1752  unwrap(ToType)));
1753 }
1754 
1756  return wrap(ConstantExpr::getZExt(unwrap<Constant>(ConstantVal),
1757  unwrap(ToType)));
1758 }
1759 
1761  return wrap(ConstantExpr::getFPTrunc(unwrap<Constant>(ConstantVal),
1762  unwrap(ToType)));
1763 }
1764 
1766  return wrap(ConstantExpr::getFPExtend(unwrap<Constant>(ConstantVal),
1767  unwrap(ToType)));
1768 }
1769 
1771  return wrap(ConstantExpr::getUIToFP(unwrap<Constant>(ConstantVal),
1772  unwrap(ToType)));
1773 }
1774 
1776  return wrap(ConstantExpr::getSIToFP(unwrap<Constant>(ConstantVal),
1777  unwrap(ToType)));
1778 }
1779 
1781  return wrap(ConstantExpr::getFPToUI(unwrap<Constant>(ConstantVal),
1782  unwrap(ToType)));
1783 }
1784 
1786  return wrap(ConstantExpr::getFPToSI(unwrap<Constant>(ConstantVal),
1787  unwrap(ToType)));
1788 }
1789 
1791  return wrap(ConstantExpr::getPtrToInt(unwrap<Constant>(ConstantVal),
1792  unwrap(ToType)));
1793 }
1794 
1796  return wrap(ConstantExpr::getIntToPtr(unwrap<Constant>(ConstantVal),
1797  unwrap(ToType)));
1798 }
1799 
1801  return wrap(ConstantExpr::getBitCast(unwrap<Constant>(ConstantVal),
1802  unwrap(ToType)));
1803 }
1804 
1806  LLVMTypeRef ToType) {
1807  return wrap(ConstantExpr::getAddrSpaceCast(unwrap<Constant>(ConstantVal),
1808  unwrap(ToType)));
1809 }
1810 
1812  LLVMTypeRef ToType) {
1813  return wrap(ConstantExpr::getZExtOrBitCast(unwrap<Constant>(ConstantVal),
1814  unwrap(ToType)));
1815 }
1816 
1818  LLVMTypeRef ToType) {
1819  return wrap(ConstantExpr::getSExtOrBitCast(unwrap<Constant>(ConstantVal),
1820  unwrap(ToType)));
1821 }
1822 
1824  LLVMTypeRef ToType) {
1825  return wrap(ConstantExpr::getTruncOrBitCast(unwrap<Constant>(ConstantVal),
1826  unwrap(ToType)));
1827 }
1828 
1830  LLVMTypeRef ToType) {
1831  return wrap(ConstantExpr::getPointerCast(unwrap<Constant>(ConstantVal),
1832  unwrap(ToType)));
1833 }
1834 
1836  LLVMBool isSigned) {
1837  return wrap(ConstantExpr::getIntegerCast(unwrap<Constant>(ConstantVal),
1838  unwrap(ToType), isSigned));
1839 }
1840 
1842  return wrap(ConstantExpr::getFPCast(unwrap<Constant>(ConstantVal),
1843  unwrap(ToType)));
1844 }
1845 
1847  LLVMValueRef ConstantIfTrue,
1848  LLVMValueRef ConstantIfFalse) {
1849  return wrap(ConstantExpr::getSelect(unwrap<Constant>(ConstantCondition),
1850  unwrap<Constant>(ConstantIfTrue),
1851  unwrap<Constant>(ConstantIfFalse)));
1852 }
1853 
1855  LLVMValueRef IndexConstant) {
1856  return wrap(ConstantExpr::getExtractElement(unwrap<Constant>(VectorConstant),
1857  unwrap<Constant>(IndexConstant)));
1858 }
1859 
1861  LLVMValueRef ElementValueConstant,
1862  LLVMValueRef IndexConstant) {
1863  return wrap(ConstantExpr::getInsertElement(unwrap<Constant>(VectorConstant),
1864  unwrap<Constant>(ElementValueConstant),
1865  unwrap<Constant>(IndexConstant)));
1866 }
1867 
1869  LLVMValueRef VectorBConstant,
1870  LLVMValueRef MaskConstant) {
1871  SmallVector<int, 16> IntMask;
1872  ShuffleVectorInst::getShuffleMask(unwrap<Constant>(MaskConstant), IntMask);
1873  return wrap(ConstantExpr::getShuffleVector(unwrap<Constant>(VectorAConstant),
1874  unwrap<Constant>(VectorBConstant),
1875  IntMask));
1876 }
1877 
1878 LLVMValueRef LLVMConstExtractValue(LLVMValueRef AggConstant, unsigned *IdxList,
1879  unsigned NumIdx) {
1880  return wrap(ConstantExpr::getExtractValue(unwrap<Constant>(AggConstant),
1881  makeArrayRef(IdxList, NumIdx)));
1882 }
1883 
1885  LLVMValueRef ElementValueConstant,
1886  unsigned *IdxList, unsigned NumIdx) {
1887  return wrap(ConstantExpr::getInsertValue(unwrap<Constant>(AggConstant),
1888  unwrap<Constant>(ElementValueConstant),
1889  makeArrayRef(IdxList, NumIdx)));
1890 }
1891 
1892 LLVMValueRef LLVMConstInlineAsm(LLVMTypeRef Ty, const char *AsmString,
1893  const char *Constraints,
1894  LLVMBool HasSideEffects,
1895  LLVMBool IsAlignStack) {
1896  return wrap(InlineAsm::get(dyn_cast<FunctionType>(unwrap(Ty)), AsmString,
1897  Constraints, HasSideEffects, IsAlignStack));
1898 }
1899 
1901  return wrap(BlockAddress::get(unwrap<Function>(F), unwrap(BB)));
1902 }
1903 
1904 /*--.. Operations on global variables, functions, and aliases (globals) ....--*/
1905 
1907  return wrap(unwrap<GlobalValue>(Global)->getParent());
1908 }
1909 
1911  return unwrap<GlobalValue>(Global)->isDeclaration();
1912 }
1913 
1915  switch (unwrap<GlobalValue>(Global)->getLinkage()) {
1917  return LLVMExternalLinkage;
1921  return LLVMLinkOnceAnyLinkage;
1923  return LLVMLinkOnceODRLinkage;
1925  return LLVMWeakAnyLinkage;
1927  return LLVMWeakODRLinkage;
1929  return LLVMAppendingLinkage;
1931  return LLVMInternalLinkage;
1933  return LLVMPrivateLinkage;
1935  return LLVMExternalWeakLinkage;
1937  return LLVMCommonLinkage;
1938  }
1939 
1940  llvm_unreachable("Invalid GlobalValue linkage!");
1941 }
1942 
1943 void LLVMSetLinkage(LLVMValueRef Global, LLVMLinkage Linkage) {
1944  GlobalValue *GV = unwrap<GlobalValue>(Global);
1945 
1946  switch (Linkage) {
1947  case LLVMExternalLinkage:
1949  break;
1952  break;
1955  break;
1958  break;
1960  LLVM_DEBUG(
1961  errs() << "LLVMSetLinkage(): LLVMLinkOnceODRAutoHideLinkage is no "
1962  "longer supported.");
1963  break;
1964  case LLVMWeakAnyLinkage:
1966  break;
1967  case LLVMWeakODRLinkage:
1969  break;
1970  case LLVMAppendingLinkage:
1972  break;
1973  case LLVMInternalLinkage:
1975  break;
1976  case LLVMPrivateLinkage:
1978  break;
1981  break;
1984  break;
1985  case LLVMDLLImportLinkage:
1986  LLVM_DEBUG(
1987  errs()
1988  << "LLVMSetLinkage(): LLVMDLLImportLinkage is no longer supported.");
1989  break;
1990  case LLVMDLLExportLinkage:
1991  LLVM_DEBUG(
1992  errs()
1993  << "LLVMSetLinkage(): LLVMDLLExportLinkage is no longer supported.");
1994  break;
1997  break;
1998  case LLVMGhostLinkage:
1999  LLVM_DEBUG(
2000  errs() << "LLVMSetLinkage(): LLVMGhostLinkage is no longer supported.");
2001  break;
2002  case LLVMCommonLinkage:
2004  break;
2005  }
2006 }
2007 
2008 const char *LLVMGetSection(LLVMValueRef Global) {
2009  // Using .data() is safe because of how GlobalObject::setSection is
2010  // implemented.
2011  return unwrap<GlobalValue>(Global)->getSection().data();
2012 }
2013 
2014 void LLVMSetSection(LLVMValueRef Global, const char *Section) {
2015  unwrap<GlobalObject>(Global)->setSection(Section);
2016 }
2017 
2019  return static_cast<LLVMVisibility>(
2020  unwrap<GlobalValue>(Global)->getVisibility());
2021 }
2022 
2024  unwrap<GlobalValue>(Global)
2025  ->setVisibility(static_cast<GlobalValue::VisibilityTypes>(Viz));
2026 }
2027 
2029  return static_cast<LLVMDLLStorageClass>(
2030  unwrap<GlobalValue>(Global)->getDLLStorageClass());
2031 }
2032 
2034  unwrap<GlobalValue>(Global)->setDLLStorageClass(
2035  static_cast<GlobalValue::DLLStorageClassTypes>(Class));
2036 }
2037 
2039  switch (unwrap<GlobalValue>(Global)->getUnnamedAddr()) {
2041  return LLVMNoUnnamedAddr;
2043  return LLVMLocalUnnamedAddr;
2045  return LLVMGlobalUnnamedAddr;
2046  }
2047  llvm_unreachable("Unknown UnnamedAddr kind!");
2048 }
2049 
2051  GlobalValue *GV = unwrap<GlobalValue>(Global);
2052 
2053  switch (UnnamedAddr) {
2054  case LLVMNoUnnamedAddr:
2056  case LLVMLocalUnnamedAddr:
2058  case LLVMGlobalUnnamedAddr:
2060  }
2061 }
2062 
2064  return unwrap<GlobalValue>(Global)->hasGlobalUnnamedAddr();
2065 }
2066 
2067 void LLVMSetUnnamedAddr(LLVMValueRef Global, LLVMBool HasUnnamedAddr) {
2068  unwrap<GlobalValue>(Global)->setUnnamedAddr(
2069  HasUnnamedAddr ? GlobalValue::UnnamedAddr::Global
2071 }
2072 
2074  return wrap(unwrap<GlobalValue>(Global)->getValueType());
2075 }
2076 
2077 /*--.. Operations on global variables, load and store instructions .........--*/
2078 
2080  Value *P = unwrap<Value>(V);
2081  if (GlobalObject *GV = dyn_cast<GlobalObject>(P))
2082  return GV->getAlign() ? GV->getAlign()->value() : 0;
2083  if (AllocaInst *AI = dyn_cast<AllocaInst>(P))
2084  return AI->getAlign().value();
2085  if (LoadInst *LI = dyn_cast<LoadInst>(P))
2086  return LI->getAlign().value();
2087  if (StoreInst *SI = dyn_cast<StoreInst>(P))
2088  return SI->getAlign().value();
2089  if (AtomicRMWInst *RMWI = dyn_cast<AtomicRMWInst>(P))
2090  return RMWI->getAlign().value();
2091  if (AtomicCmpXchgInst *CXI = dyn_cast<AtomicCmpXchgInst>(P))
2092  return CXI->getAlign().value();
2093 
2095  "only GlobalValue, AllocaInst, LoadInst, StoreInst, AtomicRMWInst, "
2096  "and AtomicCmpXchgInst have alignment");
2097 }
2098 
2099 void LLVMSetAlignment(LLVMValueRef V, unsigned Bytes) {
2100  Value *P = unwrap<Value>(V);
2101  if (GlobalObject *GV = dyn_cast<GlobalObject>(P))
2102  GV->setAlignment(MaybeAlign(Bytes));
2103  else if (AllocaInst *AI = dyn_cast<AllocaInst>(P))
2104  AI->setAlignment(Align(Bytes));
2105  else if (LoadInst *LI = dyn_cast<LoadInst>(P))
2106  LI->setAlignment(Align(Bytes));
2107  else if (StoreInst *SI = dyn_cast<StoreInst>(P))
2108  SI->setAlignment(Align(Bytes));
2109  else if (AtomicRMWInst *RMWI = dyn_cast<AtomicRMWInst>(P))
2110  RMWI->setAlignment(Align(Bytes));
2111  else if (AtomicCmpXchgInst *CXI = dyn_cast<AtomicCmpXchgInst>(P))
2112  CXI->setAlignment(Align(Bytes));
2113  else
2115  "only GlobalValue, AllocaInst, LoadInst, StoreInst, AtomicRMWInst, and "
2116  "and AtomicCmpXchgInst have alignment");
2117 }
2118 
2120  size_t *NumEntries) {
2121  return llvm_getMetadata(NumEntries, [&Value](MetadataEntries &Entries) {
2122  Entries.clear();
2123  if (Instruction *Instr = dyn_cast<Instruction>(unwrap(Value))) {
2124  Instr->getAllMetadata(Entries);
2125  } else {
2126  unwrap<GlobalObject>(Value)->getAllMetadata(Entries);
2127  }
2128  });
2129 }
2130 
2132  unsigned Index) {
2134  static_cast<LLVMOpaqueValueMetadataEntry>(Entries[Index]);
2135  return MVE.Kind;
2136 }
2137 
2140  unsigned Index) {
2142  static_cast<LLVMOpaqueValueMetadataEntry>(Entries[Index]);
2143  return MVE.Metadata;
2144 }
2145 
2147  free(Entries);
2148 }
2149 
2151  LLVMMetadataRef MD) {
2152  unwrap<GlobalObject>(Global)->setMetadata(Kind, unwrap<MDNode>(MD));
2153 }
2154 
2156  unwrap<GlobalObject>(Global)->eraseMetadata(Kind);
2157 }
2158 
2160  unwrap<GlobalObject>(Global)->clearMetadata();
2161 }
2162 
2163 /*--.. Operations on global variables ......................................--*/
2164 
2166  return wrap(new GlobalVariable(*unwrap(M), unwrap(Ty), false,
2167  GlobalValue::ExternalLinkage, nullptr, Name));
2168 }
2169 
2171  const char *Name,
2172  unsigned AddressSpace) {
2173  return wrap(new GlobalVariable(*unwrap(M), unwrap(Ty), false,
2176  AddressSpace));
2177 }
2178 
2180  return wrap(unwrap(M)->getNamedGlobal(Name));
2181 }
2182 
2184  Module *Mod = unwrap(M);
2186  if (I == Mod->global_end())
2187  return nullptr;
2188  return wrap(&*I);
2189 }
2190 
2192  Module *Mod = unwrap(M);
2194  if (I == Mod->global_begin())
2195  return nullptr;
2196  return wrap(&*--I);
2197 }
2198 
2200  GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
2202  if (++I == GV->getParent()->global_end())
2203  return nullptr;
2204  return wrap(&*I);
2205 }
2206 
2208  GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
2210  if (I == GV->getParent()->global_begin())
2211  return nullptr;
2212  return wrap(&*--I);
2213 }
2214 
2216  unwrap<GlobalVariable>(GlobalVar)->eraseFromParent();
2217 }
2218 
2220  GlobalVariable* GV = unwrap<GlobalVariable>(GlobalVar);
2221  if ( !GV->hasInitializer() )
2222  return nullptr;
2223  return wrap(GV->getInitializer());
2224 }
2225 
2227  unwrap<GlobalVariable>(GlobalVar)
2228  ->setInitializer(unwrap<Constant>(ConstantVal));
2229 }
2230 
2232  return unwrap<GlobalVariable>(GlobalVar)->isThreadLocal();
2233 }
2234 
2236  unwrap<GlobalVariable>(GlobalVar)->setThreadLocal(IsThreadLocal != 0);
2237 }
2238 
2240  return unwrap<GlobalVariable>(GlobalVar)->isConstant();
2241 }
2242 
2244  unwrap<GlobalVariable>(GlobalVar)->setConstant(IsConstant != 0);
2245 }
2246 
2248  switch (unwrap<GlobalVariable>(GlobalVar)->getThreadLocalMode()) {
2250  return LLVMNotThreadLocal;
2254  return LLVMLocalDynamicTLSModel;
2256  return LLVMInitialExecTLSModel;
2258  return LLVMLocalExecTLSModel;
2259  }
2260 
2261  llvm_unreachable("Invalid GlobalVariable thread local mode");
2262 }
2263 
2265  GlobalVariable *GV = unwrap<GlobalVariable>(GlobalVar);
2266 
2267  switch (Mode) {
2268  case LLVMNotThreadLocal:
2270  break;
2273  break;
2276  break;
2279  break;
2280  case LLVMLocalExecTLSModel:
2282  break;
2283  }
2284 }
2285 
2287  return unwrap<GlobalVariable>(GlobalVar)->isExternallyInitialized();
2288 }
2289 
2291  unwrap<GlobalVariable>(GlobalVar)->setExternallyInitialized(IsExtInit);
2292 }
2293 
2294 /*--.. Operations on aliases ......................................--*/
2295 
2297  const char *Name) {
2298  auto *PTy = cast<PointerType>(unwrap(Ty));
2299  return wrap(GlobalAlias::create(PTy->getNonOpaquePointerElementType(),
2300  PTy->getAddressSpace(),
2302  unwrap<Constant>(Aliasee), unwrap(M)));
2303 }
2304 
2306  unsigned AddrSpace, LLVMValueRef Aliasee,
2307  const char *Name) {
2308  return wrap(GlobalAlias::create(unwrap(ValueTy), AddrSpace,
2310  unwrap<Constant>(Aliasee), unwrap(M)));
2311 }
2312 
2314  const char *Name, size_t NameLen) {
2315  return wrap(unwrap(M)->getNamedAlias(StringRef(Name, NameLen)));
2316 }
2317 
2319  Module *Mod = unwrap(M);
2321  if (I == Mod->alias_end())
2322  return nullptr;
2323  return wrap(&*I);
2324 }
2325 
2327  Module *Mod = unwrap(M);
2329  if (I == Mod->alias_begin())
2330  return nullptr;
2331  return wrap(&*--I);
2332 }
2333 
2335  GlobalAlias *Alias = unwrap<GlobalAlias>(GA);
2336  Module::alias_iterator I(Alias);
2337  if (++I == Alias->getParent()->alias_end())
2338  return nullptr;
2339  return wrap(&*I);
2340 }
2341 
2343  GlobalAlias *Alias = unwrap<GlobalAlias>(GA);
2344  Module::alias_iterator I(Alias);
2345  if (I == Alias->getParent()->alias_begin())
2346  return nullptr;
2347  return wrap(&*--I);
2348 }
2349 
2351  return wrap(unwrap<GlobalAlias>(Alias)->getAliasee());
2352 }
2353 
2355  unwrap<GlobalAlias>(Alias)->setAliasee(unwrap<Constant>(Aliasee));
2356 }
2357 
2358 /*--.. Operations on functions .............................................--*/
2359 
2361  LLVMTypeRef FunctionTy) {
2362  return wrap(Function::Create(unwrap<FunctionType>(FunctionTy),
2364 }
2365 
2367  return wrap(unwrap(M)->getFunction(Name));
2368 }
2369 
2371  Module *Mod = unwrap(M);
2372  Module::iterator I = Mod->begin();
2373  if (I == Mod->end())
2374  return nullptr;
2375  return wrap(&*I);
2376 }
2377 
2379  Module *Mod = unwrap(M);
2380  Module::iterator I = Mod->end();
2381  if (I == Mod->begin())
2382  return nullptr;
2383  return wrap(&*--I);
2384 }
2385 
2387  Function *Func = unwrap<Function>(Fn);
2388  Module::iterator I(Func);
2389  if (++I == Func->getParent()->end())
2390  return nullptr;
2391  return wrap(&*I);
2392 }
2393 
2395  Function *Func = unwrap<Function>(Fn);
2396  Module::iterator I(Func);
2397  if (I == Func->getParent()->begin())
2398  return nullptr;
2399  return wrap(&*--I);
2400 }
2401 
2403  unwrap<Function>(Fn)->eraseFromParent();
2404 }
2405 
2407  return unwrap<Function>(Fn)->hasPersonalityFn();
2408 }
2409 
2411  return wrap(unwrap<Function>(Fn)->getPersonalityFn());
2412 }
2413 
2415  unwrap<Function>(Fn)->setPersonalityFn(unwrap<Constant>(PersonalityFn));
2416 }
2417 
2419  if (Function *F = dyn_cast<Function>(unwrap(Fn)))
2420  return F->getIntrinsicID();
2421  return 0;
2422 }
2423 
2425  assert(ID < llvm::Intrinsic::num_intrinsics && "Intrinsic ID out of range");
2426  return llvm::Intrinsic::ID(ID);
2427 }
2428 
2430  unsigned ID,
2431  LLVMTypeRef *ParamTypes,
2432  size_t ParamCount) {
2433  ArrayRef<Type*> Tys(unwrap(ParamTypes), ParamCount);
2434  auto IID = llvm_map_to_intrinsic_id(ID);
2435  return wrap(llvm::Intrinsic::getDeclaration(unwrap(Mod), IID, Tys));
2436 }
2437 
2438 const char *LLVMIntrinsicGetName(unsigned ID, size_t *NameLength) {
2439  auto IID = llvm_map_to_intrinsic_id(ID);
2440  auto Str = llvm::Intrinsic::getName(IID);
2441  *NameLength = Str.size();
2442  return Str.data();
2443 }
2444 
2446  LLVMTypeRef *ParamTypes, size_t ParamCount) {
2447  auto IID = llvm_map_to_intrinsic_id(ID);
2448  ArrayRef<Type*> Tys(unwrap(ParamTypes), ParamCount);
2449  return wrap(llvm::Intrinsic::getType(*unwrap(Ctx), IID, Tys));
2450 }
2451 
2453  LLVMTypeRef *ParamTypes,
2454  size_t ParamCount,
2455  size_t *NameLength) {
2456  auto IID = llvm_map_to_intrinsic_id(ID);
2457  ArrayRef<Type*> Tys(unwrap(ParamTypes), ParamCount);
2458  auto Str = llvm::Intrinsic::getNameNoUnnamedTypes(IID, Tys);
2459  *NameLength = Str.length();
2460  return strdup(Str.c_str());
2461 }
2462 
2464  LLVMTypeRef *ParamTypes,
2465  size_t ParamCount,
2466  size_t *NameLength) {
2467  auto IID = llvm_map_to_intrinsic_id(ID);
2468  ArrayRef<Type *> Tys(unwrap(ParamTypes), ParamCount);
2469  auto Str = llvm::Intrinsic::getName(IID, Tys, unwrap(Mod));
2470  *NameLength = Str.length();
2471  return strdup(Str.c_str());
2472 }
2473 
2474 unsigned LLVMLookupIntrinsicID(const char *Name, size_t NameLen) {
2475  return Function::lookupIntrinsicID({Name, NameLen});
2476 }
2477 
2479  auto IID = llvm_map_to_intrinsic_id(ID);
2480  return llvm::Intrinsic::isOverloaded(IID);
2481 }
2482 
2484  return unwrap<Function>(Fn)->getCallingConv();
2485 }
2486 
2487 void LLVMSetFunctionCallConv(LLVMValueRef Fn, unsigned CC) {
2488  return unwrap<Function>(Fn)->setCallingConv(
2489  static_cast<CallingConv::ID>(CC));
2490 }
2491 
2492 const char *LLVMGetGC(LLVMValueRef Fn) {
2493  Function *F = unwrap<Function>(Fn);
2494  return F->hasGC()? F->getGC().c_str() : nullptr;
2495 }
2496 
2497 void LLVMSetGC(LLVMValueRef Fn, const char *GC) {
2498  Function *F = unwrap<Function>(Fn);
2499  if (GC)
2500  F->setGC(GC);
2501  else
2502  F->clearGC();
2503 }
2504 
2506  LLVMAttributeRef A) {
2507  unwrap<Function>(F)->addAttributeAtIndex(Idx, unwrap(A));
2508 }
2509 
2511  auto AS = unwrap<Function>(F)->getAttributes().getAttributes(Idx);
2512  return AS.getNumAttributes();
2513 }
2514 
2517  auto AS = unwrap<Function>(F)->getAttributes().getAttributes(Idx);
2518  for (auto A : AS)
2519  *Attrs++ = wrap(A);
2520 }
2521 
2523  LLVMAttributeIndex Idx,
2524  unsigned KindID) {
2525  return wrap(unwrap<Function>(F)->getAttributeAtIndex(
2526  Idx, (Attribute::AttrKind)KindID));
2527 }
2528 
2530  LLVMAttributeIndex Idx,
2531  const char *K, unsigned KLen) {
2532  return wrap(
2533  unwrap<Function>(F)->getAttributeAtIndex(Idx, StringRef(K, KLen)));
2534 }
2535 
2537  unsigned KindID) {
2538  unwrap<Function>(F)->removeAttributeAtIndex(Idx, (Attribute::AttrKind)KindID);
2539 }
2540 
2542  const char *K, unsigned KLen) {
2543  unwrap<Function>(F)->removeAttributeAtIndex(Idx, StringRef(K, KLen));
2544 }
2545 
2547  const char *V) {
2548  Function *Func = unwrap<Function>(Fn);
2549  Attribute Attr = Attribute::get(Func->getContext(), A, V);
2550  Func->addFnAttr(Attr);
2551 }
2552 
2553 /*--.. Operations on parameters ............................................--*/
2554 
2556  // This function is strictly redundant to
2557  // LLVMCountParamTypes(LLVMGetElementType(LLVMTypeOf(FnRef)))
2558  return unwrap<Function>(FnRef)->arg_size();
2559 }
2560 
2561 void LLVMGetParams(LLVMValueRef FnRef, LLVMValueRef *ParamRefs) {
2562  Function *Fn = unwrap<Function>(FnRef);
2563  for (Argument &A : Fn->args())
2564  *ParamRefs++ = wrap(&A);
2565 }
2566 
2568  Function *Fn = unwrap<Function>(FnRef);
2569  return wrap(&Fn->arg_begin()[index]);
2570 }
2571 
2573  return wrap(unwrap<Argument>(V)->getParent());
2574 }
2575 
2577  Function *Func = unwrap<Function>(Fn);
2578  Function::arg_iterator I = Func->arg_begin();
2579  if (I == Func->arg_end())
2580  return nullptr;
2581  return wrap(&*I);
2582 }
2583 
2585  Function *Func = unwrap<Function>(Fn);
2586  Function::arg_iterator I = Func->arg_end();
2587  if (I == Func->arg_begin())
2588  return nullptr;
2589  return wrap(&*--I);
2590 }
2591 
2593  Argument *A = unwrap<Argument>(Arg);
2594  Function *Fn = A->getParent();
2595  if (A->getArgNo() + 1 >= Fn->arg_size())
2596  return nullptr;
2597  return wrap(&Fn->arg_begin()[A->getArgNo() + 1]);
2598 }
2599 
2601  Argument *A = unwrap<Argument>(Arg);
2602  if (A->getArgNo() == 0)
2603  return nullptr;
2604  return wrap(&A->getParent()->arg_begin()[A->getArgNo() - 1]);
2605 }
2606 
2608  Argument *A = unwrap<Argument>(Arg);
2609  A->addAttr(Attribute::getWithAlignment(A->getContext(), Align(align)));
2610 }
2611 
2612 /*--.. Operations on ifuncs ................................................--*/
2613 
2615  const char *Name, size_t NameLen,
2616  LLVMTypeRef Ty, unsigned AddrSpace,
2618  return wrap(GlobalIFunc::create(unwrap(Ty), AddrSpace,
2620  StringRef(Name, NameLen),
2621  unwrap<Constant>(Resolver), unwrap(M)));
2622 }
2623 
2625  const char *Name, size_t NameLen) {
2626  return wrap(unwrap(M)->getNamedIFunc(StringRef(Name, NameLen)));
2627 }
2628 
2630  Module *Mod = unwrap(M);
2632  if (I == Mod->ifunc_end())
2633  return nullptr;
2634  return wrap(&*I);
2635 }
2636 
2638  Module *Mod = unwrap(M);
2640  if (I == Mod->ifunc_begin())
2641  return nullptr;
2642  return wrap(&*--I);
2643 }
2644 
2646  GlobalIFunc *GIF = unwrap<GlobalIFunc>(IFunc);
2648  if (++I == GIF->getParent()->ifunc_end())
2649  return nullptr;
2650  return wrap(&*I);
2651 }
2652 
2654  GlobalIFunc *GIF = unwrap<GlobalIFunc>(IFunc);
2656  if (I == GIF->getParent()->ifunc_begin())
2657  return nullptr;
2658  return wrap(&*--I);
2659 }
2660 
2662  return wrap(unwrap<GlobalIFunc>(IFunc)->getResolver());
2663 }
2664 
2666  unwrap<GlobalIFunc>(IFunc)->setResolver(unwrap<Constant>(Resolver));
2667 }
2668 
2670  unwrap<GlobalIFunc>(IFunc)->eraseFromParent();
2671 }
2672 
2674  unwrap<GlobalIFunc>(IFunc)->removeFromParent();
2675 }
2676 
2677 /*--.. Operations on basic blocks ..........................................--*/
2678 
2680  return wrap(static_cast<Value*>(unwrap(BB)));
2681 }
2682 
2684  return isa<BasicBlock>(unwrap(Val));
2685 }
2686 
2688  return wrap(unwrap<BasicBlock>(Val));
2689 }
2690 
2692  return unwrap(BB)->getName().data();
2693 }
2694 
2696  return wrap(unwrap(BB)->getParent());
2697 }
2698 
2700  return wrap(unwrap(BB)->getTerminator());
2701 }
2702 
2704  return unwrap<Function>(FnRef)->size();
2705 }
2706 
2707 void LLVMGetBasicBlocks(LLVMValueRef FnRef, LLVMBasicBlockRef *BasicBlocksRefs){
2708  Function *Fn = unwrap<Function>(FnRef);
2709  for (BasicBlock &BB : *Fn)
2710  *BasicBlocksRefs++ = wrap(&BB);
2711 }
2712 
2714  return wrap(&unwrap<Function>(Fn)->getEntryBlock());
2715 }
2716 
2718  Function *Func = unwrap<Function>(Fn);
2719  Function::iterator I = Func->begin();
2720  if (I == Func->end())
2721  return nullptr;
2722  return wrap(&*I);
2723 }
2724 
2726  Function *Func = unwrap<Function>(Fn);
2727  Function::iterator I = Func->end();
2728  if (I == Func->begin())
2729  return nullptr;
2730  return wrap(&*--I);
2731 }
2732 
2734  BasicBlock *Block = unwrap(BB);
2735  Function::iterator I(Block);
2736  if (++I == Block->getParent()->end())
2737  return nullptr;
2738  return wrap(&*I);
2739 }
2740 
2742  BasicBlock *Block = unwrap(BB);
2743  Function::iterator I(Block);
2744  if (I == Block->getParent()->begin())
2745  return nullptr;
2746  return wrap(&*--I);
2747 }
2748 
2750  const char *Name) {
2752 }
2753 
2756  BasicBlock *ToInsert = unwrap(BB);
2757  BasicBlock *CurBB = unwrap(Builder)->GetInsertBlock();
2758  assert(CurBB && "current insertion point is invalid!");
2759  CurBB->getParent()->getBasicBlockList().insertAfter(CurBB->getIterator(),
2760  ToInsert);
2761 }
2762 
2765  unwrap<Function>(Fn)->getBasicBlockList().push_back(unwrap(BB));
2766 }
2767 
2769  LLVMValueRef FnRef,
2770  const char *Name) {
2771  return wrap(BasicBlock::Create(*unwrap(C), Name, unwrap<Function>(FnRef)));
2772 }
2773 
2776 }
2777 
2779  LLVMBasicBlockRef BBRef,
2780  const char *Name) {
2781  BasicBlock *BB = unwrap(BBRef);
2782  return wrap(BasicBlock::Create(*unwrap(C), Name, BB->getParent(), BB));
2783 }
2784 
2786  const char *Name) {
2788 }
2789 
2791  unwrap(BBRef)->eraseFromParent();
2792 }
2793 
2795  unwrap(BBRef)->removeFromParent();
2796 }
2797 
2799  unwrap(BB)->moveBefore(unwrap(MovePos));
2800 }
2801 
2803  unwrap(BB)->moveAfter(unwrap(MovePos));
2804 }
2805 
2806 /*--.. Operations on instructions ..........................................--*/
2807 
2809  return wrap(unwrap<Instruction>(Inst)->getParent());
2810 }
2811 
2813  BasicBlock *Block = unwrap(BB);
2814  BasicBlock::iterator I = Block->begin();
2815  if (I == Block->end())
2816  return nullptr;
2817  return wrap(&*I);
2818 }
2819 
2821  BasicBlock *Block = unwrap(BB);
2822  BasicBlock::iterator I = Block->end();
2823  if (I == Block->begin())
2824  return nullptr;
2825  return wrap(&*--I);
2826 }
2827 
2829  Instruction *Instr = unwrap<Instruction>(Inst);
2830  BasicBlock::iterator I(Instr);
2831  if (++I == Instr->getParent()->end())
2832  return nullptr;
2833  return wrap(&*I);
2834 }
2835 
2837  Instruction *Instr = unwrap<Instruction>(Inst);
2838  BasicBlock::iterator I(Instr);
2839  if (I == Instr->getParent()->begin())
2840  return nullptr;
2841  return wrap(&*--I);
2842 }
2843 
2845  unwrap<Instruction>(Inst)->removeFromParent();
2846 }
2847 
2849  unwrap<Instruction>(Inst)->eraseFromParent();
2850 }
2851 
2853  if (ICmpInst *I = dyn_cast<ICmpInst>(unwrap(Inst)))
2854  return (LLVMIntPredicate)I->getPredicate();
2855  if (ConstantExpr *CE = dyn_cast<ConstantExpr>(unwrap(Inst)))
2856  if (CE->getOpcode() == Instruction::ICmp)
2857  return (LLVMIntPredicate)CE->getPredicate();
2858  return (LLVMIntPredicate)0;
2859 }
2860 
2862  if (FCmpInst *I = dyn_cast<FCmpInst>(unwrap(Inst)))
2863  return (LLVMRealPredicate)I->getPredicate();
2864  if (ConstantExpr *CE = dyn_cast<ConstantExpr>(unwrap(Inst)))
2865  if (CE->getOpcode() == Instruction::FCmp)
2866  return (LLVMRealPredicate)CE->getPredicate();
2867  return (LLVMRealPredicate)0;
2868 }
2869 
2871  if (Instruction *C = dyn_cast<Instruction>(unwrap(Inst)))
2872  return map_to_llvmopcode(C->getOpcode());
2873  return (LLVMOpcode)0;
2874 }
2875 
2877  if (Instruction *C = dyn_cast<Instruction>(unwrap(Inst)))
2878  return wrap(C->clone());
2879  return nullptr;
2880 }
2881 
2883  Instruction *I = dyn_cast<Instruction>(unwrap(Inst));
2884  return (I && I->isTerminator()) ? wrap(I) : nullptr;
2885 }
2886 
2888  if (FuncletPadInst *FPI = dyn_cast<FuncletPadInst>(unwrap(Instr))) {
2889  return FPI->getNumArgOperands();
2890  }
2891  return unwrap<CallBase>(Instr)->arg_size();
2892 }
2893 
2894 /*--.. Call and invoke instructions ........................................--*/
2895 
2897  return unwrap<CallBase>(Instr)->getCallingConv();
2898 }
2899 
2900 void LLVMSetInstructionCallConv(LLVMValueRef Instr, unsigned CC) {
2901  return unwrap<CallBase>(Instr)->setCallingConv(
2902  static_cast<CallingConv::ID>(CC));
2903 }
2904 
2906  unsigned align) {
2907  auto *Call = unwrap<CallBase>(Instr);
2908  Attribute AlignAttr =
2909  Attribute::getWithAlignment(Call->getContext(), Align(align));
2910  Call->addAttributeAtIndex(Idx, AlignAttr);
2911 }
2912 
2914  LLVMAttributeRef A) {
2915  unwrap<CallBase>(C)->addAttributeAtIndex(Idx, unwrap(A));
2916 }
2917 
2919  LLVMAttributeIndex Idx) {
2920  auto *Call = unwrap<CallBase>(C);
2921  auto AS = Call->getAttributes().getAttributes(Idx);
2922  return AS.getNumAttributes();
2923 }
2924 
2927  auto *Call = unwrap<CallBase>(C);
2928  auto AS = Call->getAttributes().getAttributes(Idx);
2929  for (auto A : AS)
2930  *Attrs++ = wrap(A);
2931 }
2932 
2934  LLVMAttributeIndex Idx,
2935  unsigned KindID) {
2936  return wrap(unwrap<CallBase>(C)->getAttributeAtIndex(
2937  Idx, (Attribute::AttrKind)KindID));
2938 }
2939 
2941  LLVMAttributeIndex Idx,
2942  const char *K, unsigned KLen) {
2943  return wrap(
2944  unwrap<CallBase>(C)->getAttributeAtIndex(Idx, StringRef(K, KLen)));
2945 }
2946 
2948  unsigned KindID) {
2949  unwrap<CallBase>(C)->removeAttributeAtIndex(Idx, (Attribute::AttrKind)KindID);
2950 }
2951 
2953  const char *K, unsigned KLen) {
2954  unwrap<CallBase>(C)->removeAttributeAtIndex(Idx, StringRef(K, KLen));
2955 }
2956 
2958  return wrap(unwrap<CallBase>(Instr)->getCalledOperand());
2959 }
2960 
2962  return wrap(unwrap<CallBase>(Instr)->getFunctionType());
2963 }
2964 
2965 /*--.. Operations on call instructions (only) ..............................--*/
2966 
2968  return unwrap<CallInst>(Call)->isTailCall();
2969 }
2970 
2971 void LLVMSetTailCall(LLVMValueRef Call, LLVMBool isTailCall) {
2972  unwrap<CallInst>(Call)->setTailCall(isTailCall);
2973 }
2974 
2975 /*--.. Operations on invoke instructions (only) ............................--*/
2976 
2978  return wrap(unwrap<InvokeInst>(Invoke)->getNormalDest());
2979 }
2980 
2982  if (CleanupReturnInst *CRI = dyn_cast<CleanupReturnInst>(unwrap(Invoke))) {
2983  return wrap(CRI->getUnwindDest());
2984  } else if (CatchSwitchInst *CSI = dyn_cast<CatchSwitchInst>(unwrap(Invoke))) {
2985  return wrap(CSI->getUnwindDest());
2986  }
2987  return wrap(unwrap<InvokeInst>(Invoke)->getUnwindDest());
2988 }
2989 
2991  unwrap<InvokeInst>(Invoke)->setNormalDest(unwrap(B));
2992 }
2993 
2995  if (CleanupReturnInst *CRI = dyn_cast<CleanupReturnInst>(unwrap(Invoke))) {
2996  return CRI->setUnwindDest(unwrap(B));
2997  } else if (CatchSwitchInst *CSI = dyn_cast<CatchSwitchInst>(unwrap(Invoke))) {
2998  return CSI->setUnwindDest(unwrap(B));
2999  }
3000  unwrap<InvokeInst>(Invoke)->setUnwindDest(unwrap(B));
3001 }
3002 
3003 /*--.. Operations on terminators ...........................................--*/
3004 
3006  return unwrap<Instruction>(Term)->getNumSuccessors();
3007 }
3008 
3010  return wrap(unwrap<Instruction>(Term)->getSuccessor(i));
3011 }
3012 
3014  return unwrap<Instruction>(Term)->setSuccessor(i, unwrap(block));
3015 }
3016 
3017 /*--.. Operations on branch instructions (only) ............................--*/
3018 
3020  return unwrap<BranchInst>(Branch)->isConditional();
3021 }
3022 
3024  return wrap(unwrap<BranchInst>(Branch)->getCondition());
3025 }
3026 
3028  return unwrap<BranchInst>(Branch)->setCondition(unwrap(Cond));
3029 }
3030 
3031 /*--.. Operations on switch instructions (only) ............................--*/
3032 
3034  return wrap(unwrap<SwitchInst>(Switch)->getDefaultDest());
3035 }
3036 
3037 /*--.. Operations on alloca instructions (only) ............................--*/
3038 
3040  return wrap(unwrap<AllocaInst>(Alloca)->getAllocatedType());
3041 }
3042 
3043 /*--.. Operations on gep instructions (only) ...............................--*/
3044 
3046  return unwrap<GEPOperator>(GEP)->isInBounds();
3047 }
3048 
3050  return unwrap<GetElementPtrInst>(GEP)->setIsInBounds(InBounds);
3051 }
3052 
3054  return wrap(unwrap<GEPOperator>(GEP)->getSourceElementType());
3055 }
3056 
3057 /*--.. Operations on phi nodes .............................................--*/
3058 
3059 void LLVMAddIncoming(LLVMValueRef PhiNode, LLVMValueRef *IncomingValues,
3060  LLVMBasicBlockRef *IncomingBlocks, unsigned Count) {
3061  PHINode *PhiVal = unwrap<PHINode>(PhiNode);
3062  for (unsigned I = 0; I != Count; ++I)
3063  PhiVal->addIncoming(unwrap(IncomingValues[I]), unwrap(IncomingBlocks[I]));
3064 }
3065 
3066 unsigned LLVMCountIncoming(LLVMValueRef PhiNode) {
3067  return unwrap<PHINode>(PhiNode)->getNumIncomingValues();
3068 }
3069 
3071  return wrap(unwrap<PHINode>(PhiNode)->getIncomingValue(Index));
3072 }
3073 
3075  return wrap(unwrap<PHINode>(PhiNode)->getIncomingBlock(Index));
3076 }
3077 
3078 /*--.. Operations on extractvalue and insertvalue nodes ....................--*/
3079 
3081  auto *I = unwrap(Inst);
3082  if (auto *GEP = dyn_cast<GEPOperator>(I))
3083  return GEP->getNumIndices();
3084  if (auto *EV = dyn_cast<ExtractValueInst>(I))
3085  return EV->getNumIndices();
3086  if (auto *IV = dyn_cast<InsertValueInst>(I))
3087  return IV->getNumIndices();
3088  if (auto *CE = dyn_cast<ConstantExpr>(I))
3089  return CE->getIndices().size();
3091  "LLVMGetNumIndices applies only to extractvalue and insertvalue!");
3092 }
3093 
3094 const unsigned *LLVMGetIndices(LLVMValueRef Inst) {
3095  auto *I = unwrap(Inst);
3096  if (auto *EV = dyn_cast<ExtractValueInst>(I))
3097  return EV->getIndices().data();
3098  if (auto *IV = dyn_cast<InsertValueInst>(I))
3099  return IV->getIndices().data();
3100  if (auto *CE = dyn_cast<ConstantExpr>(I))
3101  return CE->getIndices().data();
3103  "LLVMGetIndices applies only to extractvalue and insertvalue!");
3104 }
3105 
3106 
3107 /*===-- Instruction builders ----------------------------------------------===*/
3108 
3110  return wrap(new IRBuilder<>(*unwrap(C)));
3111 }
3112 
3115 }
3116 
3118  LLVMValueRef Instr) {
3119  BasicBlock *BB = unwrap(Block);
3120  auto I = Instr ? unwrap<Instruction>(Instr)->getIterator() : BB->end();
3121  unwrap(Builder)->SetInsertPoint(BB, I);
3122 }
3123 
3125  Instruction *I = unwrap<Instruction>(Instr);
3126  unwrap(Builder)->SetInsertPoint(I->getParent(), I->getIterator());
3127 }
3128 
3130  BasicBlock *BB = unwrap(Block);
3131  unwrap(Builder)->SetInsertPoint(BB);
3132 }
3133 
3135  return wrap(unwrap(Builder)->GetInsertBlock());
3136 }
3137 
3139  unwrap(Builder)->ClearInsertionPoint();
3140 }
3141 
3143  unwrap(Builder)->Insert(unwrap<Instruction>(Instr));
3144 }
3145 
3147  const char *Name) {
3148  unwrap(Builder)->Insert(unwrap<Instruction>(Instr), Name);
3149 }
3150 
3152  delete unwrap(Builder);
3153 }
3154 
3155 /*--.. Metadata builders ...................................................--*/
3156 
3158  return wrap(unwrap(Builder)->getCurrentDebugLocation().getAsMDNode());
3159 }
3160 
3162  if (Loc)
3163  unwrap(Builder)->SetCurrentDebugLocation(DebugLoc(unwrap<MDNode>(Loc)));
3164  else
3165  unwrap(Builder)->SetCurrentDebugLocation(DebugLoc());
3166 }
3167 
3169  MDNode *Loc =
3170  L ? cast<MDNode>(unwrap<MetadataAsValue>(L)->getMetadata()) : nullptr;
3171  unwrap(Builder)->SetCurrentDebugLocation(DebugLoc(Loc));
3172 }
3173 
3175  LLVMContext &Context = unwrap(Builder)->getContext();
3176  return wrap(MetadataAsValue::get(
3177  Context, unwrap(Builder)->getCurrentDebugLocation().getAsMDNode()));
3178 }
3179 
3181  unwrap(Builder)->SetInstDebugLocation(unwrap<Instruction>(Inst));
3182 }
3183 
3185  unwrap(Builder)->AddMetadataToInst(unwrap<Instruction>(Inst));
3186 }
3187 
3189  LLVMMetadataRef FPMathTag) {
3190 
3191  unwrap(Builder)->setDefaultFPMathTag(FPMathTag
3192  ? unwrap<MDNode>(FPMathTag)
3193  : nullptr);
3194 }
3195 
3197  return wrap(unwrap(Builder)->getDefaultFPMathTag());
3198 }
3199 
3200 /*--.. Instruction builders ................................................--*/
3201 
3203  return wrap(unwrap(B)->CreateRetVoid());
3204 }
3205 
3207  return wrap(unwrap(B)->CreateRet(unwrap(V)));
3208 }
3209 
3211  unsigned N) {
3212  return wrap(unwrap(B)->CreateAggregateRet(unwrap(RetVals), N));
3213 }
3214 
3216  return wrap(unwrap(B)->CreateBr(unwrap(Dest)));
3217 }
3218 
3221  return wrap(unwrap(B)->CreateCondBr(unwrap(If), unwrap(Then), unwrap(Else)));
3222 }
3223 
3225  LLVMBasicBlockRef Else, unsigned NumCases) {
3226  return wrap(unwrap(B)->CreateSwitch(unwrap(V), unwrap(Else), NumCases));
3227 }
3228 
3230  unsigned NumDests) {
3231  return wrap(unwrap(B)->CreateIndirectBr(unwrap(Addr), NumDests));
3232 }
3233 
3235  LLVMValueRef *Args, unsigned NumArgs,
3237  const char *Name) {
3238  Value *V = unwrap(Fn);
3239  FunctionType *FnT =
3240  cast<FunctionType>(V->getType()->getNonOpaquePointerElementType());
3241 
3242  return wrap(
3243  unwrap(B)->CreateInvoke(FnT, unwrap(Fn), unwrap(Then), unwrap(Catch),
3244  makeArrayRef(unwrap(Args), NumArgs), Name));
3245 }
3246 
3248  LLVMValueRef *Args, unsigned NumArgs,
3250  const char *Name) {
3251  return wrap(unwrap(B)->CreateInvoke(
3252  unwrap<FunctionType>(Ty), unwrap(Fn), unwrap(Then), unwrap(Catch),
3253  makeArrayRef(unwrap(Args), NumArgs), Name));
3254 }
3255 
3257  LLVMValueRef PersFn, unsigned NumClauses,
3258  const char *Name) {
3259  // The personality used to live on the landingpad instruction, but now it
3260  // lives on the parent function. For compatibility, take the provided
3261  // personality and put it on the parent function.
3262  if (PersFn)
3263  unwrap(B)->GetInsertBlock()->getParent()->setPersonalityFn(
3264  cast<Function>(unwrap(PersFn)));
3265  return wrap(unwrap(B)->CreateLandingPad(unwrap(Ty), NumClauses, Name));
3266 }
3267 
3269  LLVMValueRef *Args, unsigned NumArgs,
3270  const char *Name) {
3271  return wrap(unwrap(B)->CreateCatchPad(unwrap(ParentPad),
3272  makeArrayRef(unwrap(Args), NumArgs),
3273  Name));
3274 }
3275 
3277  LLVMValueRef *Args, unsigned NumArgs,
3278  const char *Name) {
3279  if (ParentPad == nullptr) {
3280  Type *Ty = Type::getTokenTy(unwrap(B)->getContext());
3281  ParentPad = wrap(Constant::getNullValue(Ty));
3282  }
3283  return wrap(unwrap(B)->CreateCleanupPad(unwrap(ParentPad),
3284  makeArrayRef(unwrap(Args), NumArgs),
3285  Name));
3286 }
3287 
3289  return wrap(unwrap(B)->CreateResume(unwrap(Exn)));
3290 }
3291 
3293  LLVMBasicBlockRef UnwindBB,
3294  unsigned NumHandlers, const char *Name) {
3295  if (ParentPad == nullptr) {
3296  Type *Ty = Type::getTokenTy(unwrap(B)->getContext());
3297  ParentPad = wrap(Constant::getNullValue(Ty));
3298  }
3299  return wrap(unwrap(B)->CreateCatchSwitch(unwrap(ParentPad), unwrap(UnwindBB),
3300  NumHandlers, Name));
3301 }
3302 
3305  return wrap(unwrap(B)->CreateCatchRet(unwrap<CatchPadInst>(CatchPad),
3306  unwrap(BB)));
3307 }
3308 
3311  return wrap(unwrap(B)->CreateCleanupRet(unwrap<CleanupPadInst>(CatchPad),
3312  unwrap(BB)));
3313 }
3314 
3316  return wrap(unwrap(B)->CreateUnreachable());
3317 }
3318 
3320  LLVMBasicBlockRef Dest) {
3321  unwrap<SwitchInst>(Switch)->addCase(unwrap<ConstantInt>(OnVal), unwrap(Dest));
3322 }
3323 
3325  unwrap<IndirectBrInst>(IndirectBr)->addDestination(unwrap(Dest));
3326 }
3327 
3328 unsigned LLVMGetNumClauses(LLVMValueRef LandingPad) {
3329  return unwrap<LandingPadInst>(LandingPad)->getNumClauses();
3330 }
3331 
3332 LLVMValueRef LLVMGetClause(LLVMValueRef LandingPad, unsigned Idx) {
3333  return wrap(unwrap<LandingPadInst>(LandingPad)->getClause(Idx));
3334 }
3335 
3337  unwrap<LandingPadInst>(LandingPad)->
3338  addClause(cast<Constant>(unwrap(ClauseVal)));
3339 }
3340 
3342  return unwrap<LandingPadInst>(LandingPad)->isCleanup();
3343 }
3344 
3345 void LLVMSetCleanup(LLVMValueRef LandingPad, LLVMBool Val) {
3346  unwrap<LandingPadInst>(LandingPad)->setCleanup(Val);
3347 }
3348 
3350  unwrap<CatchSwitchInst>(CatchSwitch)->addHandler(unwrap(Dest));
3351 }
3352 
3353 unsigned LLVMGetNumHandlers(LLVMValueRef CatchSwitch) {
3354  return unwrap<CatchSwitchInst>(CatchSwitch)->getNumHandlers();
3355 }
3356 
3357 void LLVMGetHandlers(LLVMValueRef CatchSwitch, LLVMBasicBlockRef *Handlers) {
3358  CatchSwitchInst *CSI = unwrap<CatchSwitchInst>(CatchSwitch);
3359  for (const BasicBlock *H : CSI->handlers())
3360  *Handlers++ = wrap(H);
3361 }
3362 
3364  return wrap(unwrap<CatchPadInst>(CatchPad)->getCatchSwitch());
3365 }
3366 
3368  unwrap<CatchPadInst>(CatchPad)
3369  ->setCatchSwitch(unwrap<CatchSwitchInst>(CatchSwitch));
3370 }
3371 
3372 /*--.. Funclets ...........................................................--*/
3373 
3375  return wrap(unwrap<FuncletPadInst>(Funclet)->getArgOperand(i));
3376 }
3377 
3378 void LLVMSetArgOperand(LLVMValueRef Funclet, unsigned i, LLVMValueRef value) {
3379  unwrap<FuncletPadInst>(Funclet)->setArgOperand(i, unwrap(value));
3380 }
3381 
3382 /*--.. Arithmetic ..........................................................--*/
3383 
3385  const char *Name) {
3386  return wrap(unwrap(B)->CreateAdd(unwrap(LHS), unwrap(RHS), Name));
3387 }
3388 
3390  const char *Name) {
3391  return wrap(unwrap(B)->CreateNSWAdd(unwrap(LHS), unwrap(RHS), Name));
3392 }
3393 
3395  const char *Name) {
3396  return wrap(unwrap(B)->CreateNUWAdd(unwrap(LHS), unwrap(RHS), Name));
3397 }
3398 
3400  const char *Name) {
3401  return wrap(unwrap(B)->CreateFAdd(unwrap(LHS), unwrap(RHS), Name));
3402 }
3403 
3405  const char *Name) {
3406  return wrap(unwrap(B)->CreateSub(unwrap(LHS), unwrap(RHS), Name));
3407 }
3408 
3410  const char *Name) {
3411  return wrap(unwrap(B)->CreateNSWSub(unwrap(LHS), unwrap(RHS), Name));
3412 }
3413 
3415  const char *Name) {
3416  return wrap(unwrap(B)->CreateNUWSub(unwrap(LHS), unwrap(RHS), Name));
3417 }
3418 
3420  const char *Name) {
3421  return wrap(unwrap(B)->CreateFSub(unwrap(LHS), unwrap(RHS), Name));
3422 }
3423 
3425  const char *Name) {
3426  return wrap(unwrap(B)->CreateMul(unwrap(LHS), unwrap(RHS), Name));
3427 }
3428 
3430  const char *Name) {
3431  return wrap(unwrap(B)->CreateNSWMul(unwrap(LHS), unwrap(RHS), Name));
3432 }
3433 
3435  const char *Name) {
3436  return wrap(unwrap(B)->CreateNUWMul(unwrap(LHS), unwrap(RHS), Name));
3437 }
3438 
3440  const char *Name) {
3441  return wrap(unwrap(B)->CreateFMul(unwrap(LHS), unwrap(RHS), Name));
3442 }
3443 
3445  const char *Name) {
3446  return wrap(unwrap(B)->CreateUDiv(unwrap(LHS), unwrap(RHS), Name));
3447 }
3448 
3450  LLVMValueRef RHS, const char *Name) {
3451  return wrap(unwrap(B)->CreateExactUDiv(unwrap(LHS), unwrap(RHS), Name));
3452 }
3453 
3455  const char *Name) {
3456  return wrap(unwrap(B)->CreateSDiv(unwrap(LHS), unwrap(RHS), Name));
3457 }
3458 
3460  LLVMValueRef RHS, const char *Name) {
3461  return wrap(unwrap(B)->CreateExactSDiv(unwrap(LHS), unwrap(RHS), Name));
3462 }
3463 
3465  const char *Name) {
3466  return wrap(unwrap(B)->CreateFDiv(unwrap(LHS), unwrap(RHS), Name));
3467 }
3468 
3470  const char *Name) {
3471  return wrap(unwrap(B)->CreateURem(unwrap(LHS), unwrap(RHS), Name));
3472 }
3473 
3475  const char *Name) {
3476  return wrap(unwrap(B)->CreateSRem(unwrap(LHS), unwrap(RHS), Name));
3477 }
3478 
3480  const char *Name) {
3481  return wrap(unwrap(B)->CreateFRem(unwrap(LHS), unwrap(RHS), Name));
3482 }
3483 
3485  const char *Name) {
3486  return wrap(unwrap(B)->CreateShl(unwrap(LHS), unwrap(RHS), Name));
3487 }
3488 
3490  const char *Name) {
3491  return wrap(unwrap(B)->CreateLShr(unwrap(LHS), unwrap(RHS), Name));
3492 }
3493 
3495  const char *Name) {
3496  return wrap(unwrap(B)->CreateAShr(unwrap(LHS), unwrap(RHS), Name));
3497 }
3498 
3500  const char *Name) {
3501  return wrap(unwrap(B)->CreateAnd(unwrap(LHS), unwrap(RHS), Name));
3502 }
3503 
3505  const char *Name) {
3506  return wrap(unwrap(B)->CreateOr(unwrap(LHS), unwrap(RHS), Name));
3507 }
3508 
3510  const char *Name) {
3511  return wrap(unwrap(B)->CreateXor(unwrap(LHS), unwrap(RHS), Name));
3512 }
3513 
3516  const char *Name) {
3518  unwrap(RHS), Name));
3519 }
3520 
3522  return wrap(unwrap(B)->CreateNeg(unwrap(V), Name));
3523 }
3524 
3526  const char *Name) {
3527  return wrap(unwrap(B)->CreateNSWNeg(unwrap(V), Name));
3528 }
3529 
3531  const char *Name) {
3532  return wrap(unwrap(B)->CreateNUWNeg(unwrap(V), Name));
3533 }
3534 
3536  return wrap(unwrap(B)->CreateFNeg(unwrap(V), Name));
3537 }
3538 
3540  return wrap(unwrap(B)->CreateNot(unwrap(V), Name));
3541 }
3542 
3543 /*--.. Memory ..............................................................--*/
3544 
3546  const char *Name) {
3547  Type* ITy = Type::getInt32Ty(unwrap(B)->GetInsertBlock()->getContext());
3548  Constant* AllocSize = ConstantExpr::getSizeOf(unwrap(Ty));
3549  AllocSize = ConstantExpr::getTruncOrBitCast(AllocSize, ITy);
3550  Instruction* Malloc = CallInst::CreateMalloc(unwrap(B)->GetInsertBlock(),
3551  ITy, unwrap(Ty), AllocSize,
3552  nullptr, nullptr, "");
3553  return wrap(unwrap(B)->Insert(Malloc, Twine(Name)));
3554 }
3555 
3557  LLVMValueRef Val, const char *Name) {
3558  Type* ITy = Type::getInt32Ty(unwrap(B)->GetInsertBlock()->getContext());
3559  Constant* AllocSize = ConstantExpr::getSizeOf(unwrap(Ty));
3560  AllocSize = ConstantExpr::getTruncOrBitCast(AllocSize, ITy);
3561  Instruction* Malloc = CallInst::CreateMalloc(unwrap(B)->GetInsertBlock(),
3562  ITy, unwrap(Ty), AllocSize,
3563  unwrap(Val), nullptr, "");
3564  return wrap(unwrap(B)->Insert(Malloc, Twine(Name)));
3565 }
3566 
3568  LLVMValueRef Val, LLVMValueRef Len,
3569  unsigned Align) {
3570  return wrap(unwrap(B)->CreateMemSet(unwrap(Ptr), unwrap(Val), unwrap(Len),
3571  MaybeAlign(Align)));
3572 }
3573 
3575  LLVMValueRef Dst, unsigned DstAlign,
3576  LLVMValueRef Src, unsigned SrcAlign,
3577  LLVMValueRef Size) {
3578  return wrap(unwrap(B)->CreateMemCpy(unwrap(Dst), MaybeAlign(DstAlign),
3579  unwrap(Src), MaybeAlign(SrcAlign),
3580  unwrap(Size)));
3581 }
3582 
3584  LLVMValueRef Dst, unsigned DstAlign,
3585  LLVMValueRef Src, unsigned SrcAlign,
3586  LLVMValueRef Size) {
3587  return wrap(unwrap(B)->CreateMemMove(unwrap(Dst), MaybeAlign(DstAlign),
3588  unwrap(Src), MaybeAlign(SrcAlign),
3589  unwrap(Size)));
3590 }
3591 
3593  const char *Name) {
3594  return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), nullptr, Name));
3595 }
3596 
3598  LLVMValueRef Val, const char *Name) {
3599  return wrap(unwrap(B)->CreateAlloca(unwrap(Ty), unwrap(Val), Name));
3600 }
3601 
3603  return wrap(unwrap(B)->Insert(
3604  CallInst::CreateFree(unwrap(PointerVal), unwrap(B)->GetInsertBlock())));
3605 }
3606 
3608  const char *Name) {
3609  Value *V = unwrap(PointerVal);
3610  PointerType *Ty = cast<PointerType>(V->getType());
3611 
3612  return wrap(
3613  unwrap(B)->CreateLoad(Ty->getNonOpaquePointerElementType(), V, Name));
3614 }
3615 
3617  LLVMValueRef PointerVal, const char *Name) {
3618  return wrap(unwrap(B)->CreateLoad(unwrap(Ty), unwrap(PointerVal), Name));
3619 }
3620 
3622  LLVMValueRef PointerVal) {
3623  return wrap(unwrap(B)->CreateStore(unwrap(Val), unwrap(PointerVal)));
3624 }
3625 
3627  switch (Ordering) {
3637  }
3638 
3639  llvm_unreachable("Invalid LLVMAtomicOrdering value!");
3640 }
3641 
3643  switch (Ordering) {
3653  }
3654 
3655  llvm_unreachable("Invalid AtomicOrdering value!");
3656 }
3657 
3659  switch (BinOp) {
3673  }
3674 
3675  llvm_unreachable("Invalid LLVMAtomicRMWBinOp value!");
3676 }
3677 
3679  switch (BinOp) {
3693  default: break;
3694  }
3695 
3696  llvm_unreachable("Invalid AtomicRMWBinOp value!");
3697 }
3698 
3699 // TODO: Should this and other atomic instructions support building with
3700 // "syncscope"?
3702  LLVMBool isSingleThread, const char *Name) {
3703  return wrap(
3704  unwrap(B)->CreateFence(mapFromLLVMOrdering(Ordering),
3705  isSingleThread ? SyncScope::SingleThread
3707  Name));
3708 }
3709 
3711  LLVMValueRef *Indices, unsigned NumIndices,
3712  const char *Name) {
3713  ArrayRef<Value *> IdxList(unwrap(Indices), NumIndices);
3714  Value *Val = unwrap(Pointer);
3716  return wrap(unwrap(B)->CreateGEP(Ty, Val, IdxList, Name));
3717 }
3718 
3720  LLVMValueRef Pointer, LLVMValueRef *Indices,
3721  unsigned NumIndices, const char *Name) {
3722  ArrayRef<Value *> IdxList(unwrap(Indices), NumIndices);
3723  return wrap(unwrap(B)->CreateGEP(unwrap(Ty), unwrap(Pointer), IdxList, Name));
3724 }
3725 
3727  LLVMValueRef *Indices, unsigned NumIndices,
3728  const char *Name) {
3729  ArrayRef<Value *> IdxList(unwrap(Indices), NumIndices);
3730  Value *Val = unwrap(Pointer);
3732  return wrap(unwrap(B)->CreateInBoundsGEP(Ty, Val, IdxList, Name));
3733 }
3734 
3736  LLVMValueRef Pointer, LLVMValueRef *Indices,
3737  unsigned NumIndices, const char *Name) {
3738  ArrayRef<Value *> IdxList(unwrap(Indices), NumIndices);
3739  return wrap(
3740  unwrap(B)->CreateInBoundsGEP(unwrap(Ty), unwrap(Pointer), IdxList, Name));
3741 }
3742 
3744  unsigned Idx, const char *Name) {
3745  Value *Val = unwrap(Pointer);
3747  return wrap(unwrap(B)->CreateStructGEP(Ty, Val, Idx, Name));
3748 }
3749 
3751  LLVMValueRef Pointer, unsigned Idx,
3752  const char *Name) {
3753  return wrap(
3754  unwrap(B)->CreateStructGEP(unwrap(Ty), unwrap(Pointer), Idx, Name));
3755 }
3756 
3758  const char *Name) {
3759  return wrap(unwrap(B)->CreateGlobalString(Str, Name));
3760 }
3761 
3763  const char *Name) {
3764  return wrap(unwrap(B)->CreateGlobalStringPtr(Str, Name));
3765 }
3766 
3768  Value *P = unwrap<Value>(MemAccessInst);
3769  if (LoadInst *LI = dyn_cast<LoadInst>(P))
3770  return LI->isVolatile();
3771  if (StoreInst *SI = dyn_cast<StoreInst>(P))
3772  return SI->isVolatile();
3773  if (AtomicRMWInst *AI = dyn_cast<AtomicRMWInst>(P))
3774  return AI->isVolatile();
3775  return cast<AtomicCmpXchgInst>(P)->isVolatile();
3776 }
3777 
3778 void LLVMSetVolatile(LLVMValueRef MemAccessInst, LLVMBool isVolatile) {
3779  Value *P = unwrap<Value>(MemAccessInst);
3780  if (LoadInst *LI = dyn_cast<LoadInst>(P))
3781  return LI->setVolatile(isVolatile);
3782  if (StoreInst *SI = dyn_cast<StoreInst>(P))
3783  return SI->setVolatile(isVolatile);
3784  if (AtomicRMWInst *AI = dyn_cast<AtomicRMWInst>(P))
3785  return AI->setVolatile(isVolatile);
3786  return cast<AtomicCmpXchgInst>(P)->setVolatile(isVolatile);
3787 }
3788 
3790  return unwrap<AtomicCmpXchgInst>(CmpXchgInst)->isWeak();
3791 }
3792 
3793 void LLVMSetWeak(LLVMValueRef CmpXchgInst, LLVMBool isWeak) {
3794  return unwrap<AtomicCmpXchgInst>(CmpXchgInst)->setWeak(isWeak);
3795 }
3796 
3798  Value *P = unwrap<Value>(MemAccessInst);
3799  AtomicOrdering O;
3800  if (LoadInst *LI = dyn_cast<LoadInst>(P))
3801  O = LI->getOrdering();
3802  else if (StoreInst *SI = dyn_cast<StoreInst>(P))
3803  O = SI->getOrdering();
3804  else
3805  O = cast<AtomicRMWInst>(P)->getOrdering();
3806  return mapToLLVMOrdering(O);
3807 }
3808 
3809 void LLVMSetOrdering(LLVMValueRef MemAccessInst, LLVMAtomicOrdering Ordering) {
3810  Value *P = unwrap<Value>(MemAccessInst);
3811  AtomicOrdering O = mapFromLLVMOrdering(Ordering);
3812 
3813  if (LoadInst *LI = dyn_cast<LoadInst>(P))
3814  return LI->setOrdering(O);
3815  return cast<StoreInst>(P)->setOrdering(O);
3816 }
3817 
3819  return mapToLLVMRMWBinOp(unwrap<AtomicRMWInst>(Inst)->getOperation());
3820 }
3821 
3823  unwrap<AtomicRMWInst>(Inst)->setOperation(mapFromLLVMRMWBinOp(BinOp));
3824 }
3825 
3826 /*--.. Casts ...............................................................--*/
3827 
3829  LLVMTypeRef DestTy, const char *Name) {
3830  return wrap(unwrap(B)->CreateTrunc(unwrap(Val), unwrap(DestTy), Name));
3831 }
3832 
3834  LLVMTypeRef DestTy, const char *Name) {
3835  return wrap(unwrap(B)->CreateZExt(unwrap(Val), unwrap(DestTy), Name));
3836 }
3837 
3839  LLVMTypeRef DestTy, const char *Name) {
3840  return wrap(unwrap(B)->CreateSExt(unwrap(Val), unwrap(DestTy), Name));
3841 }
3842 
3844  LLVMTypeRef DestTy, const char *Name) {
3845  return wrap(unwrap(B)->CreateFPToUI(unwrap(Val), unwrap(DestTy), Name));
3846 }
3847 
3849  LLVMTypeRef DestTy, const char *Name) {
3850  return wrap(unwrap(B)->CreateFPToSI(unwrap(Val), unwrap(DestTy), Name));
3851 }
3852 
3854  LLVMTypeRef DestTy, const char *Name) {
3855  return wrap(unwrap(B)->CreateUIToFP(unwrap(Val), unwrap(DestTy), Name));
3856 }
3857 
3859  LLVMTypeRef DestTy, const char *Name) {
3860  return wrap(unwrap(B)->CreateSIToFP(unwrap(Val), unwrap(DestTy), Name));
3861 }
3862 
3864  LLVMTypeRef DestTy, const char *Name) {
3865  return wrap(unwrap(B)->CreateFPTrunc(unwrap(Val), unwrap(DestTy), Name));
3866 }
3867 
3869  LLVMTypeRef DestTy, const char *Name) {
3870  return wrap(unwrap(B)->CreateFPExt(unwrap(Val), unwrap(DestTy), Name));
3871 }
3872