1 //===- CodeGen/ValueTypes.h - Low-Level Target independ. types --*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source 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 typedef uint32_t ValueType;
114 static const int SimpleTypeBits = 8;
115 static const int PrecisionBits = 8;
116 static const int VectorBits = 32 - SimpleTypeBits - PrecisionBits;
118 static const uint32_t SimpleTypeMask =
119 (~uint32_t(0) << (32 - SimpleTypeBits)) >> (32 - SimpleTypeBits);
121 static const uint32_t PrecisionMask =
122 ((~uint32_t(0) << VectorBits) >> (32 - PrecisionBits)) << SimpleTypeBits;
124 static const uint32_t VectorMask =
125 (~uint32_t(0) >> (32 - VectorBits)) << (32 - VectorBits);
127 static const uint32_t ElementMask =
128 (~uint32_t(0) << VectorBits) >> VectorBits;
130 /// MVT::isExtendedVT - Test if the given ValueType is extended
131 /// (as opposed to being simple).
132 static inline bool isExtendedVT(ValueType VT) {
133 return VT > SimpleTypeMask;
136 /// MVT::isInteger - Return true if this is an integer, or a vector integer
138 static inline bool isInteger(ValueType VT) {
139 ValueType SVT = VT & SimpleTypeMask;
140 return (SVT >= FIRST_INTEGER_VALUETYPE && SVT <= LAST_INTEGER_VALUETYPE) ||
141 (SVT >= v8i8 && SVT <= v2i64) || (SVT == iAny && (VT & PrecisionMask));
144 /// MVT::isFloatingPoint - Return true if this is an FP, or a vector FP type.
145 static inline bool isFloatingPoint(ValueType VT) {
146 ValueType SVT = VT & SimpleTypeMask;
147 return (SVT >= f32 && SVT <= ppcf128) || (SVT >= v2f32 && SVT <= v2f64);
150 /// MVT::isVector - Return true if this is a vector value type.
151 static inline bool isVector(ValueType VT) {
152 return (VT >= FIRST_VECTOR_VALUETYPE && VT <= LAST_VECTOR_VALUETYPE) ||
156 /// MVT::getVectorElementType - Given a vector type, return the type of
158 static inline ValueType getVectorElementType(ValueType VT) {
159 assert(isVector(VT) && "Invalid vector type!");
162 assert(isExtendedVT(VT) && "Unknown simple vector type!");
163 return VT & ElementMask;
165 case v16i8: return i8;
167 case v8i16: return i16;
170 case v4i32: return i32;
172 case v2i64: return i64;
175 case v4f32: return f32;
176 case v2f64: return f64;
180 /// MVT::getVectorNumElements - Given a vector type, return the
181 /// number of elements it contains.
182 static inline unsigned getVectorNumElements(ValueType VT) {
183 assert(isVector(VT) && "Invalid vector type!");
186 assert(isExtendedVT(VT) && "Unknown simple vector type!");
187 return ((VT & VectorMask) >> (32 - VectorBits)) - 1;
188 case v16i8: return 16;
190 case v8i16: return 8;
193 case v4f32: return 4;
195 case v3f32: return 3;
199 case v2f64: return 2;
200 case v1i64: return 1;
204 /// MVT::getSizeInBits - Return the size of the specified value type
207 static inline unsigned getSizeInBits(ValueType VT) {
210 assert(isExtendedVT(VT) && "ValueType has no known size!");
212 return getSizeInBits(getVectorElementType(VT)) *
213 getVectorNumElements(VT);
215 return ((VT & PrecisionMask) >> SimpleTypeBits) + 1;
216 assert(0 && "Unknown value type!");
217 case MVT::i1 : return 1;
218 case MVT::i8 : return 8;
219 case MVT::i16 : return 16;
221 case MVT::i32 : return 32;
228 case MVT::v2f32: return 64;
229 case MVT::f80 : return 80;
231 case MVT::v3f32: return 96;
240 case MVT::v2f64: return 128;
244 /// MVT::getIntegerType - Returns the ValueType that represents an integer
245 /// with the given number of bits.
247 static inline ValueType getIntegerType(unsigned BitWidth) {
264 ValueType Result = iAny |
265 (((BitWidth - 1) << SimpleTypeBits) & PrecisionMask);
266 assert(getSizeInBits(Result) == BitWidth && "Bad bit width!");
270 /// MVT::RoundIntegerType - Rounds the bit-width of the given integer
271 /// ValueType up to the nearest power of two (and at least to eight),
272 /// and returns the integer ValueType with that number of bits.
274 static inline ValueType RoundIntegerType(ValueType VT) {
275 assert(isInteger(VT) && !isVector(VT) && "Invalid integer type!");
276 unsigned BitWidth = getSizeInBits(VT);
280 return getIntegerType(1 << Log2_32_Ceil(BitWidth));
283 /// MVT::getVectorType - Returns the ValueType that represents a vector
284 /// NumElements in length, where each element is of type VT.
286 static inline ValueType getVectorType(ValueType VT, unsigned NumElements) {
291 if (NumElements == 8) return MVT::v8i8;
292 if (NumElements == 16) return MVT::v16i8;
295 if (NumElements == 4) return MVT::v4i16;
296 if (NumElements == 8) return MVT::v8i16;
299 if (NumElements == 2) return MVT::v2i32;
300 if (NumElements == 3) return MVT::v3i32;
301 if (NumElements == 4) return MVT::v4i32;
304 if (NumElements == 1) return MVT::v1i64;
305 if (NumElements == 2) return MVT::v2i64;
308 if (NumElements == 2) return MVT::v2f32;
309 if (NumElements == 3) return MVT::v3f32;
310 if (NumElements == 4) return MVT::v4f32;
313 if (NumElements == 2) return MVT::v2f64;
316 ValueType Result = VT | ((NumElements + 1) << (32 - VectorBits));
317 assert(getVectorElementType(Result) == VT &&
318 "Bad vector element type!");
319 assert(getVectorNumElements(Result) == NumElements &&
320 "Bad vector length!");
324 /// MVT::getIntVectorWithNumElements - Return any integer vector type that has
325 /// the specified number of elements.
326 static inline ValueType getIntVectorWithNumElements(unsigned NumElts) {
328 default: return getVectorType(i8, NumElts);
329 case 1: return v1i64;
330 case 2: return v2i32;
331 case 3: return v3i32;
332 case 4: return v4i16;
334 case 16: return v16i8;
339 /// MVT::getIntVTBitMask - Return an integer with 1's every place there are
340 /// bits in the specified integer value type.
341 static inline uint64_t getIntVTBitMask(ValueType VT) {
342 assert(isInteger(VT) && !isVector(VT) && "Only applies to int scalars!");
343 return ~uint64_t(0UL) >> (64-getSizeInBits(VT));
345 /// MVT::getIntVTSignBit - Return an integer with a 1 in the position of the
346 /// sign bit for the specified integer value type.
347 static inline uint64_t getIntVTSignBit(ValueType VT) {
348 assert(isInteger(VT) && !isVector(VT) && "Only applies to int scalars!");
349 return uint64_t(1UL) << (getSizeInBits(VT)-1);
352 /// MVT::getValueTypeString - This function returns value type as a string,
354 std::string getValueTypeString(ValueType VT);
356 /// MVT::getTypeForValueType - This method returns an LLVM type corresponding
357 /// to the specified ValueType. For integer types, this returns an unsigned
358 /// type. Note that this will abort for types that cannot be represented.
359 const Type *getTypeForValueType(ValueType VT);
361 /// MVT::getValueType - Return the value type corresponding to the specified
362 /// type. This returns all pointers as MVT::iPTR. If HandleUnknown is true,
363 /// unknown types are returned as Other, otherwise they are invalid.
364 ValueType getValueType(const Type *Ty, bool HandleUnknown = false);
367 } // End llvm namespace