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 /// MVT namespace - This namespace defines the SimpleValueType enum, which
28 /// contains the various low-level value types, and the ValueType typedef.
30 namespace MVT { // MVT = Machine Value Types
31 enum SimpleValueType {
32 // If you change this numbering, you must change the values in ValueTypes.td
34 Other = 0, // This is a non-standard value
35 i1 = 1, // This is a 1 bit integer value
36 i8 = 2, // This is an 8 bit integer value
37 i16 = 3, // This is a 16 bit integer value
38 i32 = 4, // This is a 32 bit integer value
39 i64 = 5, // This is a 64 bit integer value
40 i128 = 6, // This is a 128 bit integer value
42 FIRST_INTEGER_VALUETYPE = i1,
43 LAST_INTEGER_VALUETYPE = i128,
45 f32 = 7, // This is a 32 bit floating point value
46 f64 = 8, // This is a 64 bit floating point value
47 f80 = 9, // This is a 80 bit floating point value
48 f128 = 10, // This is a 128 bit floating point value
49 ppcf128 = 11, // This is a PPC 128-bit floating point value
50 Flag = 12, // This is a condition code or machine flag.
52 isVoid = 13, // This has no value
55 v4i16 = 15, // 4 x i16
56 v2i32 = 16, // 2 x i32
57 v1i64 = 17, // 1 x i64
58 v16i8 = 18, // 16 x i8
59 v8i16 = 19, // 8 x i16
60 v3i32 = 20, // 3 x i32
61 v4i32 = 21, // 4 x i32
62 v2i64 = 22, // 2 x i64
64 v2f32 = 23, // 2 x f32
65 v3f32 = 24, // 3 x f32
66 v4f32 = 25, // 4 x f32
67 v2f64 = 26, // 2 x f64
69 FIRST_VECTOR_VALUETYPE = v8i8,
70 LAST_VECTOR_VALUETYPE = v2f64,
72 LAST_VALUETYPE = 27, // This always remains at the end of the list.
74 // fAny - Any floating-point or vector floating-point value. This is used
75 // for intrinsics that have overloadings based on floating-point types.
76 // This is only for tblgen's consumption!
79 // iAny - An integer or vector integer value of any bit width. This is
80 // used for intrinsics that have overloadings based on integer bit widths.
81 // This is only for tblgen's consumption!
84 // iPTR - An int value the size of the pointer of the current
85 // target. This should only be used internal to tblgen!
89 /// MVT::ValueType - This type holds low-level value types. Valid values
90 /// include any of the values in the SimpleValueType enum, or any value
91 /// returned from a function in the MVT namespace that has a ValueType
92 /// return type. Any value type equal to one of the SimpleValueType enum
93 /// values is a "simple" value type. All other value types are "extended".
95 /// Note that simple doesn't necessary mean legal for the target machine.
96 /// All legal value types must be simple, but often there are some simple
97 /// value types that are not legal.
100 /// Extended types are either vector types or arbitrary precision integers.
101 /// Arbitrary precision integers have iAny in the first SimpleTypeBits bits,
102 /// and the bit-width in the next PrecisionBits bits, offset by minus one.
103 /// Vector types are encoded by having the first SimpleTypeBits+PrecisionBits
104 /// bits encode the vector element type (which must be a scalar type, possibly
105 /// an arbitrary precision integer) and the remaining VectorBits upper bits
106 /// encode the vector length, offset by one.
108 /// 31--------------16-----------8-------------0
109 /// | Vector length | Precision | Simple type |
110 /// | | Vector element |
112 /// Note that the verifier currently requires the top bit to be zero.
114 typedef uint32_t ValueType;
116 static const int SimpleTypeBits = 8;
117 static const int PrecisionBits = 8;
118 static const int VectorBits = 32 - SimpleTypeBits - PrecisionBits;
120 static const uint32_t SimpleTypeMask =
121 (~uint32_t(0) << (32 - SimpleTypeBits)) >> (32 - SimpleTypeBits);
123 static const uint32_t PrecisionMask =
124 ((~uint32_t(0) << VectorBits) >> (32 - PrecisionBits)) << SimpleTypeBits;
126 static const uint32_t VectorMask =
127 (~uint32_t(0) >> (32 - VectorBits)) << (32 - VectorBits);
129 static const uint32_t ElementMask =
130 (~uint32_t(0) << VectorBits) >> VectorBits;
132 /// MVT::isExtendedVT - Test if the given ValueType is extended
133 /// (as opposed to being simple).
134 static inline bool isExtendedVT(ValueType VT) {
135 return VT > SimpleTypeMask;
138 /// MVT::isInteger - Return true if this is an integer, or a vector integer
140 static inline bool isInteger(ValueType VT) {
141 ValueType SVT = VT & SimpleTypeMask;
142 return (SVT >= FIRST_INTEGER_VALUETYPE && SVT <= LAST_INTEGER_VALUETYPE) ||
143 (SVT >= v8i8 && SVT <= v2i64) || (SVT == iAny && (VT & PrecisionMask));
146 /// MVT::isFloatingPoint - Return true if this is an FP, or a vector FP type.
147 static inline bool isFloatingPoint(ValueType VT) {
148 ValueType SVT = VT & SimpleTypeMask;
149 return (SVT >= f32 && SVT <= ppcf128) || (SVT >= v2f32 && SVT <= v2f64);
152 /// MVT::isVector - Return true if this is a vector value type.
153 static inline bool isVector(ValueType VT) {
154 return (VT >= FIRST_VECTOR_VALUETYPE && VT <= LAST_VECTOR_VALUETYPE) ||
158 /// MVT::getVectorElementType - Given a vector type, return the type of
160 static inline ValueType getVectorElementType(ValueType VT) {
161 assert(isVector(VT) && "Invalid vector type!");
164 assert(isExtendedVT(VT) && "Unknown simple vector type!");
165 return VT & ElementMask;
167 case v16i8: return i8;
169 case v8i16: return i16;
172 case v4i32: return i32;
174 case v2i64: return i64;
177 case v4f32: return f32;
178 case v2f64: return f64;
182 /// MVT::getVectorNumElements - Given a vector type, return the
183 /// number of elements it contains.
184 static inline unsigned getVectorNumElements(ValueType VT) {
185 assert(isVector(VT) && "Invalid vector type!");
188 assert(isExtendedVT(VT) && "Unknown simple vector type!");
189 return ((VT & VectorMask) >> (32 - VectorBits)) - 1;
190 case v16i8: return 16;
192 case v8i16: return 8;
195 case v4f32: return 4;
197 case v3f32: return 3;
201 case v2f64: return 2;
202 case v1i64: return 1;
206 /// MVT::getSizeInBits - Return the size of the specified value type
209 static inline unsigned getSizeInBits(ValueType VT) {
212 assert(isExtendedVT(VT) && "ValueType has no known size!");
214 return getSizeInBits(getVectorElementType(VT)) *
215 getVectorNumElements(VT);
217 return ((VT & PrecisionMask) >> SimpleTypeBits) + 1;
218 assert(0 && "Unknown value type!");
219 case MVT::i1 : return 1;
220 case MVT::i8 : return 8;
221 case MVT::i16 : return 16;
223 case MVT::i32 : return 32;
230 case MVT::v2f32: return 64;
231 case MVT::f80 : return 80;
233 case MVT::v3f32: return 96;
242 case MVT::v2f64: return 128;
246 /// MVT::getStoreSizeInBits - Return the number of bits overwritten by a
247 /// store of the specified value type.
249 static inline unsigned getStoreSizeInBits(ValueType VT) {
250 return (getSizeInBits(VT) + 7)/8*8;
253 /// MVT::getIntegerType - Returns the ValueType that represents an integer
254 /// with the given number of bits.
256 static inline ValueType getIntegerType(unsigned BitWidth) {
273 ValueType Result = iAny |
274 (((BitWidth - 1) << SimpleTypeBits) & PrecisionMask);
275 assert(getSizeInBits(Result) == BitWidth && "Bad bit width!");
279 /// MVT::RoundIntegerType - Rounds the bit-width of the given integer
280 /// ValueType up to the nearest power of two (and at least to eight),
281 /// and returns the integer ValueType with that number of bits.
283 static inline ValueType RoundIntegerType(ValueType VT) {
284 assert(isInteger(VT) && !isVector(VT) && "Invalid integer type!");
285 unsigned BitWidth = getSizeInBits(VT);
289 return getIntegerType(1 << Log2_32_Ceil(BitWidth));
292 /// MVT::getVectorType - Returns the ValueType that represents a vector
293 /// NumElements in length, where each element is of type VT.
295 static inline ValueType getVectorType(ValueType VT, unsigned NumElements) {
300 if (NumElements == 8) return MVT::v8i8;
301 if (NumElements == 16) return MVT::v16i8;
304 if (NumElements == 4) return MVT::v4i16;
305 if (NumElements == 8) return MVT::v8i16;
308 if (NumElements == 2) return MVT::v2i32;
309 if (NumElements == 3) return MVT::v3i32;
310 if (NumElements == 4) return MVT::v4i32;
313 if (NumElements == 1) return MVT::v1i64;
314 if (NumElements == 2) return MVT::v2i64;
317 if (NumElements == 2) return MVT::v2f32;
318 if (NumElements == 3) return MVT::v3f32;
319 if (NumElements == 4) return MVT::v4f32;
322 if (NumElements == 2) return MVT::v2f64;
325 // Set the length with the top bit forced to zero (needed by the verifier).
326 ValueType Result = VT | (((NumElements + 1) << (33 - VectorBits)) >> 1);
327 assert(getVectorElementType(Result) == VT &&
328 "Bad vector element type!");
329 assert(getVectorNumElements(Result) == NumElements &&
330 "Bad vector length!");
334 /// MVT::getIntVectorWithNumElements - Return any integer vector type that has
335 /// the specified number of elements.
336 static inline ValueType getIntVectorWithNumElements(unsigned NumElts) {
338 default: return getVectorType(i8, NumElts);
339 case 1: return v1i64;
340 case 2: return v2i32;
341 case 3: return v3i32;
342 case 4: return v4i16;
344 case 16: return v16i8;
349 /// MVT::getIntVTBitMask - Return an integer with 1's every place there are
350 /// bits in the specified integer value type.
351 static inline uint64_t getIntVTBitMask(ValueType VT) {
352 assert(isInteger(VT) && !isVector(VT) && "Only applies to int scalars!");
353 return ~uint64_t(0UL) >> (64-getSizeInBits(VT));
355 /// MVT::getIntVTSignBit - Return an integer with a 1 in the position of the
356 /// sign bit for the specified integer value type.
357 static inline uint64_t getIntVTSignBit(ValueType VT) {
358 assert(isInteger(VT) && !isVector(VT) && "Only applies to int scalars!");
359 return uint64_t(1UL) << (getSizeInBits(VT)-1);
362 /// MVT::getValueTypeString - This function returns value type as a string,
364 std::string getValueTypeString(ValueType VT);
366 /// MVT::getTypeForValueType - This method returns an LLVM type corresponding
367 /// to the specified ValueType. For integer types, this returns an unsigned
368 /// type. Note that this will abort for types that cannot be represented.
369 const Type *getTypeForValueType(ValueType VT);
371 /// MVT::getValueType - Return the value type corresponding to the specified
372 /// type. This returns all pointers as MVT::iPTR. If HandleUnknown is true,
373 /// unknown types are returned as Other, otherwise they are invalid.
374 ValueType getValueType(const Type *Ty, bool HandleUnknown = false);
377 } // End llvm namespace