2 //===- CodeGen/ValueTypes.h - Low-Level Target independ. types --*- C++ -*-===//
4 // The LLVM Compiler Infrastructure
6 // This file is distributed under the University of Illinois Open Source
7 // License. See LICENSE.TXT for details.
9 //===----------------------------------------------------------------------===//
11 // This file defines the set of low-level target independent types which various
12 // values in the code generator are. This allows the target specific behavior
13 // of instructions to be described to target independent passes.
15 //===----------------------------------------------------------------------===//
17 #ifndef LLVM_CODEGEN_VALUETYPES_H
18 #define LLVM_CODEGEN_VALUETYPES_H
22 #include "llvm/Support/DataTypes.h"
23 #include "llvm/Support/MathExtras.h"
28 struct MVT { // MVT = Machine Value Type
30 enum SimpleValueType {
31 // If you change this numbering, you must change the values in
32 // ValueTypes.td as well!
33 Other = 0, // This is a non-standard value
34 i1 = 1, // This is a 1 bit integer value
35 i8 = 2, // This is an 8 bit integer value
36 i16 = 3, // This is a 16 bit integer value
37 i32 = 4, // This is a 32 bit integer value
38 i64 = 5, // This is a 64 bit integer value
39 i128 = 6, // This is a 128 bit integer value
41 FIRST_INTEGER_VALUETYPE = i1,
42 LAST_INTEGER_VALUETYPE = i128,
44 f32 = 7, // This is a 32 bit floating point value
45 f64 = 8, // This is a 64 bit floating point value
46 f80 = 9, // This is a 80 bit floating point value
47 f128 = 10, // This is a 128 bit floating point value
48 ppcf128 = 11, // This is a PPC 128-bit floating point value
49 Flag = 12, // This is a condition code or machine flag.
51 isVoid = 13, // This has no value
55 v2i16 = 16, // 2 x i16
57 v4i16 = 18, // 4 x i16
58 v2i32 = 19, // 2 x i32
59 v1i64 = 20, // 1 x i64
60 v16i8 = 21, // 16 x i8
61 v8i16 = 22, // 8 x i16
62 v3i32 = 23, // 3 x i32
63 v4i32 = 24, // 4 x i32
64 v2i64 = 25, // 2 x i64
66 v2f32 = 26, // 2 x f32
67 v3f32 = 27, // 3 x f32
68 v4f32 = 28, // 4 x f32
69 v2f64 = 29, // 2 x f64
71 FIRST_VECTOR_VALUETYPE = v2i8,
72 LAST_VECTOR_VALUETYPE = v2f64,
74 LAST_VALUETYPE = 30, // This always remains at the end of the list.
76 // This is the current maximum for LAST_VALUETYPE.
77 // Affects ValueTypeActions in TargetLowering.h.
78 // MVT::MAX_ALLOWED_VALUETYPE is used for asserts and to size bit vectors
79 MAX_ALLOWED_VALUETYPE = 64,
81 // iPTRAny - An int value the size of the pointer of the current
82 // target to any address space. This must only be used internal to
83 // tblgen. Other than for overloading, we treat iPTRAny the same as iPTR.
86 // fAny - Any floating-point or vector floating-point value. This is used
87 // for intrinsics that have overloadings based on floating-point types.
88 // This is only for tblgen's consumption!
91 // iAny - An integer or vector integer value of any bit width. This is
92 // used for intrinsics that have overloadings based on integer bit widths.
93 // This is only for tblgen's consumption!
96 // iPTR - An int value the size of the pointer of the current
97 // target. This should only be used internal to tblgen!
100 // LastSimpleValueType - The greatest valid SimpleValueType value.
101 LastSimpleValueType = 255
105 /// This union holds low-level value types. Valid values include any of
106 /// the values in the SimpleValueType enum, or any value returned from one
107 /// of the MVT methods. Any value type equal to one of the SimpleValueType
108 /// enum values is a "simple" value type. All others are "extended".
110 /// Note that simple doesn't necessary mean legal for the target machine.
111 /// All legal value types must be simple, but often there are some simple
112 /// value types that are not legal.
121 MVT(SimpleValueType S) : V(S) {}
123 bool operator==(const MVT VT) const {
124 return getRawBits() == VT.getRawBits();
126 bool operator!=(const MVT VT) const {
127 return getRawBits() != VT.getRawBits();
130 /// getFloatingPointVT - Returns the MVT that represents a floating point
131 /// type with the given number of bits. There are two floating point types
132 /// with 128 bits - this returns f128 rather than ppcf128.
133 static MVT getFloatingPointVT(unsigned BitWidth) {
136 assert(false && "Bad bit width!");
148 /// getIntegerVT - Returns the MVT that represents an integer with the given
150 static MVT getIntegerVT(unsigned BitWidth) {
167 return getExtendedIntegerVT(BitWidth);
170 /// getVectorVT - Returns the MVT that represents a vector NumElements in
171 /// length, where each element is of type VT.
172 static MVT getVectorVT(MVT VT, unsigned NumElements) {
177 if (NumElements == 2) return v2i8;
178 if (NumElements == 4) return v4i8;
179 if (NumElements == 8) return v8i8;
180 if (NumElements == 16) return v16i8;
183 if (NumElements == 2) return v2i16;
184 if (NumElements == 4) return v4i16;
185 if (NumElements == 8) return v8i16;
188 if (NumElements == 2) return v2i32;
189 if (NumElements == 3) return v3i32;
190 if (NumElements == 4) return v4i32;
193 if (NumElements == 1) return v1i64;
194 if (NumElements == 2) return v2i64;
197 if (NumElements == 2) return v2f32;
198 if (NumElements == 3) return v3f32;
199 if (NumElements == 4) return v4f32;
202 if (NumElements == 2) return v2f64;
205 return getExtendedVectorVT(VT, NumElements);
208 /// getIntVectorWithNumElements - Return any integer vector type that has
209 /// the specified number of elements.
210 static MVT getIntVectorWithNumElements(unsigned NumElts) {
212 default: return getVectorVT(i8, NumElts);
213 case 1: return v1i64;
214 case 2: return v2i32;
215 case 3: return v3i32;
216 case 4: return v4i16;
218 case 16: return v16i8;
222 /// isSimple - Test if the given MVT is simple (as opposed to being
224 bool isSimple() const {
225 return V <= LastSimpleValueType;
228 /// isExtended - Test if the given MVT is extended (as opposed to
230 bool isExtended() const {
234 /// isFloatingPoint - Return true if this is a FP, or a vector FP type.
235 bool isFloatingPoint() const {
237 ((V >= f32 && V <= ppcf128) || (V >= v2f32 && V <= v2f64)) :
238 isExtendedFloatingPoint();
241 /// isInteger - Return true if this is an integer, or a vector integer type.
242 bool isInteger() const {
244 ((V >= FIRST_INTEGER_VALUETYPE && V <= LAST_INTEGER_VALUETYPE) ||
245 (V >= v2i8 && V <= v2i64)) : isExtendedInteger();
248 /// isVector - Return true if this is a vector value type.
249 bool isVector() const {
251 (V >= FIRST_VECTOR_VALUETYPE && V <= LAST_VECTOR_VALUETYPE) :
255 /// is64BitVector - Return true if this is a 64-bit vector type.
256 bool is64BitVector() const {
258 (V==v8i8 || V==v4i16 || V==v2i32 || V==v1i64 || V==v2f32) :
259 isExtended64BitVector();
262 /// is128BitVector - Return true if this is a 128-bit vector type.
263 bool is128BitVector() const {
265 (V==v16i8 || V==v8i16 || V==v4i32 ||
266 V==v2i64 || V==v4f32 || V==v2f64) :
267 isExtended128BitVector();
270 /// isByteSized - Return true if the bit size is a multiple of 8.
271 bool isByteSized() const {
272 return (getSizeInBits() & 7) == 0;
275 /// isRound - Return true if the size is a power-of-two number of bytes.
276 bool isRound() const {
277 unsigned BitSize = getSizeInBits();
278 return BitSize >= 8 && !(BitSize & (BitSize - 1));
281 /// bitsEq - Return true if this has the same number of bits as VT.
282 bool bitsEq(MVT VT) const {
283 return getSizeInBits() == VT.getSizeInBits();
286 /// bitsGT - Return true if this has more bits than VT.
287 bool bitsGT(MVT VT) const {
288 return getSizeInBits() > VT.getSizeInBits();
291 /// bitsGE - Return true if this has no less bits than VT.
292 bool bitsGE(MVT VT) const {
293 return getSizeInBits() >= VT.getSizeInBits();
296 /// bitsLT - Return true if this has less bits than VT.
297 bool bitsLT(MVT VT) const {
298 return getSizeInBits() < VT.getSizeInBits();
301 /// bitsLE - Return true if this has no more bits than VT.
302 bool bitsLE(MVT VT) const {
303 return getSizeInBits() <= VT.getSizeInBits();
307 /// getSimpleVT - Return the SimpleValueType held in the specified
309 SimpleValueType getSimpleVT() const {
310 assert(isSimple() && "Expected a SimpleValueType!");
311 return SimpleValueType(V);
314 /// getVectorElementType - Given a vector type, return the type of
316 MVT getVectorElementType() const {
317 assert(isVector() && "Invalid vector type!");
320 return getExtendedVectorElementType();
324 case v16i8: return i8;
327 case v8i16: return i16;
330 case v4i32: return i32;
332 case v2i64: return i64;
335 case v4f32: return f32;
336 case v2f64: return f64;
340 /// getVectorNumElements - Given a vector type, return the number of
341 /// elements it contains.
342 unsigned getVectorNumElements() const {
343 assert(isVector() && "Invalid vector type!");
346 return getExtendedVectorNumElements();
347 case v16i8: return 16;
349 case v8i16: return 8;
353 case v4f32: return 4;
355 case v3f32: return 3;
361 case v2f64: return 2;
362 case v1i64: return 1;
366 /// getSizeInBits - Return the size of the specified value type in bits.
367 unsigned getSizeInBits() const {
370 assert(0 && "Value type size is target-dependent. Ask TLI.");
374 assert(0 && "Value type is overloaded.");
376 return getExtendedSizeInBits();
380 case v2i8: return 16;
384 case v2i16: return 32;
391 case v2f32: return 64;
392 case f80 : return 80;
394 case v3f32: return 96;
403 case v2f64: return 128;
407 /// getStoreSizeInBits - Return the number of bits overwritten by a store
408 /// of the specified value type.
409 unsigned getStoreSizeInBits() const {
410 return (getSizeInBits() + 7)/8*8;
413 /// getRoundIntegerType - Rounds the bit-width of the given integer MVT up
414 /// to the nearest power of two (and at least to eight), and returns the
415 /// integer MVT with that number of bits.
416 MVT getRoundIntegerType() const {
417 assert(isInteger() && !isVector() && "Invalid integer type!");
418 unsigned BitWidth = getSizeInBits();
422 return getIntegerVT(1 << Log2_32_Ceil(BitWidth));
425 /// isPow2VectorType - Retuns true if the given vector is a power of 2.
426 bool isPow2VectorType() const {
427 unsigned NElts = getVectorNumElements();
428 return !(NElts & (NElts - 1));
431 /// getPow2VectorType - Widens the length of the given vector MVT up to
432 /// the nearest power of 2 and returns that type.
433 MVT getPow2VectorType() const {
434 if (!isPow2VectorType()) {
435 unsigned NElts = getVectorNumElements();
436 unsigned Pow2NElts = 1 << Log2_32_Ceil(NElts);
437 return MVT::getVectorVT(getVectorElementType(), Pow2NElts);
444 /// getMVTString - This function returns value type as a string,
446 std::string getMVTString() const;
448 /// getTypeForMVT - This method returns an LLVM type corresponding to the
449 /// specified MVT. For integer types, this returns an unsigned type. Note
450 /// that this will abort for types that cannot be represented.
451 const Type *getTypeForMVT() const;
453 /// getMVT - Return the value type corresponding to the specified type.
454 /// This returns all pointers as iPTR. If HandleUnknown is true, unknown
455 /// types are returned as Other, otherwise they are invalid.
456 static MVT getMVT(const Type *Ty, bool HandleUnknown = false);
458 /// getRawBits - Represent the type as a bunch of bits.
459 uintptr_t getRawBits() const { return V; }
461 /// compareRawBits - A meaningless but well-behaved order, useful for
462 /// constructing containers.
463 struct compareRawBits {
464 bool operator()(MVT L, MVT R) const {
465 return L.getRawBits() < R.getRawBits();
470 // Methods for handling the Extended-type case in functions above.
471 // These are all out-of-line to prevent users of this header file
472 // from having a dependency on Type.h.
473 static MVT getExtendedIntegerVT(unsigned BitWidth);
474 static MVT getExtendedVectorVT(MVT VT, unsigned NumElements);
475 bool isExtendedFloatingPoint() const;
476 bool isExtendedInteger() const;
477 bool isExtendedVector() const;
478 bool isExtended64BitVector() const;
479 bool isExtended128BitVector() const;
480 MVT getExtendedVectorElementType() const;
481 unsigned getExtendedVectorNumElements() const;
482 unsigned getExtendedSizeInBits() const;
485 } // End llvm namespace