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"
28 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
56 v16i8 = 17, // 16 x i8
57 v24i8 = 18, // 24 x i8
58 v32i8 = 19, // 32 x i8
59 v48i8 = 20, // 48 x i8
60 v64i8 = 21, // 64 x i8
62 v2i16 = 22, // 2 x i16
63 v4i16 = 23, // 4 x i16
64 v8i16 = 24, // 8 x i16
65 v12i16 = 25, // 12 x i16
66 v16i16 = 26, // 16 x i16
67 v24i16 = 27, // 24 x i16
68 v32i16 = 28, // 32 x i16
70 v2i32 = 29, // 2 x i32
71 v3i32 = 30, // 3 x i32
72 v4i32 = 31, // 4 x i32
73 v6i32 = 32, // 6 x i32
74 v8i32 = 33, // 8 x i32
75 v12i32 = 34, // 12 x i32
76 v16i32 = 35, // 16 x i32
78 v1i64 = 36, // 1 x i64
79 v2i64 = 37, // 2 x i64
80 v3i64 = 38, // 3 x i64
81 v4i64 = 39, // 4 x i64
82 v6i64 = 40, // 6 x i64
83 v8i64 = 41, // 8 x i64
85 v2f32 = 42, // 2 x f32
86 v3f32 = 43, // 3 x f32
87 v4f32 = 44, // 4 x f32
88 v6f32 = 45, // 6 x f32
89 v8f32 = 46, // 8 x f32
90 v12f32 = 47, // 12 x f32
91 v16f32 = 48, // 16 x f32
93 v2f64 = 49, // 2 x f64
94 v4f64 = 50, // 4 x f64
96 FIRST_VECTOR_VALUETYPE = v2i8,
97 LAST_VECTOR_VALUETYPE = v4f64,
99 LAST_VALUETYPE = 51, // This always remains at the end of the list.
101 // This is the current maximum for LAST_VALUETYPE.
102 // MVT::MAX_ALLOWED_VALUETYPE is used for asserts and to size bit vectors
103 // This value must be a multiple of 32.
104 MAX_ALLOWED_VALUETYPE = 64,
106 // Metadata - This is MDNode or MDString.
109 // iPTRAny - An int value the size of the pointer of the current
110 // target to any address space. This must only be used internal to
111 // tblgen. Other than for overloading, we treat iPTRAny the same as iPTR.
114 // fAny - Any floating-point or vector floating-point value. This is used
115 // for intrinsics that have overloadings based on floating-point types.
116 // This is only for tblgen's consumption!
119 // iAny - An integer or vector integer value of any bit width. This is
120 // used for intrinsics that have overloadings based on integer bit widths.
121 // This is only for tblgen's consumption!
124 // iPTR - An int value the size of the pointer of the current
125 // target. This should only be used internal to tblgen!
128 // LastSimpleValueType - The greatest valid SimpleValueType value.
129 LastSimpleValueType = 255
133 /// This union holds low-level value types. Valid values include any of
134 /// the values in the SimpleValueType enum, or any value returned from one
135 /// of the MVT methods. Any value type equal to one of the SimpleValueType
136 /// enum values is a "simple" value type. All others are "extended".
138 /// Note that simple doesn't necessary mean legal for the target machine.
139 /// All legal value types must be simple, but often there are some simple
140 /// value types that are not legal.
149 MVT(SimpleValueType S) : V(S) {}
151 bool operator==(const MVT VT) const {
152 return getRawBits() == VT.getRawBits();
154 bool operator!=(const MVT VT) const {
155 return getRawBits() != VT.getRawBits();
158 /// getFloatingPointVT - Returns the MVT that represents a floating point
159 /// type with the given number of bits. There are two floating point types
160 /// with 128 bits - this returns f128 rather than ppcf128.
161 static MVT getFloatingPointVT(unsigned BitWidth) {
164 assert(false && "Bad bit width!");
176 /// getIntegerVT - Returns the MVT that represents an integer with the given
178 static MVT getIntegerVT(unsigned BitWidth) {
195 return getExtendedIntegerVT(BitWidth);
198 /// getVectorVT - Returns the MVT that represents a vector NumElements in
199 /// length, where each element is of type VT.
200 static MVT getVectorVT(MVT VT, unsigned NumElements) {
205 if (NumElements == 2) return v2i8;
206 if (NumElements == 4) return v4i8;
207 if (NumElements == 8) return v8i8;
208 if (NumElements == 16) return v16i8;
209 if (NumElements == 24) return v24i8;
210 if (NumElements == 32) return v32i8;
211 if (NumElements == 48) return v48i8;
212 if (NumElements == 64) return v64i8;
215 if (NumElements == 2) return v2i16;
216 if (NumElements == 4) return v4i16;
217 if (NumElements == 8) return v8i16;
218 if (NumElements == 12) return v12i16;
219 if (NumElements == 16) return v16i16;
220 if (NumElements == 24) return v24i16;
221 if (NumElements == 32) return v32i16;
224 if (NumElements == 2) return v2i32;
225 if (NumElements == 3) return v3i32;
226 if (NumElements == 4) return v4i32;
227 if (NumElements == 6) return v6i32;
228 if (NumElements == 8) return v8i32;
229 if (NumElements == 12) return v12i32;
230 if (NumElements == 16) return v16i32;
233 if (NumElements == 1) return v1i64;
234 if (NumElements == 2) return v2i64;
235 if (NumElements == 3) return v3i64;
236 if (NumElements == 4) return v4i64;
237 if (NumElements == 6) return v6i64;
238 if (NumElements == 8) return v8i64;
241 if (NumElements == 2) return v2f32;
242 if (NumElements == 3) return v3f32;
243 if (NumElements == 4) return v4f32;
244 if (NumElements == 6) return v6f32;
245 if (NumElements == 8) return v8f32;
246 if (NumElements == 12) return v12f32;
247 if (NumElements == 16) return v16f32;
250 if (NumElements == 2) return v2f64;
251 if (NumElements == 4) return v4f64;
254 return getExtendedVectorVT(VT, NumElements);
257 /// getIntVectorWithNumElements - Return any integer vector type that has
258 /// the specified number of elements.
259 static MVT getIntVectorWithNumElements(unsigned NumElts) {
261 default: return getVectorVT(i8, NumElts);
262 case 1: return v1i64;
263 case 2: return v2i32;
264 case 3: return v3i32;
265 case 4: return v4i16;
267 case 16: return v16i8;
271 /// isSimple - Test if the given MVT is simple (as opposed to being
273 bool isSimple() const {
274 return V <= LastSimpleValueType;
277 /// isExtended - Test if the given MVT is extended (as opposed to
279 bool isExtended() const {
283 /// isFloatingPoint - Return true if this is a FP, or a vector FP type.
284 bool isFloatingPoint() const {
286 ((V >= f32 && V <= ppcf128) ||
287 (V >= v2f32 && V <= v4f64)) : isExtendedFloatingPoint();
290 /// isInteger - Return true if this is an integer, or a vector integer type.
291 bool isInteger() const {
293 ((V >= FIRST_INTEGER_VALUETYPE && V <= LAST_INTEGER_VALUETYPE) ||
294 (V >= v2i8 && V <= v8i64)) : isExtendedInteger();
297 /// isVector - Return true if this is a vector value type.
298 bool isVector() const {
300 (V >= FIRST_VECTOR_VALUETYPE && V <= LAST_VECTOR_VALUETYPE) :
304 /// is64BitVector - Return true if this is a 64-bit vector type.
305 bool is64BitVector() const {
307 (V==v8i8 || V==v4i16 || V==v2i32 || V==v1i64 || V==v2f32) :
308 isExtended64BitVector();
311 /// is128BitVector - Return true if this is a 128-bit vector type.
312 bool is128BitVector() const {
314 (V==v16i8 || V==v8i16 || V==v4i32 ||
315 V==v2i64 || V==v4f32 || V==v2f64) :
316 isExtended128BitVector();
319 /// is256BitVector - Return true if this is a 256-bit vector type.
320 inline bool is256BitVector() const {
322 (V==v8f32 || V==v4f64 || V==v32i8 || V==v16i16 || V==v8i32 ||
323 V==v4i64) : isExtended256BitVector();
326 /// isByteSized - Return true if the bit size is a multiple of 8.
327 bool isByteSized() const {
328 return (getSizeInBits() & 7) == 0;
331 /// isRound - Return true if the size is a power-of-two number of bytes.
332 bool isRound() const {
333 unsigned BitSize = getSizeInBits();
334 return BitSize >= 8 && !(BitSize & (BitSize - 1));
337 /// bitsEq - Return true if this has the same number of bits as VT.
338 bool bitsEq(MVT VT) const {
339 return getSizeInBits() == VT.getSizeInBits();
342 /// bitsGT - Return true if this has more bits than VT.
343 bool bitsGT(MVT VT) const {
344 return getSizeInBits() > VT.getSizeInBits();
347 /// bitsGE - Return true if this has no less bits than VT.
348 bool bitsGE(MVT VT) const {
349 return getSizeInBits() >= VT.getSizeInBits();
352 /// bitsLT - Return true if this has less bits than VT.
353 bool bitsLT(MVT VT) const {
354 return getSizeInBits() < VT.getSizeInBits();
357 /// bitsLE - Return true if this has no more bits than VT.
358 bool bitsLE(MVT VT) const {
359 return getSizeInBits() <= VT.getSizeInBits();
363 /// getSimpleVT - Return the SimpleValueType held in the specified
365 SimpleValueType getSimpleVT() const {
366 assert(isSimple() && "Expected a SimpleValueType!");
367 return SimpleValueType(V);
370 /// getVectorElementType - Given a vector type, return the type of
372 MVT getVectorElementType() const {
373 assert(isVector() && "Invalid vector type!");
376 return getExtendedVectorElementType();
384 case v64i8: return i8;
391 case v32i16: return i16;
398 case v16i32: return i32;
404 case v8i64: return i64;
411 case v16f32: return f32;
413 case v4f64: return f64;
417 /// getVectorNumElements - Given a vector type, return the number of
418 /// elements it contains.
419 unsigned getVectorNumElements() const {
420 assert(isVector() && "Invalid vector type!");
423 return getExtendedVectorNumElements();
424 case v64i8: return 64;
425 case v48i8: return 48;
427 case v32i16: return 32;
429 case v24i16: return 24;
433 case v16f32: return 16;
436 case v12f32: return 12;
441 case v8f32: return 8;
444 case v6f32: return 6;
450 case v4f64: return 4;
453 case v3f32: return 3;
459 case v2f64: return 2;
460 case v1i64: return 1;
464 /// getSizeInBits - Return the size of the specified value type in bits.
465 unsigned getSizeInBits() const {
468 assert(0 && "Value type size is target-dependent. Ask TLI.");
472 assert(0 && "Value type is overloaded.");
474 return getExtendedSizeInBits();
478 case v2i8: return 16;
482 case v2i16: return 32;
489 case v2f32: return 64;
490 case f80 : return 80;
492 case v3f32: return 96;
501 case v2f64: return 128;
506 case v6f32: return 192;
512 case v4f64: return 256;
517 case v12f32: return 384;
522 case v16f32: return 512;
526 /// getStoreSizeInBits - Return the number of bits overwritten by a store
527 /// of the specified value type.
528 unsigned getStoreSizeInBits() const {
529 return (getSizeInBits() + 7)/8*8;
532 /// getRoundIntegerType - Rounds the bit-width of the given integer MVT up
533 /// to the nearest power of two (and at least to eight), and returns the
534 /// integer MVT with that number of bits.
535 MVT getRoundIntegerType() const {
536 assert(isInteger() && !isVector() && "Invalid integer type!");
537 unsigned BitWidth = getSizeInBits();
541 return getIntegerVT(1 << Log2_32_Ceil(BitWidth));
544 /// isPow2VectorType - Retuns true if the given vector is a power of 2.
545 bool isPow2VectorType() const {
546 unsigned NElts = getVectorNumElements();
547 return !(NElts & (NElts - 1));
550 /// getPow2VectorType - Widens the length of the given vector MVT up to
551 /// the nearest power of 2 and returns that type.
552 MVT getPow2VectorType() const {
553 if (!isPow2VectorType()) {
554 unsigned NElts = getVectorNumElements();
555 unsigned Pow2NElts = 1 << Log2_32_Ceil(NElts);
556 return MVT::getVectorVT(getVectorElementType(), Pow2NElts);
563 /// getMVTString - This function returns value type as a string,
565 std::string getMVTString() const;
567 /// getTypeForMVT - This method returns an LLVM type corresponding to the
568 /// specified MVT. For integer types, this returns an unsigned type. Note
569 /// that this will abort for types that cannot be represented.
570 const Type *getTypeForMVT(LLVMContext &Context) const;
572 /// getMVT - Return the value type corresponding to the specified type.
573 /// This returns all pointers as iPTR. If HandleUnknown is true, unknown
574 /// types are returned as Other, otherwise they are invalid.
575 static MVT getMVT(const Type *Ty, bool HandleUnknown = false);
577 /// getRawBits - Represent the type as a bunch of bits.
578 uintptr_t getRawBits() const { return V; }
580 /// compareRawBits - A meaningless but well-behaved order, useful for
581 /// constructing containers.
582 struct compareRawBits {
583 bool operator()(MVT L, MVT R) const {
584 return L.getRawBits() < R.getRawBits();
589 // Methods for handling the Extended-type case in functions above.
590 // These are all out-of-line to prevent users of this header file
591 // from having a dependency on Type.h.
592 static MVT getExtendedIntegerVT(unsigned BitWidth);
593 static MVT getExtendedVectorVT(MVT VT, unsigned NumElements);
594 bool isExtendedFloatingPoint() const;
595 bool isExtendedInteger() const;
596 bool isExtendedVector() const;
597 bool isExtended64BitVector() const;
598 bool isExtended128BitVector() const;
599 bool isExtended256BitVector() const;
600 MVT getExtendedVectorElementType() const;
601 unsigned getExtendedVectorNumElements() const;
602 unsigned getExtendedSizeInBits() const;
605 } // End llvm namespace