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