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 // iPTRAny - An int value the size of the pointer of the current
74 // target to any address space. This must only be used internal to
75 // tblgen. Other than for overloading, we treat iPTRAny the same as iPTR.
78 // fAny - Any floating-point or vector floating-point value. This is used
79 // for intrinsics that have overloadings based on floating-point types.
80 // This is only for tblgen's consumption!
83 // iAny - An integer or vector integer value of any bit width. This is
84 // used for intrinsics that have overloadings based on integer bit widths.
85 // This is only for tblgen's consumption!
88 // iPTR - An int value the size of the pointer of the current
89 // target. This should only be used internal to tblgen!
93 /// MVT - This type holds low-level value types. Valid values include any of
94 /// the values in the SimpleValueType enum, or any value returned from one
95 /// of the MVT methods. Any value type equal to one of the SimpleValueType
96 /// enum values is a "simple" value type. All others are "extended".
98 /// Note that simple doesn't necessary mean legal for the target machine.
99 /// All legal value types must be simple, but often there are some simple
100 /// value types that are not legal.
103 /// Extended types are either vector types or arbitrary precision integers.
104 /// Arbitrary precision integers have iAny in the first SimpleTypeBits bits,
105 /// and the bit-width in the next PrecisionBits bits, offset by minus one.
106 /// Vector types are encoded by having the first SimpleTypeBits+PrecisionBits
107 /// bits encode the vector element type (which must be a scalar type, possibly
108 /// an arbitrary precision integer) and the remaining VectorBits upper bits
109 /// encode the vector length, offset by one.
111 /// 32--------------16-----------8-------------0
112 /// | Vector length | Precision | Simple type |
113 /// | | Vector element |
118 static const int SimpleTypeBits = 8;
119 static const int PrecisionBits = 8;
120 static const int VectorBits = 32 - SimpleTypeBits - PrecisionBits;
122 static const uint32_t SimpleTypeMask =
123 (~uint32_t(0) << (32 - SimpleTypeBits)) >> (32 - SimpleTypeBits);
125 static const uint32_t PrecisionMask =
126 ((~uint32_t(0) << VectorBits) >> (32 - PrecisionBits)) << SimpleTypeBits;
128 static const uint32_t VectorMask =
129 (~uint32_t(0) >> (32 - VectorBits)) << (32 - VectorBits);
131 static const uint32_t ElementMask =
132 (~uint32_t(0) << VectorBits) >> VectorBits;
139 MVT(SimpleValueType S) { V = S; }
141 inline bool operator== (const MVT VT) const { return V == VT.V; }
142 inline bool operator!= (const MVT VT) const { return V != VT.V; }
144 /// getIntegerVT - Returns the MVT that represents an integer with the given
146 static inline MVT getIntegerVT(unsigned BitWidth) {
164 VT.V = iAny | (((BitWidth - 1) << SimpleTypeBits) & PrecisionMask);
165 assert(VT.getSizeInBits() == BitWidth && "Bad bit width!");
169 /// getVectorVT - Returns the MVT that represents a vector NumElements in
170 /// length, where each element is of type VT.
171 static inline MVT getVectorVT(MVT VT, unsigned NumElements) {
176 if (NumElements == 8) return v8i8;
177 if (NumElements == 16) return v16i8;
180 if (NumElements == 4) return v4i16;
181 if (NumElements == 8) return v8i16;
184 if (NumElements == 2) return v2i32;
185 if (NumElements == 3) return v3i32;
186 if (NumElements == 4) return v4i32;
189 if (NumElements == 1) return v1i64;
190 if (NumElements == 2) return v2i64;
193 if (NumElements == 2) return v2f32;
194 if (NumElements == 3) return v3f32;
195 if (NumElements == 4) return v4f32;
198 if (NumElements == 2) return v2f64;
202 Result.V = VT.V | ((NumElements + 1) << (32 - VectorBits));
203 assert(Result.getVectorElementType() == VT &&
204 "Bad vector element type!");
205 assert(Result.getVectorNumElements() == NumElements &&
206 "Bad vector length!");
210 /// getIntVectorWithNumElements - Return any integer vector type that has
211 /// the specified number of elements.
212 static inline MVT getIntVectorWithNumElements(unsigned NumElts) {
214 default: return getVectorVT(i8, NumElts);
215 case 1: return v1i64;
216 case 2: return v2i32;
217 case 3: return v3i32;
218 case 4: return v4i16;
220 case 16: return v16i8;
225 /// isSimple - Test if the given MVT is simple (as opposed to being
227 inline bool isSimple() const {
228 return V <= SimpleTypeMask;
231 /// isExtended - Test if the given MVT is extended (as opposed to
233 inline bool isExtended() const {
237 /// isFloatingPoint - Return true if this is a FP, or a vector FP type.
238 inline bool isFloatingPoint() const {
239 uint32_t SVT = V & SimpleTypeMask;
240 return (SVT >= f32 && SVT <= ppcf128) || (SVT >= v2f32 && SVT <= v2f64);
243 /// isInteger - Return true if this is an integer, or a vector integer type.
244 inline bool isInteger() const {
245 uint32_t SVT = V & SimpleTypeMask;
246 return (SVT >= FIRST_INTEGER_VALUETYPE && SVT <= LAST_INTEGER_VALUETYPE) ||
247 (SVT >= v8i8 && SVT <= v2i64) || (SVT == iAny && (V & PrecisionMask));
250 /// isVector - Return true if this is a vector value type.
251 inline bool isVector() const {
252 return (V >= FIRST_VECTOR_VALUETYPE && V <= LAST_VECTOR_VALUETYPE) ||
256 /// is64BitVector - Return true if this is a 64-bit vector type.
257 inline bool is64BitVector() const {
258 return (V==v8i8 || V==v4i16 || V==v2i32 || V==v1i64 || V==v2f32 ||
259 (isExtended() && isVector() && getSizeInBits()==64));
262 /// is128BitVector - Return true if this is a 128-bit vector type.
263 inline bool is128BitVector() const {
264 return (V==v16i8 || V==v8i16 || V==v4i32 || V==v2i64 ||
265 V==v4f32 || V==v2f64 ||
266 (isExtended() && isVector() && getSizeInBits()==128));
269 /// isByteSized - Return true if the bit size is a multiple of 8.
270 inline bool isByteSized() const {
271 return (getSizeInBits() & 7) == 0;
274 /// isRound - Return true if the size is a power-of-two number of bytes.
275 inline bool isRound() const {
276 unsigned BitSize = getSizeInBits();
277 return BitSize >= 8 && !(BitSize & (BitSize - 1));
280 /// bitsGT - Return true if this has more bits than VT.
281 inline bool bitsGT(MVT VT) const {
282 return getSizeInBits() > VT.getSizeInBits();
285 /// bitsGE - Return true if this has no less bits than VT.
286 inline bool bitsGE(MVT VT) const {
287 return getSizeInBits() >= VT.getSizeInBits();
290 /// bitsLT - Return true if this has less bits than VT.
291 inline bool bitsLT(MVT VT) const {
292 return getSizeInBits() < VT.getSizeInBits();
295 /// bitsLE - Return true if this has no more bits than VT.
296 inline bool bitsLE(MVT VT) const {
297 return getSizeInBits() <= VT.getSizeInBits();
301 /// getSimpleVT - Return the SimpleValueType held in the specified
303 inline SimpleValueType getSimpleVT() const {
304 assert(isSimple() && "Expected a SimpleValueType!");
305 return (SimpleValueType)V;
308 /// getVectorElementType - Given a vector type, return the type of
310 inline MVT getVectorElementType() const {
311 assert(isVector() && "Invalid vector type!");
314 assert(isExtended() && "Unknown simple vector type!");
316 VT.V = V & ElementMask;
320 case v16i8: return i8;
322 case v8i16: return i16;
325 case v4i32: return i32;
327 case v2i64: return i64;
330 case v4f32: return f32;
331 case v2f64: return f64;
335 /// getVectorNumElements - Given a vector type, return the number of
336 /// elements it contains.
337 inline unsigned getVectorNumElements() const {
338 assert(isVector() && "Invalid vector type!");
341 assert(isExtended() && "Unknown simple vector type!");
342 return ((V & VectorMask) >> (32 - VectorBits)) - 1;
343 case v16i8: return 16;
345 case v8i16: return 8;
348 case v4f32: return 4;
350 case v3f32: return 3;
354 case v2f64: return 2;
355 case v1i64: return 1;
359 /// getSizeInBits - Return the size of the specified value type in bits.
360 inline unsigned getSizeInBits() const {
363 assert(isExtended() && "MVT has no known size!");
365 return getVectorElementType().getSizeInBits()*getVectorNumElements();
367 return ((V & PrecisionMask) >> SimpleTypeBits) + 1;
368 assert(false && "Unknown value type!");
372 case i16 : return 16;
374 case i32 : return 32;
381 case v2f32: return 64;
382 case f80 : return 80;
384 case v3f32: return 96;
393 case v2f64: return 128;
397 /// getStoreSizeInBits - Return the number of bits overwritten by a store
398 /// of the specified value type.
399 inline unsigned getStoreSizeInBits() const {
400 return (getSizeInBits() + 7)/8*8;
403 /// getRoundIntegerType - Rounds the bit-width of the given integer MVT up
404 /// to the nearest power of two (and at least to eight), and returns the
405 /// integer MVT with that number of bits.
406 inline MVT getRoundIntegerType() const {
407 assert(isInteger() && !isVector() && "Invalid integer type!");
408 unsigned BitWidth = getSizeInBits();
412 return getIntegerVT(1 << Log2_32_Ceil(BitWidth));
415 /// getIntegerVTBitMask - Return an integer with 1's every place there are
416 /// bits in the specified integer value type. FIXME: Should return an apint.
417 inline uint64_t getIntegerVTBitMask() const {
418 assert(isInteger() && !isVector() && "Only applies to int scalars!");
419 return ~uint64_t(0UL) >> (64-getSizeInBits());
422 /// getIntegerVTSignBit - Return an integer with a 1 in the position of the
423 /// sign bit for the specified integer value type. FIXME: Should return an
425 inline uint64_t getIntegerVTSignBit() const {
426 assert(isInteger() && !isVector() && "Only applies to int scalars!");
427 return uint64_t(1UL) << (getSizeInBits()-1);
430 /// getMVTString - This function returns value type as a string,
432 std::string getMVTString() const;
434 /// getTypeForMVT - This method returns an LLVM type corresponding to the
435 /// specified MVT. For integer types, this returns an unsigned type. Note
436 /// that this will abort for types that cannot be represented.
437 const Type *getTypeForMVT() const;
439 /// getMVT - Return the value type corresponding to the specified type.
440 /// This returns all pointers as iPTR. If HandleUnknown is true, unknown
441 /// types are returned as Other, otherwise they are invalid.
442 static MVT getMVT(const Type *Ty, bool HandleUnknown = false);
444 /// getRawBits - Represent the type as a bunch of bits.
445 uint32_t getRawBits() const { return V; }
447 /// compareRawBits - A meaningless but well-behaved order, useful for
448 /// constructing containers.
449 struct compareRawBits {
450 bool operator()(MVT L, MVT R) const {
451 return L.getRawBits() < R.getRawBits();
456 } // End llvm namespace