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 as 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
107 /// SimpleTypeBits+PrecisionBits bits encode the vector element type
108 /// (which must be a scalar type, possibly an arbitrary precision integer)
109 /// and the remaining VectorBits upper bits encode the vector length, offset
112 /// 32--------------16-----------8-------------0
113 /// | Vector length | Precision | Simple type |
114 /// | | Vector element |
119 static const int SimpleTypeBits = 8;
120 static const int PrecisionBits = 8;
121 static const int VectorBits = 32 - SimpleTypeBits - PrecisionBits;
123 static const uint32_t SimpleTypeMask =
124 (~uint32_t(0) << (32 - SimpleTypeBits)) >> (32 - SimpleTypeBits);
126 static const uint32_t PrecisionMask =
127 ((~uint32_t(0) << VectorBits) >> (32 - PrecisionBits)) << SimpleTypeBits;
129 static const uint32_t VectorMask =
130 (~uint32_t(0) >> (32 - VectorBits)) << (32 - VectorBits);
132 static const uint32_t ElementMask =
133 (~uint32_t(0) << VectorBits) >> VectorBits;
140 MVT(SimpleValueType S) : V(S) {}
142 bool operator== (const MVT VT) const { return V == VT.V; }
143 bool operator!= (const MVT VT) const { return V != VT.V; }
145 /// getFloatingPointVT - Returns the MVT that represents a floating point
146 /// type with the given number of bits. There are two floating point types
147 /// with 128 bits - this returns f128 rather than ppcf128.
148 static MVT getFloatingPointVT(unsigned BitWidth) {
151 assert(false && "Bad bit width!");
163 /// getIntegerVT - Returns the MVT that represents an integer with the given
165 static MVT getIntegerVT(unsigned BitWidth) {
183 VT.V = iAny | (((BitWidth - 1) << SimpleTypeBits) & PrecisionMask);
184 assert(VT.getSizeInBits() == BitWidth && "Bad bit width!");
188 /// getVectorVT - Returns the MVT that represents a vector NumElements in
189 /// length, where each element is of type VT.
190 static MVT getVectorVT(MVT VT, unsigned NumElements) {
195 if (NumElements == 8) return v8i8;
196 if (NumElements == 16) return v16i8;
199 if (NumElements == 4) return v4i16;
200 if (NumElements == 8) return v8i16;
203 if (NumElements == 2) return v2i32;
204 if (NumElements == 3) return v3i32;
205 if (NumElements == 4) return v4i32;
208 if (NumElements == 1) return v1i64;
209 if (NumElements == 2) return v2i64;
212 if (NumElements == 2) return v2f32;
213 if (NumElements == 3) return v3f32;
214 if (NumElements == 4) return v4f32;
217 if (NumElements == 2) return v2f64;
221 Result.V = VT.V | ((NumElements + 1) << (32 - VectorBits));
222 assert(Result.getVectorElementType() == VT &&
223 "Bad vector element type!");
224 assert(Result.getVectorNumElements() == NumElements &&
225 "Bad vector length!");
229 /// getIntVectorWithNumElements - Return any integer vector type that has
230 /// the specified number of elements.
231 static MVT getIntVectorWithNumElements(unsigned NumElts) {
233 default: return getVectorVT(i8, NumElts);
234 case 1: return v1i64;
235 case 2: return v2i32;
236 case 3: return v3i32;
237 case 4: return v4i16;
239 case 16: return v16i8;
244 /// isSimple - Test if the given MVT is simple (as opposed to being
246 bool isSimple() const {
247 return V <= SimpleTypeMask;
250 /// isExtended - Test if the given MVT is extended (as opposed to
252 bool isExtended() const {
256 /// isFloatingPoint - Return true if this is a FP, or a vector FP type.
257 bool isFloatingPoint() const {
258 uint32_t SVT = V & SimpleTypeMask;
259 return (SVT >= f32 && SVT <= ppcf128) || (SVT >= v2f32 && SVT <= v2f64);
262 /// isInteger - Return true if this is an integer, or a vector integer type.
263 bool isInteger() const {
264 uint32_t SVT = V & SimpleTypeMask;
265 return (SVT >= FIRST_INTEGER_VALUETYPE && SVT <= LAST_INTEGER_VALUETYPE) ||
266 (SVT >= v8i8 && SVT <= v2i64) || (SVT == iAny && (V & PrecisionMask));
269 /// isVector - Return true if this is a vector value type.
270 bool isVector() const {
271 return (V >= FIRST_VECTOR_VALUETYPE && V <= LAST_VECTOR_VALUETYPE) ||
275 /// is64BitVector - Return true if this is a 64-bit vector type.
276 bool is64BitVector() const {
277 return (V==v8i8 || V==v4i16 || V==v2i32 || V==v1i64 || V==v2f32 ||
278 (isExtended() && isVector() && getSizeInBits()==64));
281 /// is128BitVector - Return true if this is a 128-bit vector type.
282 bool is128BitVector() const {
283 return (V==v16i8 || V==v8i16 || V==v4i32 || V==v2i64 ||
284 V==v4f32 || V==v2f64 ||
285 (isExtended() && isVector() && getSizeInBits()==128));
288 /// isByteSized - Return true if the bit size is a multiple of 8.
289 bool isByteSized() const {
290 return (getSizeInBits() & 7) == 0;
293 /// isRound - Return true if the size is a power-of-two number of bytes.
294 bool isRound() const {
295 unsigned BitSize = getSizeInBits();
296 return BitSize >= 8 && !(BitSize & (BitSize - 1));
299 /// bitsGT - Return true if this has more bits than VT.
300 bool bitsGT(MVT VT) const {
301 return getSizeInBits() > VT.getSizeInBits();
304 /// bitsGE - Return true if this has no less bits than VT.
305 bool bitsGE(MVT VT) const {
306 return getSizeInBits() >= VT.getSizeInBits();
309 /// bitsLT - Return true if this has less bits than VT.
310 bool bitsLT(MVT VT) const {
311 return getSizeInBits() < VT.getSizeInBits();
314 /// bitsLE - Return true if this has no more bits than VT.
315 bool bitsLE(MVT VT) const {
316 return getSizeInBits() <= VT.getSizeInBits();
320 /// getSimpleVT - Return the SimpleValueType held in the specified
322 SimpleValueType getSimpleVT() const {
323 assert(isSimple() && "Expected a SimpleValueType!");
324 return (SimpleValueType)V;
327 /// getVectorElementType - Given a vector type, return the type of
329 MVT getVectorElementType() const {
330 assert(isVector() && "Invalid vector type!");
333 assert(isExtended() && "Unknown simple vector type!");
335 VT.V = V & ElementMask;
339 case v16i8: return i8;
341 case v8i16: return i16;
344 case v4i32: return i32;
346 case v2i64: return i64;
349 case v4f32: return f32;
350 case v2f64: return f64;
354 /// getVectorNumElements - Given a vector type, return the number of
355 /// elements it contains.
356 unsigned getVectorNumElements() const {
357 assert(isVector() && "Invalid vector type!");
360 assert(isExtended() && "Unknown simple vector type!");
361 return ((V & VectorMask) >> (32 - VectorBits)) - 1;
362 case v16i8: return 16;
364 case v8i16: return 8;
367 case v4f32: return 4;
369 case v3f32: return 3;
373 case v2f64: return 2;
374 case v1i64: return 1;
378 /// getSizeInBits - Return the size of the specified value type in bits.
379 unsigned getSizeInBits() const {
382 assert(isExtended() && "MVT has no known size!");
384 return getVectorElementType().getSizeInBits()*getVectorNumElements();
386 return ((V & PrecisionMask) >> SimpleTypeBits) + 1;
387 assert(false && "Unknown value type!");
391 case i16 : return 16;
393 case i32 : return 32;
400 case v2f32: return 64;
401 case f80 : return 80;
403 case v3f32: return 96;
412 case v2f64: return 128;
416 /// getStoreSizeInBits - Return the number of bits overwritten by a store
417 /// of the specified value type.
418 unsigned getStoreSizeInBits() const {
419 return (getSizeInBits() + 7)/8*8;
422 /// getRoundIntegerType - Rounds the bit-width of the given integer MVT up
423 /// to the nearest power of two (and at least to eight), and returns the
424 /// integer MVT with that number of bits.
425 MVT getRoundIntegerType() const {
426 assert(isInteger() && !isVector() && "Invalid integer type!");
427 unsigned BitWidth = getSizeInBits();
431 return getIntegerVT(1 << Log2_32_Ceil(BitWidth));
434 /// getIntegerVTBitMask - Return an integer with 1's every place there are
435 /// bits in the specified integer value type. FIXME: Should return an apint.
436 uint64_t getIntegerVTBitMask() const {
437 assert(isInteger() && !isVector() && "Only applies to int scalars!");
438 return ~uint64_t(0UL) >> (64-getSizeInBits());
441 /// getIntegerVTSignBit - Return an integer with a 1 in the position of the
442 /// sign bit for the specified integer value type. FIXME: Should return an
444 uint64_t getIntegerVTSignBit() const {
445 assert(isInteger() && !isVector() && "Only applies to int scalars!");
446 return uint64_t(1UL) << (getSizeInBits()-1);
449 /// getMVTString - This function returns value type as a string,
451 std::string getMVTString() const;
453 /// getTypeForMVT - This method returns an LLVM type corresponding to the
454 /// specified MVT. For integer types, this returns an unsigned type. Note
455 /// that this will abort for types that cannot be represented.
456 const Type *getTypeForMVT() const;
458 /// getMVT - Return the value type corresponding to the specified type.
459 /// This returns all pointers as iPTR. If HandleUnknown is true, unknown
460 /// types are returned as Other, otherwise they are invalid.
461 static MVT getMVT(const Type *Ty, bool HandleUnknown = false);
463 /// getRawBits - Represent the type as a bunch of bits.
464 uint32_t getRawBits() const { return V; }
466 /// compareRawBits - A meaningless but well-behaved order, useful for
467 /// constructing containers.
468 struct compareRawBits {
469 bool operator()(MVT L, MVT R) const {
470 return L.getRawBits() < R.getRawBits();
475 } // End llvm namespace