LLVM  10.0.0
ValueTypes.h
Go to the documentation of this file.
1 //===- CodeGen/ValueTypes.h - Low-Level Target independ. types --*- 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 the set of low-level target independent types which various
10 // values in the code generator are. This allows the target specific behavior
11 // of instructions to be described to target independent passes.
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #ifndef LLVM_CODEGEN_VALUETYPES_H
16 #define LLVM_CODEGEN_VALUETYPES_H
17 
18 #include "llvm/Support/Compiler.h"
21 #include "llvm/Support/TypeSize.h"
22 #include <cassert>
23 #include <cstdint>
24 #include <string>
25 
26 namespace llvm {
27 
28  class LLVMContext;
29  class Type;
30 
31  /// Extended Value Type. Capable of holding value types which are not native
32  /// for any processor (such as the i12345 type), as well as the types an MVT
33  /// can represent.
34  struct EVT {
35  private:
37  Type *LLVMTy = nullptr;
38 
39  public:
40  constexpr EVT() = default;
41  constexpr EVT(MVT::SimpleValueType SVT) : V(SVT) {}
42  constexpr EVT(MVT S) : V(S) {}
43 
44  bool operator==(EVT VT) const {
45  return !(*this != VT);
46  }
47  bool operator!=(EVT VT) const {
48  if (V.SimpleTy != VT.V.SimpleTy)
49  return true;
51  return LLVMTy != VT.LLVMTy;
52  return false;
53  }
54 
55  /// Returns the EVT that represents a floating-point type with the given
56  /// number of bits. There are two floating-point types with 128 bits - this
57  /// returns f128 rather than ppcf128.
58  static EVT getFloatingPointVT(unsigned BitWidth) {
59  return MVT::getFloatingPointVT(BitWidth);
60  }
61 
62  /// Returns the EVT that represents an integer with the given number of
63  /// bits.
64  static EVT getIntegerVT(LLVMContext &Context, unsigned BitWidth) {
65  MVT M = MVT::getIntegerVT(BitWidth);
67  return M;
68  return getExtendedIntegerVT(Context, BitWidth);
69  }
70 
71  /// Returns the EVT that represents a vector NumElements in length, where
72  /// each element is of type VT.
73  static EVT getVectorVT(LLVMContext &Context, EVT VT, unsigned NumElements,
74  bool IsScalable = false) {
75  MVT M = MVT::getVectorVT(VT.V, NumElements, IsScalable);
77  return M;
78 
79  assert(!IsScalable && "We don't support extended scalable types yet");
80  return getExtendedVectorVT(Context, VT, NumElements);
81  }
82 
83  /// Returns the EVT that represents a vector EC.Min elements in length,
84  /// where each element is of type VT.
86  MVT M = MVT::getVectorVT(VT.V, EC);
88  return M;
89  assert (!EC.Scalable && "We don't support extended scalable types yet");
90  return getExtendedVectorVT(Context, VT, EC.Min);
91  }
92 
93  /// Return a vector with the same number of elements as this vector, but
94  /// with the element type converted to an integer type with the same
95  /// bitwidth.
97  if (!isSimple()) {
99  "We don't support extended scalable types yet");
100  return changeExtendedVectorElementTypeToInteger();
101  }
103  unsigned BitWidth = EltTy.getSizeInBits();
104  MVT IntTy = MVT::getIntegerVT(BitWidth);
105  MVT VecTy = MVT::getVectorVT(IntTy, getVectorNumElements(),
106  isScalableVector());
108  "Simple vector VT not representable by simple integer vector VT!");
109  return VecTy;
110  }
111 
112  /// Return the type converted to an equivalently sized integer or vector
113  /// with integer element type. Similar to changeVectorElementTypeToInteger,
114  /// but also handles scalars.
116  if (isVector())
118 
119  if (isSimple())
121 
122  return changeExtendedTypeToInteger();
123  }
124 
125  /// Test if the given EVT is simple (as opposed to being extended).
126  bool isSimple() const {
128  }
129 
130  /// Test if the given EVT is extended (as opposed to being simple).
131  bool isExtended() const {
132  return !isSimple();
133  }
134 
135  /// Return true if this is a FP or a vector FP type.
136  bool isFloatingPoint() const {
137  return isSimple() ? V.isFloatingPoint() : isExtendedFloatingPoint();
138  }
139 
140  /// Return true if this is an integer or a vector integer type.
141  bool isInteger() const {
142  return isSimple() ? V.isInteger() : isExtendedInteger();
143  }
144 
145  /// Return true if this is an integer, but not a vector.
146  bool isScalarInteger() const {
147  return isSimple() ? V.isScalarInteger() : isExtendedScalarInteger();
148  }
149 
150  /// Return true if this is a vector value type.
151  bool isVector() const {
152  return isSimple() ? V.isVector() : isExtendedVector();
153  }
154 
155  /// Return true if this is a vector type where the runtime
156  /// length is machine dependent
157  bool isScalableVector() const {
158  // FIXME: We don't support extended scalable types yet, because the
159  // matching IR type doesn't exist. Once it has been added, this can
160  // be changed to call isExtendedScalableVector.
161  if (!isSimple())
162  return false;
163  return V.isScalableVector();
164  }
165 
166  /// Return true if this is a 16-bit vector type.
167  bool is16BitVector() const {
168  return isSimple() ? V.is16BitVector() : isExtended16BitVector();
169  }
170 
171  /// Return true if this is a 32-bit vector type.
172  bool is32BitVector() const {
173  return isSimple() ? V.is32BitVector() : isExtended32BitVector();
174  }
175 
176  /// Return true if this is a 64-bit vector type.
177  bool is64BitVector() const {
178  return isSimple() ? V.is64BitVector() : isExtended64BitVector();
179  }
180 
181  /// Return true if this is a 128-bit vector type.
182  bool is128BitVector() const {
183  return isSimple() ? V.is128BitVector() : isExtended128BitVector();
184  }
185 
186  /// Return true if this is a 256-bit vector type.
187  bool is256BitVector() const {
188  return isSimple() ? V.is256BitVector() : isExtended256BitVector();
189  }
190 
191  /// Return true if this is a 512-bit vector type.
192  bool is512BitVector() const {
193  return isSimple() ? V.is512BitVector() : isExtended512BitVector();
194  }
195 
196  /// Return true if this is a 1024-bit vector type.
197  bool is1024BitVector() const {
198  return isSimple() ? V.is1024BitVector() : isExtended1024BitVector();
199  }
200 
201  /// Return true if this is a 2048-bit vector type.
202  bool is2048BitVector() const {
203  return isSimple() ? V.is2048BitVector() : isExtended2048BitVector();
204  }
205 
206  /// Return true if this is an overloaded type for TableGen.
207  bool isOverloaded() const {
208  return (V==MVT::iAny || V==MVT::fAny || V==MVT::vAny || V==MVT::iPTRAny);
209  }
210 
211  /// Return true if the bit size is a multiple of 8.
212  bool isByteSized() const {
213  return getSizeInBits().isByteSized();
214  }
215 
216  /// Return true if the size is a power-of-two number of bytes.
217  bool isRound() const {
218  if (isScalableVector())
219  return false;
220  unsigned BitSize = getSizeInBits();
221  return BitSize >= 8 && !(BitSize & (BitSize - 1));
222  }
223 
224  /// Return true if this has the same number of bits as VT.
225  bool bitsEq(EVT VT) const {
226  if (EVT::operator==(VT)) return true;
227  return getSizeInBits() == VT.getSizeInBits();
228  }
229 
230  /// Return true if this has more bits than VT.
231  bool bitsGT(EVT VT) const {
232  if (EVT::operator==(VT)) return false;
233  return getSizeInBits() > VT.getSizeInBits();
234  }
235 
236  /// Return true if this has no less bits than VT.
237  bool bitsGE(EVT VT) const {
238  if (EVT::operator==(VT)) return true;
239  return getSizeInBits() >= VT.getSizeInBits();
240  }
241 
242  /// Return true if this has less bits than VT.
243  bool bitsLT(EVT VT) const {
244  if (EVT::operator==(VT)) return false;
245  return getSizeInBits() < VT.getSizeInBits();
246  }
247 
248  /// Return true if this has no more bits than VT.
249  bool bitsLE(EVT VT) const {
250  if (EVT::operator==(VT)) return true;
251  return getSizeInBits() <= VT.getSizeInBits();
252  }
253 
254  /// Return the SimpleValueType held in the specified simple EVT.
255  MVT getSimpleVT() const {
256  assert(isSimple() && "Expected a SimpleValueType!");
257  return V;
258  }
259 
260  /// If this is a vector type, return the element type, otherwise return
261  /// this.
262  EVT getScalarType() const {
263  return isVector() ? getVectorElementType() : *this;
264  }
265 
266  /// Given a vector type, return the type of each element.
268  assert(isVector() && "Invalid vector type!");
269  if (isSimple())
270  return V.getVectorElementType();
271  return getExtendedVectorElementType();
272  }
273 
274  /// Given a vector type, return the number of elements it contains.
275  unsigned getVectorNumElements() const {
276  assert(isVector() && "Invalid vector type!");
277  if (isSimple())
278  return V.getVectorNumElements();
279  return getExtendedVectorNumElements();
280  }
281 
282  // Given a (possibly scalable) vector type, return the ElementCount
284  assert((isVector()) && "Invalid vector type!");
285  if (isSimple())
286  return V.getVectorElementCount();
287 
289  "We don't support extended scalable types yet");
290  return {getExtendedVectorNumElements(), false};
291  }
292 
293  /// Return the size of the specified value type in bits.
294  ///
295  /// If the value type is a scalable vector type, the scalable property will
296  /// be set and the runtime size will be a positive integer multiple of the
297  /// base size.
299  if (isSimple())
300  return V.getSizeInBits();
301  return getExtendedSizeInBits();
302  }
303 
305  return getScalarType().getSizeInBits();
306  }
307 
308  /// Return the number of bytes overwritten by a store of the specified value
309  /// type.
310  ///
311  /// If the value type is a scalable vector type, the scalable property will
312  /// be set and the runtime size will be a positive integer multiple of the
313  /// base size.
315  TypeSize BaseSize = getSizeInBits();
316  return {(BaseSize.getKnownMinSize() + 7) / 8, BaseSize.isScalable()};
317  }
318 
319  /// Return the number of bits overwritten by a store of the specified value
320  /// type.
321  ///
322  /// If the value type is a scalable vector type, the scalable property will
323  /// be set and the runtime size will be a positive integer multiple of the
324  /// base size.
326  return getStoreSize() * 8;
327  }
328 
329  /// Rounds the bit-width of the given integer EVT up to the nearest power of
330  /// two (and at least to eight), and returns the integer EVT with that
331  /// number of bits.
333  assert(isInteger() && !isVector() && "Invalid integer type!");
334  unsigned BitWidth = getSizeInBits();
335  if (BitWidth <= 8)
336  return EVT(MVT::i8);
337  return getIntegerVT(Context, 1 << Log2_32_Ceil(BitWidth));
338  }
339 
340  /// Finds the smallest simple value type that is greater than or equal to
341  /// half the width of this EVT. If no simple value type can be found, an
342  /// extended integer value type of half the size (rounded up) is returned.
344  assert(isInteger() && !isVector() && "Invalid integer type!");
345  unsigned EVTSize = getSizeInBits();
346  for (unsigned IntVT = MVT::FIRST_INTEGER_VALUETYPE;
347  IntVT <= MVT::LAST_INTEGER_VALUETYPE; ++IntVT) {
348  EVT HalfVT = EVT((MVT::SimpleValueType)IntVT);
349  if (HalfVT.getSizeInBits() * 2 >= EVTSize)
350  return HalfVT;
351  }
352  return getIntegerVT(Context, (EVTSize + 1) / 2);
353  }
354 
355  /// Return a VT for an integer vector type with the size of the
356  /// elements doubled. The typed returned may be an extended type.
358  EVT EltVT = getVectorElementType();
359  EltVT = EVT::getIntegerVT(Context, 2 * EltVT.getSizeInBits());
360  return EVT::getVectorVT(Context, EltVT, getVectorElementCount());
361  }
362 
363  // Return a VT for a vector type with the same element type but
364  // half the number of elements. The type returned may be an
365  // extended type.
367  EVT EltVT = getVectorElementType();
368  auto EltCnt = getVectorElementCount();
369  assert(!(EltCnt.Min & 1) && "Splitting vector, but not in half!");
370  return EVT::getVectorVT(Context, EltVT, EltCnt / 2);
371  }
372 
373  /// Returns true if the given vector is a power of 2.
374  bool isPow2VectorType() const {
375  unsigned NElts = getVectorNumElements();
376  return !(NElts & (NElts - 1));
377  }
378 
379  /// Widens the length of the given vector EVT up to the nearest power of 2
380  /// and returns that type.
382  if (!isPow2VectorType()) {
383  unsigned NElts = getVectorNumElements();
384  unsigned Pow2NElts = 1 << Log2_32_Ceil(NElts);
385  return EVT::getVectorVT(Context, getVectorElementType(), Pow2NElts,
386  isScalableVector());
387  }
388  else {
389  return *this;
390  }
391  }
392 
393  /// This function returns value type as a string, e.g. "i32".
394  std::string getEVTString() const;
395 
396  /// This method returns an LLVM type corresponding to the specified EVT.
397  /// For integer types, this returns an unsigned type. Note that this will
398  /// abort for types that cannot be represented.
400 
401  /// Return the value type corresponding to the specified type.
402  /// This returns all pointers as iPTR. If HandleUnknown is true, unknown
403  /// types are returned as Other, otherwise they are invalid.
404  static EVT getEVT(Type *Ty, bool HandleUnknown = false);
405 
407  if (isSimple())
408  return V.SimpleTy;
409  else
410  return (intptr_t)(LLVMTy);
411  }
412 
413  /// A meaningless but well-behaved order, useful for constructing
414  /// containers.
415  struct compareRawBits {
416  bool operator()(EVT L, EVT R) const {
417  if (L.V.SimpleTy == R.V.SimpleTy)
418  return L.LLVMTy < R.LLVMTy;
419  else
420  return L.V.SimpleTy < R.V.SimpleTy;
421  }
422  };
423 
424  private:
425  // Methods for handling the Extended-type case in functions above.
426  // These are all out-of-line to prevent users of this header file
427  // from having a dependency on Type.h.
428  EVT changeExtendedTypeToInteger() const;
429  EVT changeExtendedVectorElementTypeToInteger() const;
430  static EVT getExtendedIntegerVT(LLVMContext &C, unsigned BitWidth);
431  static EVT getExtendedVectorVT(LLVMContext &C, EVT VT,
432  unsigned NumElements);
433  bool isExtendedFloatingPoint() const LLVM_READONLY;
434  bool isExtendedInteger() const LLVM_READONLY;
435  bool isExtendedScalarInteger() const LLVM_READONLY;
436  bool isExtendedVector() const LLVM_READONLY;
437  bool isExtended16BitVector() const LLVM_READONLY;
438  bool isExtended32BitVector() const LLVM_READONLY;
439  bool isExtended64BitVector() const LLVM_READONLY;
440  bool isExtended128BitVector() const LLVM_READONLY;
441  bool isExtended256BitVector() const LLVM_READONLY;
442  bool isExtended512BitVector() const LLVM_READONLY;
443  bool isExtended1024BitVector() const LLVM_READONLY;
444  bool isExtended2048BitVector() const LLVM_READONLY;
445  EVT getExtendedVectorElementType() const;
446  unsigned getExtendedVectorNumElements() const LLVM_READONLY;
447  TypeSize getExtendedSizeInBits() const LLVM_READONLY;
448  };
449 
450 } // end namespace llvm
451 
452 #endif // LLVM_CODEGEN_VALUETYPES_H
uint64_t CallInst * C
static MVT getIntegerVT(unsigned BitWidth)
unsigned Log2_32_Ceil(uint32_t Value)
Return the ceil log base 2 of the specified value, 32 if the value is zero.
Definition: MathExtras.h:598
bool isScalable() const
Definition: TypeSize.h:137
bool isInteger() const
Return true if this is an integer or a vector integer type.
LLVMContext & Context
bool isPow2VectorType() const
Returns true if the given vector is a power of 2.
Definition: ValueTypes.h:374
ElementCount getVectorElementCount() const
This class represents lattice values for constants.
Definition: AllocatorList.h:23
static MVT getVectorVT(MVT VT, unsigned NumElements)
EVT getScalarType() const
If this is a vector type, return the element type, otherwise return this.
Definition: ValueTypes.h:262
bool operator==(EVT VT) const
Definition: ValueTypes.h:44
bool isVector() const
Return true if this is a vector value type.
TypeSize getSizeInBits() const
Returns the size of the specified MVT in bits.
EVT getHalfNumVectorElementsVT(LLVMContext &Context) const
Definition: ValueTypes.h:366
bool is256BitVector() const
Return true if this is a 256-bit vector type.
bool isScalarInteger() const
Return true if this is an integer, but not a vector.
Definition: ValueTypes.h:146
MVT getSimpleVT() const
Return the SimpleValueType held in the specified simple EVT.
Definition: ValueTypes.h:255
static MVT getFloatingPointVT(unsigned BitWidth)
unsigned getVectorNumElements() const
bool isScalableVector() const
Return true if this is a vector type where the runtime length is machine dependent.
Definition: ValueTypes.h:157
bool isInteger() const
Return true if this is an integer or a vector integer type.
Definition: ValueTypes.h:141
static EVT getVectorVT(LLVMContext &Context, EVT VT, ElementCount EC)
Returns the EVT that represents a vector EC.Min elements in length, where each element is of type VT...
Definition: ValueTypes.h:85
bool isByteSized() const
Return true if the bit size is a multiple of 8.
Definition: ValueTypes.h:212
unsigned Min
Definition: TypeSize.h:25
EVT getPow2VectorType(LLVMContext &Context) const
Widens the length of the given vector EVT up to the nearest power of 2 and returns that type...
Definition: ValueTypes.h:381
bool is16BitVector() const
Return true if this is a 16-bit vector type.
Definition: ValueTypes.h:167
bool isFloatingPoint() const
Return true if this is a FP or a vector FP type.
Definition: ValueTypes.h:136
constexpr EVT(MVT S)
Definition: ValueTypes.h:42
Type * getTypeForEVT(LLVMContext &Context) const
This method returns an LLVM type corresponding to the specified EVT.
Definition: ValueTypes.cpp:140
SimpleValueType SimpleTy
bool is64BitVector() const
Return true if this is a 64-bit vector type.
bool operator!=(EVT VT) const
Definition: ValueTypes.h:47
A meaningless but well-behaved order, useful for constructing containers.
Definition: ValueTypes.h:415
bool is1024BitVector() const
Return true if this is a 1024-bit vector type.
bool is2048BitVector() const
Return true if this is a 2048-bit vector type.
bool is16BitVector() const
Return true if this is a 16-bit vector type.
MVT getVectorElementType() const
bool isScalableVector() const
Return true if this is a vector value type where the runtime length is machine dependent.
bool is128BitVector() const
Return true if this is a 128-bit vector type.
bool bitsGT(EVT VT) const
Return true if this has more bits than VT.
Definition: ValueTypes.h:231
bool bitsGE(EVT VT) const
Return true if this has no less bits than VT.
Definition: ValueTypes.h:237
bool operator()(EVT L, EVT R) const
Definition: ValueTypes.h:416
Machine Value Type.
The instances of the Type class are immutable: once they are created, they are never changed...
Definition: Type.h:46
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:63
bool is512BitVector() const
Return true if this is a 512-bit vector type.
Definition: ValueTypes.h:192
unsigned getVectorNumElements() const
Given a vector type, return the number of elements it contains.
Definition: ValueTypes.h:275
bool isRound() const
Return true if the size is a power-of-two number of bytes.
Definition: ValueTypes.h:217
bool bitsLE(EVT VT) const
Return true if this has no more bits than VT.
Definition: ValueTypes.h:249
bool isScalarInteger() const
Return true if this is an integer, not including vectors.
bool isByteSized() const
Definition: TypeSize.h:142
static EVT getFloatingPointVT(unsigned BitWidth)
Returns the EVT that represents a floating-point type with the given number of bits.
Definition: ValueTypes.h:58
TypeSize getStoreSize() const
Return the number of bytes overwritten by a store of the specified value type.
Definition: ValueTypes.h:314
std::string getEVTString() const
This function returns value type as a string, e.g. "i32".
Definition: ValueTypes.cpp:115
bool is32BitVector() const
Return true if this is a 32-bit vector type.
Definition: ValueTypes.h:172
Extended Value Type.
Definition: ValueTypes.h:34
bool is2048BitVector() const
Return true if this is a 2048-bit vector type.
Definition: ValueTypes.h:202
TypeSize getSizeInBits() const
Return the size of the specified value type in bits.
Definition: ValueTypes.h:298
bool isFloatingPoint() const
Return true if this is a FP or a vector FP type.
EVT getRoundIntegerType(LLVMContext &Context) const
Rounds the bit-width of the given integer EVT up to the nearest power of two (and at least to eight)...
Definition: ValueTypes.h:332
EVT getVectorElementType() const
Given a vector type, return the type of each element.
Definition: ValueTypes.h:267
EVT changeVectorElementTypeToInteger() const
Return a vector with the same number of elements as this vector, but with the element type converted ...
Definition: ValueTypes.h:96
EVT widenIntegerVectorElementType(LLVMContext &Context) const
Return a VT for an integer vector type with the size of the elements doubled.
Definition: ValueTypes.h:357
constexpr EVT()=default
static EVT getVectorVT(LLVMContext &Context, EVT VT, unsigned NumElements, bool IsScalable=false)
Returns the EVT that represents a vector NumElements in length, where each element is of type VT...
Definition: ValueTypes.h:73
bool bitsLT(EVT VT) const
Return true if this has less bits than VT.
Definition: ValueTypes.h:243
bool isOverloaded() const
Return true if this is an overloaded type for TableGen.
Definition: ValueTypes.h:207
bool is32BitVector() const
Return true if this is a 32-bit vector type.
ElementCount getVectorElementCount() const
Definition: ValueTypes.h:283
bool is256BitVector() const
Return true if this is a 256-bit vector type.
Definition: ValueTypes.h:187
bool is64BitVector() const
Return true if this is a 64-bit vector type.
Definition: ValueTypes.h:177
bool isVector() const
Return true if this is a vector value type.
Definition: ValueTypes.h:151
bool is128BitVector() const
Return true if this is a 128-bit vector type.
Definition: ValueTypes.h:182
uint64_t getKnownMinSize() const
Definition: TypeSize.h:132
bool is512BitVector() const
Return true if this is a 512-bit vector type.
#define LLVM_READONLY
Definition: Compiler.h:210
static EVT getEVT(Type *Ty, bool HandleUnknown=false)
Return the value type corresponding to the specified type.
Definition: ValueTypes.cpp:353
bool is1024BitVector() const
Return true if this is a 1024-bit vector type.
Definition: ValueTypes.h:197
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
TypeSize getScalarSizeInBits() const
Definition: ValueTypes.h:304
EVT changeTypeToInteger()
Return the type converted to an equivalently sized integer or vector with integer element type...
Definition: ValueTypes.h:115
intptr_t getRawBits() const
Definition: ValueTypes.h:406
bool bitsEq(EVT VT) const
Return true if this has the same number of bits as VT.
Definition: ValueTypes.h:225
bool isSimple() const
Test if the given EVT is simple (as opposed to being extended).
Definition: ValueTypes.h:126
TypeSize getStoreSizeInBits() const
Return the number of bits overwritten by a store of the specified value type.
Definition: ValueTypes.h:325
static EVT getIntegerVT(LLVMContext &Context, unsigned BitWidth)
Returns the EVT that represents an integer with the given number of bits.
Definition: ValueTypes.h:64
bool isExtended() const
Test if the given EVT is extended (as opposed to being simple).
Definition: ValueTypes.h:131
constexpr EVT(MVT::SimpleValueType SVT)
Definition: ValueTypes.h:41
EVT getHalfSizedIntegerVT(LLVMContext &Context) const
Finds the smallest simple value type that is greater than or equal to half the width of this EVT...
Definition: ValueTypes.h:343