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
30 enum SimpleValueType {
31 // If you change this numbering, you must change the values in
32 // ValueTypes.td 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
54 v4i16 = 15, // 4 x i16
55 v2i32 = 16, // 2 x i32
56 v1i64 = 17, // 1 x i64
57 v16i8 = 18, // 16 x i8
58 v8i16 = 19, // 8 x i16
59 v3i32 = 20, // 3 x i32
60 v4i32 = 21, // 4 x i32
61 v2i64 = 22, // 2 x i64
63 v2f32 = 23, // 2 x f32
64 v3f32 = 24, // 3 x f32
65 v4f32 = 25, // 4 x f32
66 v2f64 = 26, // 2 x f64
68 FIRST_VECTOR_VALUETYPE = v8i8,
69 LAST_VECTOR_VALUETYPE = v2f64,
71 LAST_VALUETYPE = 27, // This always remains at the end of the list.
73 // fAny - Any floating-point or vector floating-point value. This is used
74 // for intrinsics that have overloadings based on floating-point types.
75 // This is only for tblgen's consumption!
78 // iAny - An integer or vector integer value of any bit width. This is
79 // used for intrinsics that have overloadings based on integer bit widths.
80 // This is only for tblgen's consumption!
83 // iPTR - An int value the size of the pointer of the current
84 // target. This should only be used internal to tblgen!
88 /// MVT - This type holds low-level value types. Valid values include any of
89 /// the values in the SimpleValueType enum, or any value returned from one
90 /// of the MVT methods. Any value type equal to one of the SimpleValueType
91 /// enum values is a "simple" value type. All others are "extended".
93 /// Note that simple doesn't necessary mean legal for the target machine.
94 /// All legal value types must be simple, but often there are some simple
95 /// value types that are not legal.
98 /// Extended types are either vector types or arbitrary precision integers.
99 /// Arbitrary precision integers have iAny in the first SimpleTypeBits bits,
100 /// and the bit-width in the next PrecisionBits bits, offset by minus one.
101 /// Vector types are encoded by having the first SimpleTypeBits+PrecisionBits
102 /// bits encode the vector element type (which must be a scalar type, possibly
103 /// an arbitrary precision integer) and the remaining VectorBits upper bits
104 /// encode the vector length, offset by one.
106 /// 32--------------16-----------8-------------0
107 /// | Vector length | Precision | Simple type |
108 /// | | Vector element |
113 static const int SimpleTypeBits = 8;
114 static const int PrecisionBits = 8;
115 static const int VectorBits = 32 - SimpleTypeBits - PrecisionBits;
117 static const uint32_t SimpleTypeMask =
118 (~uint32_t(0) << (32 - SimpleTypeBits)) >> (32 - SimpleTypeBits);
120 static const uint32_t PrecisionMask =
121 ((~uint32_t(0) << VectorBits) >> (32 - PrecisionBits)) << SimpleTypeBits;
123 static const uint32_t VectorMask =
124 (~uint32_t(0) >> (32 - VectorBits)) << (32 - VectorBits);
126 static const uint32_t ElementMask =
127 (~uint32_t(0) << VectorBits) >> VectorBits;
134 MVT(SimpleValueType S) { V = S; }
136 inline bool operator== (const MVT VT) const { return V == VT.V; }
137 inline bool operator!= (const MVT VT) const { return V != VT.V; }
139 /// getIntegerVT - Returns the MVT that represents an integer with the given
141 static inline MVT getIntegerVT(unsigned BitWidth) {
159 VT.V = iAny | (((BitWidth - 1) << SimpleTypeBits) & PrecisionMask);
160 assert(VT.getSizeInBits() == BitWidth && "Bad bit width!");
164 /// getVectorVT - Returns the MVT that represents a vector NumElements in
165 /// length, where each element is of type VT.
166 static inline MVT getVectorVT(MVT VT, unsigned NumElements) {
171 if (NumElements == 8) return v8i8;
172 if (NumElements == 16) return v16i8;
175 if (NumElements == 4) return v4i16;
176 if (NumElements == 8) return v8i16;
179 if (NumElements == 2) return v2i32;
180 if (NumElements == 3) return v3i32;
181 if (NumElements == 4) return v4i32;
184 if (NumElements == 1) return v1i64;
185 if (NumElements == 2) return v2i64;
188 if (NumElements == 2) return v2f32;
189 if (NumElements == 3) return v3f32;
190 if (NumElements == 4) return v4f32;
193 if (NumElements == 2) return v2f64;
197 Result.V = VT.V | ((NumElements + 1) << (32 - VectorBits));
198 assert(Result.getVectorElementType() == VT &&
199 "Bad vector element type!");
200 assert(Result.getVectorNumElements() == NumElements &&
201 "Bad vector length!");
205 /// getIntVectorWithNumElements - Return any integer vector type that has
206 /// the specified number of elements.
207 static inline MVT getIntVectorWithNumElements(unsigned NumElts) {
209 default: return getVectorVT(i8, NumElts);
210 case 1: return v1i64;
211 case 2: return v2i32;
212 case 3: return v3i32;
213 case 4: return v4i16;
215 case 16: return v16i8;
220 /// isSimple - Test if the given MVT is simple (as opposed to being
222 inline bool isSimple() const {
223 return V <= SimpleTypeMask;
226 /// isExtended - Test if the given MVT is extended (as opposed to
228 inline bool isExtended() const {
232 /// isFloatingPoint - Return true if this is a FP, or a vector FP type.
233 inline bool isFloatingPoint() const {
234 uint32_t SVT = V & SimpleTypeMask;
235 return (SVT >= f32 && SVT <= ppcf128) || (SVT >= v2f32 && SVT <= v2f64);
238 /// isInteger - Return true if this is an integer, or a vector integer type.
239 inline bool isInteger() const {
240 uint32_t SVT = V & SimpleTypeMask;
241 return (SVT >= FIRST_INTEGER_VALUETYPE && SVT <= LAST_INTEGER_VALUETYPE) ||
242 (SVT >= v8i8 && SVT <= v2i64) || (SVT == iAny && (V & PrecisionMask));
245 /// isVector - Return true if this is a vector value type.
246 inline bool isVector() const {
247 return (V >= FIRST_VECTOR_VALUETYPE && V <= LAST_VECTOR_VALUETYPE) ||
251 /// is64BitVector - Return true if this is a 64-bit vector type.
252 inline bool is64BitVector() const {
253 return (V==v8i8 || V==v4i16 || V==v2i32 || V==v1i64 || V==v2f32 ||
254 (isExtended() && isVector() && getSizeInBits()==64));
257 /// is128BitVector - Return true if this is a 128-bit vector type.
258 inline bool is128BitVector() const {
259 return (V==v16i8 || V==v8i16 || V==v4i32 || V==v2i64 ||
260 V==v4f32 || V==v2f64 ||
261 (isExtended() && isVector() && getSizeInBits()==128));
264 /// isByteSized - Return true if the bit size is a multiple of 8.
265 inline bool isByteSized() const {
266 return (getSizeInBits() & 7) == 0;
269 /// isRound - Return true if the size is a power-of-two number of bytes.
270 inline 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 inline bool bitsGT(MVT VT) const {
277 return getSizeInBits() > VT.getSizeInBits();
280 /// bitsGE - Return true if this has no less bits than VT.
281 inline bool bitsGE(MVT VT) const {
282 return getSizeInBits() >= VT.getSizeInBits();
285 /// bitsLT - Return true if this has less bits than VT.
286 inline bool bitsLT(MVT VT) const {
287 return getSizeInBits() < VT.getSizeInBits();
290 /// bitsLE - Return true if this has no more bits than VT.
291 inline bool bitsLE(MVT VT) const {
292 return getSizeInBits() <= VT.getSizeInBits();
296 /// getSimpleVT - Return the SimpleValueType held in the specified
298 inline SimpleValueType getSimpleVT() const {
299 assert(isSimple() && "Expected a SimpleValueType!");
300 return (SimpleValueType)V;
303 /// getVectorElementType - Given a vector type, return the type of
305 inline MVT getVectorElementType() const {
306 assert(isVector() && "Invalid vector type!");
309 assert(isExtended() && "Unknown simple vector type!");
311 VT.V = V & ElementMask;
315 case v16i8: return i8;
317 case v8i16: return i16;
320 case v4i32: return i32;
322 case v2i64: return i64;
325 case v4f32: return f32;
326 case v2f64: return f64;
330 /// getVectorNumElements - Given a vector type, return the number of
331 /// elements it contains.
332 inline unsigned getVectorNumElements() const {
333 assert(isVector() && "Invalid vector type!");
336 assert(isExtended() && "Unknown simple vector type!");
337 return ((V & VectorMask) >> (32 - VectorBits)) - 1;
338 case v16i8: return 16;
340 case v8i16: return 8;
343 case v4f32: return 4;
345 case v3f32: return 3;
349 case v2f64: return 2;
350 case v1i64: return 1;
354 /// getSizeInBits - Return the size of the specified value type in bits.
355 inline unsigned getSizeInBits() const {
358 assert(isExtended() && "MVT has no known size!");
360 return getVectorElementType().getSizeInBits()*getVectorNumElements();
362 return ((V & PrecisionMask) >> SimpleTypeBits) + 1;
363 assert(false && "Unknown value type!");
367 case i16 : return 16;
369 case i32 : return 32;
376 case v2f32: return 64;
377 case f80 : return 80;
379 case v3f32: return 96;
388 case v2f64: return 128;
392 /// getStoreSizeInBits - Return the number of bits overwritten by a store
393 /// of the specified value type.
394 inline unsigned getStoreSizeInBits() const {
395 return (getSizeInBits() + 7)/8*8;
398 /// getRoundIntegerType - Rounds the bit-width of the given integer MVT up
399 /// to the nearest power of two (and at least to eight), and returns the
400 /// integer MVT with that number of bits.
401 inline MVT getRoundIntegerType() const {
402 assert(isInteger() && !isVector() && "Invalid integer type!");
403 unsigned BitWidth = getSizeInBits();
407 return getIntegerVT(1 << Log2_32_Ceil(BitWidth));
410 /// getIntegerVTBitMask - Return an integer with 1's every place there are
411 /// bits in the specified integer value type. FIXME: Should return an apint.
412 inline uint64_t getIntegerVTBitMask() const {
413 assert(isInteger() && !isVector() && "Only applies to int scalars!");
414 return ~uint64_t(0UL) >> (64-getSizeInBits());
417 /// getIntegerVTSignBit - Return an integer with a 1 in the position of the
418 /// sign bit for the specified integer value type. FIXME: Should return an
420 inline uint64_t getIntegerVTSignBit() const {
421 assert(isInteger() && !isVector() && "Only applies to int scalars!");
422 return uint64_t(1UL) << (getSizeInBits()-1);
425 /// getMVTString - This function returns value type as a string,
427 std::string getMVTString() const;
429 /// getTypeForMVT - This method returns an LLVM type corresponding to the
430 /// specified MVT. For integer types, this returns an unsigned type. Note
431 /// that this will abort for types that cannot be represented.
432 const Type *getTypeForMVT() const;
434 /// getMVT - Return the value type corresponding to the specified type.
435 /// This returns all pointers as iPTR. If HandleUnknown is true, unknown
436 /// types are returned as Other, otherwise they are invalid.
437 static MVT getMVT(const Type *Ty, bool HandleUnknown = false);
439 /// getRawBits - Represent the type as a bunch of bits.
440 uint32_t getRawBits() const { return V; }
442 /// compareRawBits - A meaningless but well-behaved order, useful for
443 /// constructing containers.
444 struct compareRawBits {
445 bool operator()(MVT L, MVT R) const {
446 return L.getRawBits() < R.getRawBits();
451 } // End llvm namespace