1 //===- CodeGen/ValueTypes.h - Low-Level Target independ. types --*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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.
14 //===----------------------------------------------------------------------===//
16 #ifndef LLVM_CODEGEN_VALUETYPES_H
17 #define LLVM_CODEGEN_VALUETYPES_H
21 #include "llvm/Support/DataTypes.h"
22 #include "llvm/Support/MathExtras.h"
27 struct MVT { // MVT = Machine Value Type
29 enum SimpleValueType {
30 // If you change this numbering, you must change the values in
31 // ValueTypes.td as well!
32 Other = 0, // This is a non-standard value
33 i1 = 1, // This is a 1 bit integer value
34 i8 = 2, // This is an 8 bit integer value
35 i16 = 3, // This is a 16 bit integer value
36 i32 = 4, // This is a 32 bit integer value
37 i64 = 5, // This is a 64 bit integer value
38 i128 = 6, // This is a 128 bit integer value
40 FIRST_INTEGER_VALUETYPE = i1,
41 LAST_INTEGER_VALUETYPE = i128,
43 f32 = 7, // This is a 32 bit floating point value
44 f64 = 8, // This is a 64 bit floating point value
45 f80 = 9, // This is a 80 bit floating point value
46 f128 = 10, // This is a 128 bit floating point value
47 ppcf128 = 11, // This is a PPC 128-bit floating point value
48 Flag = 12, // This is a condition code or machine flag.
50 isVoid = 13, // This has no value
53 v4i16 = 15, // 4 x i16
54 v2i32 = 16, // 2 x i32
55 v1i64 = 17, // 1 x i64
56 v16i8 = 18, // 16 x i8
57 v8i16 = 19, // 8 x i16
58 v3i32 = 20, // 3 x i32
59 v4i32 = 21, // 4 x i32
60 v2i64 = 22, // 2 x i64
62 v2f32 = 23, // 2 x f32
63 v3f32 = 24, // 3 x f32
64 v4f32 = 25, // 4 x f32
65 v2f64 = 26, // 2 x f64
67 FIRST_VECTOR_VALUETYPE = v8i8,
68 LAST_VECTOR_VALUETYPE = v2f64,
70 LAST_VALUETYPE = 27, // This always remains at the end of the list.
72 // iPTRAny - An int value the size of the pointer of the current
73 // target to any address space. This must only be used internal to
74 // tblgen. Other than for overloading, we treat iPTRAny the same as iPTR.
77 // fAny - Any floating-point or vector floating-point value. This is used
78 // for intrinsics that have overloadings based on floating-point types.
79 // This is only for tblgen's consumption!
82 // iAny - An integer or vector integer value of any bit width. This is
83 // used for intrinsics that have overloadings based on integer bit widths.
84 // This is only for tblgen's consumption!
87 // iPTR - An int value the size of the pointer of the current
88 // target. This should only be used internal to tblgen!
91 // LastSimpleValueType - The greatest valid SimpleValueType value.
92 LastSimpleValueType = 255
96 /// This union holds low-level value types. Valid values include any of
97 /// the values in the SimpleValueType enum, or any value returned from one
98 /// of the MVT methods. Any value type equal to one of the SimpleValueType
99 /// enum values is a "simple" value type. All others are "extended".
101 /// Note that simple doesn't necessary mean legal for the target machine.
102 /// All legal value types must be simple, but often there are some simple
103 /// value types that are not legal.
107 SimpleValueType SimpleTy;
113 MVT(SimpleValueType S) : V(S) {}
115 bool operator==(const MVT VT) const {
116 return getRawBits() == VT.getRawBits();
118 bool operator!=(const MVT VT) const {
119 return getRawBits() != VT.getRawBits();
122 /// getFloatingPointVT - Returns the MVT that represents a floating point
123 /// type with the given number of bits. There are two floating point types
124 /// with 128 bits - this returns f128 rather than ppcf128.
125 static MVT getFloatingPointVT(unsigned BitWidth) {
128 assert(false && "Bad bit width!");
140 /// getIntegerVT - Returns the MVT that represents an integer with the given
142 static MVT getIntegerVT(unsigned BitWidth) {
159 return getExtendedIntegerVT(BitWidth);
162 /// getVectorVT - Returns the MVT that represents a vector NumElements in
163 /// length, where each element is of type VT.
164 static MVT getVectorVT(MVT VT, unsigned NumElements) {
169 if (NumElements == 8) return v8i8;
170 if (NumElements == 16) return v16i8;
173 if (NumElements == 4) return v4i16;
174 if (NumElements == 8) return v8i16;
177 if (NumElements == 2) return v2i32;
178 if (NumElements == 3) return v3i32;
179 if (NumElements == 4) return v4i32;
182 if (NumElements == 1) return v1i64;
183 if (NumElements == 2) return v2i64;
186 if (NumElements == 2) return v2f32;
187 if (NumElements == 3) return v3f32;
188 if (NumElements == 4) return v4f32;
191 if (NumElements == 2) return v2f64;
194 return getExtendedVectorVT(VT, NumElements);
197 /// getIntVectorWithNumElements - Return any integer vector type that has
198 /// the specified number of elements.
199 static MVT getIntVectorWithNumElements(unsigned NumElts) {
201 default: return getVectorVT(i8, NumElts);
202 case 1: return v1i64;
203 case 2: return v2i32;
204 case 3: return v3i32;
205 case 4: return v4i16;
207 case 16: return v16i8;
211 /// isSimple - Test if the given MVT is simple (as opposed to being
213 bool isSimple() const {
214 return V <= LastSimpleValueType;
217 /// isExtended - Test if the given MVT is extended (as opposed to
219 bool isExtended() const {
223 /// isFloatingPoint - Return true if this is a FP, or a vector FP type.
224 bool isFloatingPoint() const {
226 ((SimpleTy >= f32 && SimpleTy <= ppcf128) ||
227 (SimpleTy >= v2f32 && SimpleTy <= v2f64)) :
228 isExtendedFloatingPoint();
231 /// isInteger - Return true if this is an integer, or a vector integer type.
232 bool isInteger() const {
234 ((SimpleTy >= FIRST_INTEGER_VALUETYPE &&
235 SimpleTy <= LAST_INTEGER_VALUETYPE) ||
236 (SimpleTy >= v8i8 && SimpleTy <= v2i64)) :
240 /// isVector - Return true if this is a vector value type.
241 bool isVector() const {
243 (SimpleTy >= FIRST_VECTOR_VALUETYPE &&
244 SimpleTy <= LAST_VECTOR_VALUETYPE) :
248 /// is64BitVector - Return true if this is a 64-bit vector type.
249 bool is64BitVector() const {
251 (SimpleTy==v8i8 || SimpleTy==v4i16 || SimpleTy==v2i32 ||
252 SimpleTy==v1i64 || SimpleTy==v2f32) :
253 isExtended64BitVector();
256 /// is128BitVector - Return true if this is a 128-bit vector type.
257 bool is128BitVector() const {
259 (SimpleTy==v16i8 || SimpleTy==v8i16 || SimpleTy==v4i32 ||
260 SimpleTy==v2i64 || SimpleTy==v4f32 || SimpleTy==v2f64) :
261 isExtended128BitVector();
264 /// isByteSized - Return true if the bit size is a multiple of 8.
265 bool isByteSized() const {
266 return (getSizeInBits() & 7) == 0;
269 /// isRound - Return true if the size is a power-of-two number of bytes.
270 bool isRound() const {
271 unsigned BitSize = getSizeInBits();
272 return BitSize >= 8 && !(BitSize & (BitSize - 1));
275 /// bitsGT - Return true if this has more bits than VT.
276 bool bitsGT(MVT VT) const {
277 return getSizeInBits() > VT.getSizeInBits();
280 /// bitsGE - Return true if this has no less bits than VT.
281 bool bitsGE(MVT VT) const {
282 return getSizeInBits() >= VT.getSizeInBits();
285 /// bitsLT - Return true if this has less bits than VT.
286 bool bitsLT(MVT VT) const {
287 return getSizeInBits() < VT.getSizeInBits();
290 /// bitsLE - Return true if this has no more bits than VT.
291 bool bitsLE(MVT VT) const {
292 return getSizeInBits() <= VT.getSizeInBits();
296 /// getSimpleVT - Return the SimpleValueType held in the specified
298 SimpleValueType getSimpleVT() const {
299 assert(isSimple() && "Expected a SimpleValueType!");
303 /// getVectorElementType - Given a vector type, return the type of
305 MVT getVectorElementType() const {
306 assert(isVector() && "Invalid vector type!");
309 return getExtendedVectorElementType();
311 case v16i8: return i8;
313 case v8i16: return i16;
316 case v4i32: return i32;
318 case v2i64: return i64;
321 case v4f32: return f32;
322 case v2f64: return f64;
326 /// getVectorNumElements - Given a vector type, return the number of
327 /// elements it contains.
328 unsigned getVectorNumElements() const {
329 assert(isVector() && "Invalid vector type!");
332 return getExtendedVectorNumElements();
333 case v16i8: return 16;
335 case v8i16: return 8;
338 case v4f32: return 4;
340 case v3f32: return 3;
344 case v2f64: return 2;
345 case v1i64: return 1;
349 /// getSizeInBits - Return the size of the specified value type in bits.
350 unsigned getSizeInBits() const {
353 return getExtendedSizeInBits();
356 case i16 : return 16;
358 case i32 : return 32;
365 case v2f32: return 64;
366 case f80 : return 80;
368 case v3f32: return 96;
377 case v2f64: return 128;
379 assert(false && "Value type size is target-dependent. Ask TLI.");
383 assert(false && "Value type is overloaded.");
387 /// getStoreSizeInBits - Return the number of bits overwritten by a store
388 /// of the specified value type.
389 unsigned getStoreSizeInBits() const {
390 return (getSizeInBits() + 7)/8*8;
393 /// getRoundIntegerType - Rounds the bit-width of the given integer MVT up
394 /// to the nearest power of two (and at least to eight), and returns the
395 /// integer MVT with that number of bits.
396 MVT getRoundIntegerType() const {
397 assert(isInteger() && !isVector() && "Invalid integer type!");
398 unsigned BitWidth = getSizeInBits();
402 return getIntegerVT(1 << Log2_32_Ceil(BitWidth));
405 /// getIntegerVTBitMask - Return an integer with 1's every place there are
406 /// bits in the specified integer value type. FIXME: Should return an apint.
407 uint64_t getIntegerVTBitMask() const {
408 assert(isInteger() && !isVector() && "Only applies to int scalars!");
409 return ~uint64_t(0UL) >> (64-getSizeInBits());
412 /// getIntegerVTSignBit - Return an integer with a 1 in the position of the
413 /// sign bit for the specified integer value type. FIXME: Should return an
415 uint64_t getIntegerVTSignBit() const {
416 assert(isInteger() && !isVector() && "Only applies to int scalars!");
417 return uint64_t(1UL) << (getSizeInBits()-1);
420 /// getMVTString - This function returns value type as a string,
422 std::string getMVTString() const;
424 /// getTypeForMVT - This method returns an LLVM type corresponding to the
425 /// specified MVT. For integer types, this returns an unsigned type. Note
426 /// that this will abort for types that cannot be represented.
427 const Type *getTypeForMVT() const;
429 /// getMVT - Return the value type corresponding to the specified type.
430 /// This returns all pointers as iPTR. If HandleUnknown is true, unknown
431 /// types are returned as Other, otherwise they are invalid.
432 static MVT getMVT(const Type *Ty, bool HandleUnknown = false);
434 /// getRawBits - Represent the type as a bunch of bits.
435 uintptr_t getRawBits() const { return V; }
437 /// compareRawBits - A meaningless but well-behaved order, useful for
438 /// constructing containers.
439 struct compareRawBits {
440 bool operator()(MVT L, MVT R) const {
441 return L.getRawBits() < R.getRawBits();
446 // Methods for handling the Extended-type case in functions above.
447 // These are all out-of-line to prevent users of this header file
448 // from having a dependency on Type.h.
449 static MVT getExtendedIntegerVT(unsigned BitWidth);
450 static MVT getExtendedVectorVT(MVT VT, unsigned NumElements);
451 bool isExtendedFloatingPoint() const;
452 bool isExtendedInteger() const;
453 bool isExtendedVector() const;
454 bool isExtended64BitVector() const;
455 bool isExtended128BitVector() const;
456 MVT getExtendedVectorElementType() const;
457 unsigned getExtendedVectorNumElements() const;
458 unsigned getExtendedSizeInBits() const;
461 } // End llvm namespace