Intrinsics.h 9.2 KB

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