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 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 // fAny - Any floating-point or vector floating-point value. This is used
73 // for intrinsics that have overloadings based on floating-point types.
74 // This is only for tblgen's consumption!
77 // iAny - An integer or vector integer value of any bit width. This is
78 // used for intrinsics that have overloadings based on integer bit widths.
79 // This is only for tblgen's consumption!
82 // iPTR - An int value the size of the pointer of the current
83 // target. This should only be used internal to tblgen!
87 /// MVT - This type holds low-level value types. Valid values include any of
88 /// the values in the SimpleValueType enum, or any value returned from one
89 /// of the MVT methods. Any value type equal to one of the SimpleValueType
90 /// enum values is a "simple" value type. All others are "extended".
92 /// Note that simple doesn't necessary mean legal for the target machine.
93 /// All legal value types must be simple, but often there are some simple
94 /// value types that are not legal.
97 /// Extended types are either vector types or arbitrary precision integers.
98 /// Arbitrary precision integers have iAny in the first SimpleTypeBits bits,
99 /// and the bit-width in the next PrecisionBits bits, offset by minus one.
100 /// Vector types are encoded by having the first SimpleTypeBits+PrecisionBits
101 /// bits encode the vector element type (which must be a scalar type, possibly
102 /// an arbitrary precision integer) and the remaining VectorBits upper bits
103 /// encode the vector length, offset by one.
105 /// 31--------------16-----------8-------------0
106 /// | Vector length | Precision | Simple type |
107 /// | | Vector element |
110 static const int SimpleTypeBits = 8;
111 static const int PrecisionBits = 8;
112 static const int VectorBits = 32 - SimpleTypeBits - PrecisionBits;
114 static const uint32_t SimpleTypeMask =
115 (~uint32_t(0) << (32 - SimpleTypeBits)) >> (32 - SimpleTypeBits);
117 static const uint32_t PrecisionMask =
118 ((~uint32_t(0) << VectorBits) >> (32 - PrecisionBits)) << SimpleTypeBits;
120 static const uint32_t VectorMask =
121 (~uint32_t(0) >> (32 - VectorBits)) << (32 - VectorBits);
123 static const uint32_t ElementMask =
124 (~uint32_t(0) << VectorBits) >> VectorBits;
129 MVT(SimpleValueType S) { V = S; }
130 inline bool operator== (const MVT VT) const { return V == VT.V; }
131 inline bool operator!= (const MVT VT) const { return V != VT.V; }
133 /// FIXME: The following comparison methods are bogus - they are only here
134 /// to ease the transition to a struct type.
135 inline bool operator< (const MVT VT) const { return V < VT.V; }
136 inline bool operator<= (const MVT VT) const { return V <= VT.V; }
137 inline bool operator> (const MVT VT) const { return V > VT.V; }
138 inline bool operator>= (const MVT VT) const { return V >= VT.V; }
140 /// getIntegerVT - Returns the MVT that represents an integer with the given
142 static inline MVT getIntegerVT(unsigned BitWidth) {
160 VT.V = iAny | (((BitWidth - 1) << SimpleTypeBits) & PrecisionMask);
161 assert(VT.getSizeInBits() == BitWidth && "Bad bit width!");
165 /// getVectorVT - Returns the MVT that represents a vector NumElements in
166 /// length, where each element is of type VT.
167 static inline MVT getVectorVT(MVT VT, unsigned NumElements) {
172 if (NumElements == 8) return v8i8;
173 if (NumElements == 16) return v16i8;
176 if (NumElements == 4) return v4i16;
177 if (NumElements == 8) return v8i16;
180 if (NumElements == 2) return v2i32;
181 if (NumElements == 3) return v3i32;
182 if (NumElements == 4) return v4i32;
185 if (NumElements == 1) return v1i64;
186 if (NumElements == 2) return v2i64;
189 if (NumElements == 2) return v2f32;
190 if (NumElements == 3) return v3f32;
191 if (NumElements == 4) return v4f32;
194 if (NumElements == 2) return v2f64;
197 // Set the length with the top bit forced to zero (needed by the verifier).
199 Result.V = VT.V | (((NumElements + 1) << (33 - VectorBits)) >> 1);
200 assert(Result.getVectorElementType() == VT &&
201 "Bad vector element type!");
202 assert(Result.getVectorNumElements() == NumElements &&
203 "Bad vector length!");
207 /// getIntVectorWithNumElements - Return any integer vector type that has
208 /// the specified number of elements.
209 static inline MVT getIntVectorWithNumElements(unsigned NumElts) {
211 default: return getVectorVT(i8, NumElts);
212 case 1: return v1i64;
213 case 2: return v2i32;
214 case 3: return v3i32;
215 case 4: return v4i16;
217 case 16: return v16i8;
222 /// isSimple - Test if the given MVT is simple (as opposed to being
224 inline bool isSimple() const {
225 return V <= SimpleTypeMask;
228 /// isExtended - Test if the given MVT is extended (as opposed to
230 inline bool isExtended() const {
234 /// isFloatingPoint - Return true if this is a FP, or a vector FP type.
235 inline bool isFloatingPoint() const {
236 uint32_t SVT = V & SimpleTypeMask;
237 return (SVT >= f32 && SVT <= ppcf128) || (SVT >= v2f32 && SVT <= v2f64);
240 /// isInteger - Return true if this is an integer, or a vector integer type.
241 inline bool isInteger() const {
242 uint32_t SVT = V & SimpleTypeMask;
243 return (SVT >= FIRST_INTEGER_VALUETYPE && SVT <= LAST_INTEGER_VALUETYPE) ||
244 (SVT >= v8i8 && SVT <= v2i64) || (SVT == iAny && (V & PrecisionMask));
247 /// isVector - Return true if this is a vector value type.
248 inline bool isVector() const {
249 return (V >= FIRST_VECTOR_VALUETYPE && V <= LAST_VECTOR_VALUETYPE) ||
253 /// is64BitVector - Return true if this is a 64-bit vector type.
254 inline bool is64BitVector() const {
255 return (V==v8i8 || V==v4i16 || V==v2i32 || V==v1i64 || V==v2f32 ||
256 (isExtended() && isVector() && getSizeInBits()==64));
259 /// is128BitVector - Return true if this is a 128-bit vector type.
260 inline bool is128BitVector() const {
261 return (V==v16i8 || V==v8i16 || V==v4i32 || V==v2i64 ||
262 V==v4f32 || V==v2f64 ||
263 (isExtended() && isVector() && getSizeInBits()==128));
267 /// getSimpleVT - Return the SimpleValueType held in the specified
269 inline SimpleValueType getSimpleVT() const {
270 assert(isSimple() && "Expected a SimpleValueType!");
271 return (SimpleValueType)V;
274 /// getVectorElementType - Given a vector type, return the type of
276 inline MVT getVectorElementType() const {
277 assert(isVector() && "Invalid vector type!");
280 assert(isExtended() && "Unknown simple vector type!");
282 VT.V = V & ElementMask;
286 case v16i8: return i8;
288 case v8i16: return i16;
291 case v4i32: return i32;
293 case v2i64: return i64;
296 case v4f32: return f32;
297 case v2f64: return f64;
301 /// getVectorNumElements - Given a vector type, return the number of
302 /// elements it contains.
303 inline unsigned getVectorNumElements() const {
304 assert(isVector() && "Invalid vector type!");
307 assert(isExtended() && "Unknown simple vector type!");
308 return ((V & VectorMask) >> (32 - VectorBits)) - 1;
309 case v16i8: return 16;
311 case v8i16: return 8;
314 case v4f32: return 4;
316 case v3f32: return 3;
320 case v2f64: return 2;
321 case v1i64: return 1;
325 /// getSizeInBits - Return the size of the specified value type in bits.
326 inline unsigned getSizeInBits() const {
329 assert(isExtended() && "MVT has no known size!");
331 return getVectorElementType().getSizeInBits()*getVectorNumElements();
333 return ((V & PrecisionMask) >> SimpleTypeBits) + 1;
334 assert(false && "Unknown value type!");
338 case i16 : return 16;
340 case i32 : return 32;
347 case v2f32: return 64;
348 case f80 : return 80;
350 case v3f32: return 96;
359 case v2f64: return 128;
363 /// getStoreSizeInBits - Return the number of bits overwritten by a store
364 /// of the specified value type.
365 inline unsigned getStoreSizeInBits() const {
366 return (getSizeInBits() + 7)/8*8;
369 /// getRoundIntegerType - Rounds the bit-width of the given integer MVT up
370 /// to the nearest power of two (and at least to eight), and returns the
371 /// integer MVT with that number of bits.
372 inline MVT getRoundIntegerType() const {
373 assert(isInteger() && !isVector() && "Invalid integer type!");
374 unsigned BitWidth = getSizeInBits();
378 return getIntegerVT(1 << Log2_32_Ceil(BitWidth));
381 /// getIntegerVTBitMask - Return an integer with 1's every place there are
382 /// bits in the specified integer value type. FIXME: Should return an apint.
383 inline uint64_t getIntegerVTBitMask() const {
384 assert(isInteger() && !isVector() && "Only applies to int scalars!");
385 return ~uint64_t(0UL) >> (64-getSizeInBits());
388 /// getIntegerVTSignBit - Return an integer with a 1 in the position of the
389 /// sign bit for the specified integer value type. FIXME: Should return an
391 inline uint64_t getIntegerVTSignBit() const {
392 assert(isInteger() && !isVector() && "Only applies to int scalars!");
393 return uint64_t(1UL) << (getSizeInBits()-1);
396 /// getMVTString - This function returns value type as a string,
398 std::string getMVTString() const;
400 /// getTypeForMVT - This method returns an LLVM type corresponding to the
401 /// specified MVT. For integer types, this returns an unsigned type. Note
402 /// that this will abort for types that cannot be represented.
403 const Type *getTypeForMVT() const;
405 /// getMVT - Return the value type corresponding to the specified type.
406 /// This returns all pointers as iPTR. If HandleUnknown is true, unknown
407 /// types are returned as Other, otherwise they are invalid.
408 static MVT getMVT(const Type *Ty, bool HandleUnknown = false);
411 } // End llvm namespace