Intrinsics.h 8.5 KB

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