Intrinsics.h 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. //===- Intrinsics.h - LLVM Intrinsic Function Handling ----------*- C++ -*-===//
  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 defines a set of enums which allow processing of intrinsic
  10. // functions. Values of these enum types are returned by
  11. // Function::getIntrinsicID.
  12. //
  13. //===----------------------------------------------------------------------===//
  14. #ifndef LLVM_IR_INTRINSICS_H
  15. #define LLVM_IR_INTRINSICS_H
  16. #include "llvm/ADT/ArrayRef.h"
  17. #include "llvm/ADT/None.h"
  18. #include "llvm/ADT/Optional.h"
  19. #include "llvm/Support/TypeSize.h"
  20. #include <string>
  21. namespace llvm {
  22. class Type;
  23. class FunctionType;
  24. class Function;
  25. class LLVMContext;
  26. class Module;
  27. class AttributeList;
  28. /// This namespace contains an enum with a value for every intrinsic/builtin
  29. /// function known by LLVM. The enum values are returned by
  30. /// Function::getIntrinsicID().
  31. namespace Intrinsic {
  32. // Abstraction for the arguments of the noalias intrinsics
  33. static const int NoAliasScopeDeclScopeArg = 0;
  34. // Intrinsic ID type. This is an opaque typedef to facilitate splitting up
  35. // the enum into target-specific enums.
  36. typedef unsigned ID;
  37. enum IndependentIntrinsics : unsigned {
  38. not_intrinsic = 0, // Must be zero
  39. // Get the intrinsic enums generated from Intrinsics.td
  40. #define GET_INTRINSIC_ENUM_VALUES
  41. #include "llvm/IR/IntrinsicEnums.inc"
  42. #undef GET_INTRINSIC_ENUM_VALUES
  43. };
  44. /// Return the LLVM name for an intrinsic, such as "llvm.ppc.altivec.lvx".
  45. /// Note, this version is for intrinsics with no overloads. Use the other
  46. /// version of getName if overloads are required.
  47. StringRef getName(ID id);
  48. /// Return the LLVM name for an intrinsic, such as "llvm.ppc.altivec.lvx".
  49. /// Note, this version of getName supports overloads, but not unnamed types.
  50. /// It is less efficient than the StringRef version of this function. If no
  51. /// overloads are required, it is safe to use this version, but better to use
  52. /// the StringRef version.
  53. std::string getName(ID Id, ArrayRef<Type *> Tys);
  54. /// Return the LLVM name for an intrinsic, such as "llvm.ssa.copy.p0s_s.1".
  55. /// Note, this version of getName supports overloads and unnamed types, but is
  56. /// less efficient than the StringRef version of this function. If no
  57. /// overloads are required, it is safe to use this version, but better to use
  58. /// the StringRef version. A function type FT can be provided to avoid
  59. /// computing it. It is used (or computed) if one of the types is based on an
  60. /// unnamed type.
  61. std::string getName(ID Id, ArrayRef<Type *> Tys, Module *M, FunctionType *FT);
  62. /// Return the function type for an intrinsic.
  63. FunctionType *getType(LLVMContext &Context, ID id,
  64. ArrayRef<Type*> Tys = None);
  65. /// Returns true if the intrinsic can be overloaded.
  66. bool isOverloaded(ID id);
  67. /// Returns true if the intrinsic is a leaf, i.e. it does not make any calls
  68. /// itself. Most intrinsics are leafs, the exceptions being the patchpoint
  69. /// and statepoint intrinsics. These call (or invoke) their "target" argument.
  70. bool isLeaf(ID id);
  71. /// Return the attributes for an intrinsic.
  72. AttributeList getAttributes(LLVMContext &C, ID id);
  73. /// Create or insert an LLVM Function declaration for an intrinsic, and return
  74. /// it.
  75. ///
  76. /// The Tys parameter is for intrinsics with overloaded types (e.g., those
  77. /// using iAny, fAny, vAny, or iPTRAny). For a declaration of an overloaded
  78. /// intrinsic, Tys must provide exactly one type for each overloaded type in
  79. /// the intrinsic.
  80. Function *getDeclaration(Module *M, ID id, ArrayRef<Type*> Tys = None);
  81. /// Looks up Name in NameTable via binary search. NameTable must be sorted
  82. /// and all entries must start with "llvm.". If NameTable contains an exact
  83. /// match for Name or a prefix of Name followed by a dot, its index in
  84. /// NameTable is returned. Otherwise, -1 is returned.
  85. int lookupLLVMIntrinsicByName(ArrayRef<const char *> NameTable,
  86. StringRef Name);
  87. /// Map a GCC builtin name to an intrinsic ID.
  88. ID getIntrinsicForGCCBuiltin(const char *Prefix, StringRef BuiltinName);
  89. /// Map a MS builtin name to an intrinsic ID.
  90. ID getIntrinsicForMSBuiltin(const char *Prefix, StringRef BuiltinName);
  91. /// This is a type descriptor which explains the type requirements of an
  92. /// intrinsic. This is returned by getIntrinsicInfoTableEntries.
  93. struct IITDescriptor {
  94. enum IITDescriptorKind {
  95. Void,
  96. VarArg,
  97. MMX,
  98. Token,
  99. Metadata,
  100. Half,
  101. BFloat,
  102. Float,
  103. Double,
  104. Quad,
  105. Integer,
  106. Vector,
  107. Pointer,
  108. Struct,
  109. Argument,
  110. ExtendArgument,
  111. TruncArgument,
  112. HalfVecArgument,
  113. SameVecWidthArgument,
  114. PtrToArgument,
  115. PtrToElt,
  116. VecOfAnyPtrsToElt,
  117. VecElementArgument,
  118. Subdivide2Argument,
  119. Subdivide4Argument,
  120. VecOfBitcastsToInt,
  121. AMX
  122. } Kind;
  123. union {
  124. unsigned Integer_Width;
  125. unsigned Float_Width;
  126. unsigned Pointer_AddressSpace;
  127. unsigned Struct_NumElements;
  128. unsigned Argument_Info;
  129. ElementCount Vector_Width;
  130. };
  131. enum ArgKind {
  132. AK_Any,
  133. AK_AnyInteger,
  134. AK_AnyFloat,
  135. AK_AnyVector,
  136. AK_AnyPointer,
  137. AK_MatchType = 7
  138. };
  139. unsigned getArgumentNumber() const {
  140. assert(Kind == Argument || Kind == ExtendArgument ||
  141. Kind == TruncArgument || Kind == HalfVecArgument ||
  142. Kind == SameVecWidthArgument || Kind == PtrToArgument ||
  143. Kind == PtrToElt || Kind == VecElementArgument ||
  144. Kind == Subdivide2Argument || Kind == Subdivide4Argument ||
  145. Kind == VecOfBitcastsToInt);
  146. return Argument_Info >> 3;
  147. }
  148. ArgKind getArgumentKind() const {
  149. assert(Kind == Argument || Kind == ExtendArgument ||
  150. Kind == TruncArgument || Kind == HalfVecArgument ||
  151. Kind == SameVecWidthArgument || Kind == PtrToArgument ||
  152. Kind == VecElementArgument || Kind == Subdivide2Argument ||
  153. Kind == Subdivide4Argument || Kind == VecOfBitcastsToInt);
  154. return (ArgKind)(Argument_Info & 7);
  155. }
  156. // VecOfAnyPtrsToElt uses both an overloaded argument (for address space)
  157. // and a reference argument (for matching vector width and element types)
  158. unsigned getOverloadArgNumber() const {
  159. assert(Kind == VecOfAnyPtrsToElt);
  160. return Argument_Info >> 16;
  161. }
  162. unsigned getRefArgNumber() const {
  163. assert(Kind == VecOfAnyPtrsToElt);
  164. return Argument_Info & 0xFFFF;
  165. }
  166. static IITDescriptor get(IITDescriptorKind K, unsigned Field) {
  167. IITDescriptor Result = { K, { Field } };
  168. return Result;
  169. }
  170. static IITDescriptor get(IITDescriptorKind K, unsigned short Hi,
  171. unsigned short Lo) {
  172. unsigned Field = Hi << 16 | Lo;
  173. IITDescriptor Result = {K, {Field}};
  174. return Result;
  175. }
  176. static IITDescriptor getVector(unsigned Width, bool IsScalable) {
  177. IITDescriptor Result = {Vector, {0}};
  178. Result.Vector_Width = ElementCount::get(Width, IsScalable);
  179. return Result;
  180. }
  181. };
  182. /// Return the IIT table descriptor for the specified intrinsic into an array
  183. /// of IITDescriptors.
  184. void getIntrinsicInfoTableEntries(ID id, SmallVectorImpl<IITDescriptor> &T);
  185. enum MatchIntrinsicTypesResult {
  186. MatchIntrinsicTypes_Match = 0,
  187. MatchIntrinsicTypes_NoMatchRet = 1,
  188. MatchIntrinsicTypes_NoMatchArg = 2,
  189. };
  190. /// Match the specified function type with the type constraints specified by
  191. /// the .td file. If the given type is an overloaded type it is pushed to the
  192. /// ArgTys vector.
  193. ///
  194. /// Returns false if the given type matches with the constraints, true
  195. /// otherwise.
  196. MatchIntrinsicTypesResult
  197. matchIntrinsicSignature(FunctionType *FTy, ArrayRef<IITDescriptor> &Infos,
  198. SmallVectorImpl<Type *> &ArgTys);
  199. /// Verify if the intrinsic has variable arguments. This method is intended to
  200. /// be called after all the fixed arguments have been matched first.
  201. ///
  202. /// This method returns true on error.
  203. bool matchIntrinsicVarArg(bool isVarArg, ArrayRef<IITDescriptor> &Infos);
  204. /// Gets the type arguments of an intrinsic call by matching type contraints
  205. /// specified by the .td file. The overloaded types are pushed into the
  206. /// AgTys vector.
  207. ///
  208. /// Returns false if the given function is not a valid intrinsic call.
  209. bool getIntrinsicSignature(Function *F, SmallVectorImpl<Type *> &ArgTys);
  210. // Checks if the intrinsic name matches with its signature and if not
  211. // returns the declaration with the same signature and remangled name.
  212. llvm::Optional<Function*> remangleIntrinsicFunction(Function *F);
  213. } // End Intrinsic namespace
  214. } // End llvm namespace
  215. #endif