ValueTypes.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. //===- CodeGen/ValueTypes.h - Low-Level Target independ. types --*- C++ -*-===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This file defines the set of low-level target independent types which various
  11. // values in the code generator are. This allows the target specific behavior
  12. // of instructions to be described to target independent passes.
  13. //
  14. //===----------------------------------------------------------------------===//
  15. #ifndef LLVM_CODEGEN_VALUETYPES_H
  16. #define LLVM_CODEGEN_VALUETYPES_H
  17. #include "llvm/CodeGen/MachineValueType.h"
  18. #include <cassert>
  19. #include <string>
  20. namespace llvm {
  21. class LLVMContext;
  22. class Type;
  23. /// EVT - Extended Value Type. Capable of holding value types which are not
  24. /// native for any processor (such as the i12345 type), as well as the types
  25. /// a MVT can represent.
  26. struct EVT {
  27. private:
  28. MVT V;
  29. Type *LLVMTy;
  30. public:
  31. LLVM_CONSTEXPR EVT() : V(MVT::INVALID_SIMPLE_VALUE_TYPE), LLVMTy(nullptr) {}
  32. LLVM_CONSTEXPR EVT(MVT::SimpleValueType SVT) : V(SVT), LLVMTy(nullptr) {}
  33. LLVM_CONSTEXPR EVT(MVT S) : V(S), LLVMTy(nullptr) {}
  34. bool operator==(EVT VT) const {
  35. return !(*this != VT);
  36. }
  37. bool operator!=(EVT VT) const {
  38. if (V.SimpleTy != VT.V.SimpleTy)
  39. return true;
  40. if (V.SimpleTy < 0)
  41. return LLVMTy != VT.LLVMTy;
  42. return false;
  43. }
  44. /// getFloatingPointVT - Returns the EVT that represents a floating point
  45. /// type with the given number of bits. There are two floating point types
  46. /// with 128 bits - this returns f128 rather than ppcf128.
  47. static EVT getFloatingPointVT(unsigned BitWidth) {
  48. return MVT::getFloatingPointVT(BitWidth);
  49. }
  50. /// getIntegerVT - Returns the EVT that represents an integer with the given
  51. /// number of bits.
  52. static EVT getIntegerVT(LLVMContext &Context, unsigned BitWidth) {
  53. MVT M = MVT::getIntegerVT(BitWidth);
  54. if (M.SimpleTy >= 0)
  55. return M;
  56. return getExtendedIntegerVT(Context, BitWidth);
  57. }
  58. /// getVectorVT - Returns the EVT that represents a vector NumElements in
  59. /// length, where each element is of type VT.
  60. static EVT getVectorVT(LLVMContext &Context, EVT VT, unsigned NumElements) {
  61. MVT M = MVT::getVectorVT(VT.V, NumElements);
  62. if (M.SimpleTy >= 0)
  63. return M;
  64. return getExtendedVectorVT(Context, VT, NumElements);
  65. }
  66. /// changeVectorElementTypeToInteger - Return a vector with the same number
  67. /// of elements as this vector, but with the element type converted to an
  68. /// integer type with the same bitwidth.
  69. EVT changeVectorElementTypeToInteger() const {
  70. if (!isSimple())
  71. return changeExtendedVectorElementTypeToInteger();
  72. MVT EltTy = getSimpleVT().getVectorElementType();
  73. unsigned BitWidth = EltTy.getSizeInBits();
  74. MVT IntTy = MVT::getIntegerVT(BitWidth);
  75. MVT VecTy = MVT::getVectorVT(IntTy, getVectorNumElements());
  76. assert(VecTy.SimpleTy >= 0 &&
  77. "Simple vector VT not representable by simple integer vector VT!");
  78. return VecTy;
  79. }
  80. /// isSimple - Test if the given EVT is simple (as opposed to being
  81. /// extended).
  82. bool isSimple() const {
  83. return V.SimpleTy >= 0;
  84. }
  85. /// isExtended - Test if the given EVT is extended (as opposed to
  86. /// being simple).
  87. bool isExtended() const {
  88. return !isSimple();
  89. }
  90. /// isFloatingPoint - Return true if this is a FP, or a vector FP type.
  91. bool isFloatingPoint() const {
  92. return isSimple() ? V.isFloatingPoint() : isExtendedFloatingPoint();
  93. }
  94. /// isInteger - Return true if this is an integer, or a vector integer type.
  95. bool isInteger() const {
  96. return isSimple() ? V.isInteger() : isExtendedInteger();
  97. }
  98. /// isVector - Return true if this is a vector value type.
  99. bool isVector() const {
  100. return isSimple() ? V.isVector() : isExtendedVector();
  101. }
  102. /// is16BitVector - Return true if this is a 16-bit vector type.
  103. bool is16BitVector() const {
  104. return isSimple() ? V.is16BitVector() : isExtended16BitVector();
  105. }
  106. /// is32BitVector - Return true if this is a 32-bit vector type.
  107. bool is32BitVector() const {
  108. return isSimple() ? V.is32BitVector() : isExtended32BitVector();
  109. }
  110. /// is64BitVector - Return true if this is a 64-bit vector type.
  111. bool is64BitVector() const {
  112. return isSimple() ? V.is64BitVector() : isExtended64BitVector();
  113. }
  114. /// is128BitVector - Return true if this is a 128-bit vector type.
  115. bool is128BitVector() const {
  116. return isSimple() ? V.is128BitVector() : isExtended128BitVector();
  117. }
  118. /// is256BitVector - Return true if this is a 256-bit vector type.
  119. bool is256BitVector() const {
  120. return isSimple() ? V.is256BitVector() : isExtended256BitVector();
  121. }
  122. /// is512BitVector - Return true if this is a 512-bit vector type.
  123. bool is512BitVector() const {
  124. return isSimple() ? V.is512BitVector() : isExtended512BitVector();
  125. }
  126. /// is1024BitVector - Return true if this is a 1024-bit vector type.
  127. bool is1024BitVector() const {
  128. return isSimple() ? V.is1024BitVector() : isExtended1024BitVector();
  129. }
  130. /// isOverloaded - Return true if this is an overloaded type for TableGen.
  131. bool isOverloaded() const {
  132. return (V==MVT::iAny || V==MVT::fAny || V==MVT::vAny || V==MVT::iPTRAny);
  133. }
  134. /// isByteSized - Return true if the bit size is a multiple of 8.
  135. bool isByteSized() const {
  136. return (getSizeInBits() & 7) == 0;
  137. }
  138. /// isRound - Return true if the size is a power-of-two number of bytes.
  139. bool isRound() const {
  140. unsigned BitSize = getSizeInBits();
  141. return BitSize >= 8 && !(BitSize & (BitSize - 1));
  142. }
  143. /// bitsEq - Return true if this has the same number of bits as VT.
  144. bool bitsEq(EVT VT) const {
  145. if (EVT::operator==(VT)) return true;
  146. return getSizeInBits() == VT.getSizeInBits();
  147. }
  148. /// bitsGT - Return true if this has more bits than VT.
  149. bool bitsGT(EVT VT) const {
  150. if (EVT::operator==(VT)) return false;
  151. return getSizeInBits() > VT.getSizeInBits();
  152. }
  153. /// bitsGE - Return true if this has no less bits than VT.
  154. bool bitsGE(EVT VT) const {
  155. if (EVT::operator==(VT)) return true;
  156. return getSizeInBits() >= VT.getSizeInBits();
  157. }
  158. /// bitsLT - Return true if this has less bits than VT.
  159. bool bitsLT(EVT VT) const {
  160. if (EVT::operator==(VT)) return false;
  161. return getSizeInBits() < VT.getSizeInBits();
  162. }
  163. /// bitsLE - Return true if this has no more bits than VT.
  164. bool bitsLE(EVT VT) const {
  165. if (EVT::operator==(VT)) return true;
  166. return getSizeInBits() <= VT.getSizeInBits();
  167. }
  168. /// getSimpleVT - Return the SimpleValueType held in the specified
  169. /// simple EVT.
  170. MVT getSimpleVT() const {
  171. assert(isSimple() && "Expected a SimpleValueType!");
  172. return V;
  173. }
  174. /// getScalarType - If this is a vector type, return the element type,
  175. /// otherwise return this.
  176. EVT getScalarType() const {
  177. return isVector() ? getVectorElementType() : *this;
  178. }
  179. /// getVectorElementType - Given a vector type, return the type of
  180. /// each element.
  181. EVT getVectorElementType() const {
  182. assert(isVector() && "Invalid vector type!");
  183. if (isSimple())
  184. return V.getVectorElementType();
  185. return getExtendedVectorElementType();
  186. }
  187. /// getVectorNumElements - Given a vector type, return the number of
  188. /// elements it contains.
  189. unsigned getVectorNumElements() const {
  190. assert(isVector() && "Invalid vector type!");
  191. if (isSimple())
  192. return V.getVectorNumElements();
  193. return getExtendedVectorNumElements();
  194. }
  195. /// getSizeInBits - Return the size of the specified value type in bits.
  196. unsigned getSizeInBits() const {
  197. if (isSimple())
  198. return V.getSizeInBits();
  199. return getExtendedSizeInBits();
  200. }
  201. unsigned getScalarSizeInBits() const {
  202. return getScalarType().getSizeInBits();
  203. }
  204. /// getStoreSize - Return the number of bytes overwritten by a store
  205. /// of the specified value type.
  206. unsigned getStoreSize() const {
  207. return (getSizeInBits() + 7) / 8;
  208. }
  209. /// getStoreSizeInBits - Return the number of bits overwritten by a store
  210. /// of the specified value type.
  211. unsigned getStoreSizeInBits() const {
  212. return getStoreSize() * 8;
  213. }
  214. /// getRoundIntegerType - Rounds the bit-width of the given integer EVT up
  215. /// to the nearest power of two (and at least to eight), and returns the
  216. /// integer EVT with that number of bits.
  217. EVT getRoundIntegerType(LLVMContext &Context) const {
  218. assert(isInteger() && !isVector() && "Invalid integer type!");
  219. unsigned BitWidth = getSizeInBits();
  220. if (BitWidth <= 8)
  221. return EVT(MVT::i8);
  222. return getIntegerVT(Context, 1 << Log2_32_Ceil(BitWidth));
  223. }
  224. /// getHalfSizedIntegerVT - Finds the smallest simple value type that is
  225. /// greater than or equal to half the width of this EVT. If no simple
  226. /// value type can be found, an extended integer value type of half the
  227. /// size (rounded up) is returned.
  228. EVT getHalfSizedIntegerVT(LLVMContext &Context) const {
  229. assert(isInteger() && !isVector() && "Invalid integer type!");
  230. unsigned EVTSize = getSizeInBits();
  231. for (unsigned IntVT = MVT::FIRST_INTEGER_VALUETYPE;
  232. IntVT <= MVT::LAST_INTEGER_VALUETYPE; ++IntVT) {
  233. EVT HalfVT = EVT((MVT::SimpleValueType)IntVT);
  234. if (HalfVT.getSizeInBits() * 2 >= EVTSize)
  235. return HalfVT;
  236. }
  237. return getIntegerVT(Context, (EVTSize + 1) / 2);
  238. }
  239. /// \brief Return a VT for an integer vector type with the size of the
  240. /// elements doubled. The typed returned may be an extended type.
  241. EVT widenIntegerVectorElementType(LLVMContext &Context) const {
  242. EVT EltVT = getVectorElementType();
  243. EltVT = EVT::getIntegerVT(Context, 2 * EltVT.getSizeInBits());
  244. return EVT::getVectorVT(Context, EltVT, getVectorNumElements());
  245. }
  246. /// isPow2VectorType - Returns true if the given vector is a power of 2.
  247. bool isPow2VectorType() const {
  248. unsigned NElts = getVectorNumElements();
  249. return !(NElts & (NElts - 1));
  250. }
  251. /// getPow2VectorType - Widens the length of the given vector EVT up to
  252. /// the nearest power of 2 and returns that type.
  253. EVT getPow2VectorType(LLVMContext &Context) const {
  254. if (!isPow2VectorType()) {
  255. unsigned NElts = getVectorNumElements();
  256. unsigned Pow2NElts = 1 << Log2_32_Ceil(NElts);
  257. return EVT::getVectorVT(Context, getVectorElementType(), Pow2NElts);
  258. }
  259. else {
  260. return *this;
  261. }
  262. }
  263. /// getEVTString - This function returns value type as a string,
  264. /// e.g. "i32".
  265. std::string getEVTString() const;
  266. /// getTypeForEVT - This method returns an LLVM type corresponding to the
  267. /// specified EVT. For integer types, this returns an unsigned type. Note
  268. /// that this will abort for types that cannot be represented.
  269. Type *getTypeForEVT(LLVMContext &Context) const;
  270. /// getEVT - Return the value type corresponding to the specified type.
  271. /// This returns all pointers as iPTR. If HandleUnknown is true, unknown
  272. /// types are returned as Other, otherwise they are invalid.
  273. static EVT getEVT(Type *Ty, bool HandleUnknown = false);
  274. intptr_t getRawBits() const {
  275. if (isSimple())
  276. return V.SimpleTy;
  277. else
  278. return (intptr_t)(LLVMTy);
  279. }
  280. /// compareRawBits - A meaningless but well-behaved order, useful for
  281. /// constructing containers.
  282. struct compareRawBits {
  283. bool operator()(EVT L, EVT R) const {
  284. if (L.V.SimpleTy == R.V.SimpleTy)
  285. return L.LLVMTy < R.LLVMTy;
  286. else
  287. return L.V.SimpleTy < R.V.SimpleTy;
  288. }
  289. };
  290. private:
  291. // Methods for handling the Extended-type case in functions above.
  292. // These are all out-of-line to prevent users of this header file
  293. // from having a dependency on Type.h.
  294. EVT changeExtendedVectorElementTypeToInteger() const;
  295. static EVT getExtendedIntegerVT(LLVMContext &C, unsigned BitWidth);
  296. static EVT getExtendedVectorVT(LLVMContext &C, EVT VT,
  297. unsigned NumElements);
  298. bool isExtendedFloatingPoint() const LLVM_READONLY;
  299. bool isExtendedInteger() const LLVM_READONLY;
  300. bool isExtendedVector() const LLVM_READONLY;
  301. bool isExtended16BitVector() const LLVM_READONLY;
  302. bool isExtended32BitVector() const LLVM_READONLY;
  303. bool isExtended64BitVector() const LLVM_READONLY;
  304. bool isExtended128BitVector() const LLVM_READONLY;
  305. bool isExtended256BitVector() const LLVM_READONLY;
  306. bool isExtended512BitVector() const LLVM_READONLY;
  307. bool isExtended1024BitVector() const LLVM_READONLY;
  308. EVT getExtendedVectorElementType() const;
  309. unsigned getExtendedVectorNumElements() const LLVM_READONLY;
  310. unsigned getExtendedSizeInBits() const;
  311. };
  312. } // End llvm namespace
  313. #endif