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