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/System/DataTypes.h"
22 #include "llvm/Support/MathExtras.h"
29 class MVT { // MVT = Machine Value Type
31 enum SimpleValueType {
32 // If you change this numbering, you must change the values in
33 // ValueTypes.td as well!
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
54 v16i8 = 15, // 16 x i8
55 v32i8 = 16, // 32 x i8
56 v2i16 = 17, // 2 x i16
57 v4i16 = 18, // 4 x i16
58 v8i16 = 19, // 8 x i16
59 v16i16 = 20, // 16 x i16
60 v2i32 = 21, // 2 x i32
61 v4i32 = 22, // 4 x i32
62 v8i32 = 23, // 8 x i32
63 v1i64 = 24, // 1 x i64
64 v2i64 = 25, // 2 x i64
65 v4i64 = 26, // 4 x i64
67 v2f32 = 27, // 2 x f32
68 v4f32 = 28, // 4 x f32
69 v8f32 = 29, // 8 x f32
70 v2f64 = 30, // 2 x f64
71 v4f64 = 31, // 4 x f64
73 FIRST_VECTOR_VALUETYPE = v2i8,
74 LAST_VECTOR_VALUETYPE = v4f64,
76 Flag = 32, // This glues nodes together during pre-RA sched
78 isVoid = 33, // This has no value
80 LAST_VALUETYPE = 34, // This always remains at the end of the list.
82 // This is the current maximum for LAST_VALUETYPE.
83 // EVT::MAX_ALLOWED_VALUETYPE is used for asserts and to size bit vectors
84 // This value must be a multiple of 32.
85 MAX_ALLOWED_VALUETYPE = 64,
87 // Metadata - This is MDNode or MDString.
90 // iPTRAny - An int value the size of the pointer of the current
91 // target to any address space. This must only be used internal to
92 // tblgen. Other than for overloading, we treat iPTRAny the same as iPTR.
95 // vAny - A vector with any length and element size. This is used
96 // for intrinsics that have overloadings based on vector types.
97 // This is only for tblgen's consumption!
100 // fAny - Any floating-point or vector floating-point value. This is used
101 // for intrinsics that have overloadings based on floating-point types.
102 // This is only for tblgen's consumption!
105 // iAny - An integer or vector integer value of any bit width. This is
106 // used for intrinsics that have overloadings based on integer bit widths.
107 // This is only for tblgen's consumption!
110 // iPTR - An int value the size of the pointer of the current
111 // target. This should only be used internal to tblgen!
114 // LastSimpleValueType - The greatest valid SimpleValueType value.
115 LastSimpleValueType = 255,
117 // INVALID_SIMPLE_VALUE_TYPE - Simple value types greater than or equal
118 // to this are considered extended value types.
119 INVALID_SIMPLE_VALUE_TYPE = LastSimpleValueType + 1
122 SimpleValueType SimpleTy;
124 MVT() : SimpleTy((SimpleValueType)(INVALID_SIMPLE_VALUE_TYPE)) {}
125 MVT(SimpleValueType SVT) : SimpleTy(SVT) { }
127 bool operator>(const MVT& S) const { return SimpleTy > S.SimpleTy; }
128 bool operator<(const MVT& S) const { return SimpleTy < S.SimpleTy; }
129 bool operator==(const MVT& S) const { return SimpleTy == S.SimpleTy; }
130 bool operator>=(const MVT& S) const { return SimpleTy >= S.SimpleTy; }
131 bool operator<=(const MVT& S) const { return SimpleTy <= S.SimpleTy; }
133 /// isFloatingPoint - Return true if this is a FP, or a vector FP type.
134 bool isFloatingPoint() const {
135 return ((SimpleTy >= MVT::f32 && SimpleTy <= MVT::ppcf128) ||
136 (SimpleTy >= MVT::v2f32 && SimpleTy <= MVT::v4f64));
139 /// isInteger - Return true if this is an integer, or a vector integer type.
140 bool isInteger() const {
141 return ((SimpleTy >= MVT::FIRST_INTEGER_VALUETYPE &&
142 SimpleTy <= MVT::LAST_INTEGER_VALUETYPE) ||
143 (SimpleTy >= MVT::v2i8 && SimpleTy <= MVT::v4i64));
146 /// isVector - Return true if this is a vector value type.
147 bool isVector() const {
148 return (SimpleTy >= MVT::FIRST_VECTOR_VALUETYPE &&
149 SimpleTy <= MVT::LAST_VECTOR_VALUETYPE);
152 /// isPow2VectorType - Retuns true if the given vector is a power of 2.
153 bool isPow2VectorType() const {
154 unsigned NElts = getVectorNumElements();
155 return !(NElts & (NElts - 1));
158 /// getPow2VectorType - Widens the length of the given vector EVT up to
159 /// the nearest power of 2 and returns that type.
160 MVT getPow2VectorType() const {
161 if (!isPow2VectorType()) {
162 unsigned NElts = getVectorNumElements();
163 unsigned Pow2NElts = 1 << Log2_32_Ceil(NElts);
164 return MVT::getVectorVT(getVectorElementType(), Pow2NElts);
171 /// getScalarType - If this is a vector type, return the element type,
172 /// otherwise return this.
173 MVT getScalarType() const {
174 return isVector() ? getVectorElementType() : *this;
177 MVT getVectorElementType() const {
180 return (MVT::SimpleValueType)(MVT::INVALID_SIMPLE_VALUE_TYPE);
185 case v32i8: return i8;
189 case v16i16: return i16;
192 case v8i32: return i32;
195 case v4i64: return i64;
198 case v8f32: return f32;
200 case v4f64: return f64;
204 unsigned getVectorNumElements() const {
208 case v32i8: return 32;
210 case v16i16: return 16;
214 case v8f32: return 8;
220 case v4f64: return 4;
226 case v2f64: return 2;
227 case v1i64: return 1;
231 unsigned getSizeInBits() const {
234 assert(0 && "Value type size is target-dependent. Ask TLI.");
238 assert(0 && "Value type is overloaded.");
240 assert(0 && "getSizeInBits called on extended MVT.");
244 case v2i8: return 16;
248 case v2i16: return 32;
255 case v2f32: return 64;
256 case f80 : return 80;
265 case v2f64: return 128;
271 case v4f64: return 256;
275 static MVT getFloatingPointVT(unsigned BitWidth) {
278 assert(false && "Bad bit width!");
290 static MVT getIntegerVT(unsigned BitWidth) {
293 return (MVT::SimpleValueType)(MVT::INVALID_SIMPLE_VALUE_TYPE);
309 static MVT getVectorVT(MVT VT, unsigned NumElements) {
310 switch (VT.SimpleTy) {
314 if (NumElements == 2) return MVT::v2i8;
315 if (NumElements == 4) return MVT::v4i8;
316 if (NumElements == 8) return MVT::v8i8;
317 if (NumElements == 16) return MVT::v16i8;
318 if (NumElements == 32) return MVT::v32i8;
321 if (NumElements == 2) return MVT::v2i16;
322 if (NumElements == 4) return MVT::v4i16;
323 if (NumElements == 8) return MVT::v8i16;
324 if (NumElements == 16) return MVT::v16i16;
327 if (NumElements == 2) return MVT::v2i32;
328 if (NumElements == 4) return MVT::v4i32;
329 if (NumElements == 8) return MVT::v8i32;
332 if (NumElements == 1) return MVT::v1i64;
333 if (NumElements == 2) return MVT::v2i64;
334 if (NumElements == 4) return MVT::v4i64;
337 if (NumElements == 2) return MVT::v2f32;
338 if (NumElements == 4) return MVT::v4f32;
339 if (NumElements == 8) return MVT::v8f32;
342 if (NumElements == 2) return MVT::v2f64;
343 if (NumElements == 4) return MVT::v4f64;
346 return (MVT::SimpleValueType)(MVT::INVALID_SIMPLE_VALUE_TYPE);
349 static MVT getIntVectorWithNumElements(unsigned NumElts) {
351 default: return (MVT::SimpleValueType)(MVT::INVALID_SIMPLE_VALUE_TYPE);
352 case 1: return MVT::v1i64;
353 case 2: return MVT::v2i32;
354 case 4: return MVT::v4i16;
355 case 8: return MVT::v8i8;
356 case 16: return MVT::v16i8;
361 struct EVT { // EVT = Extended Value Type
367 EVT() : V((MVT::SimpleValueType)(MVT::INVALID_SIMPLE_VALUE_TYPE)),
369 EVT(MVT::SimpleValueType SVT) : V(SVT), LLVMTy(0) { }
370 EVT(MVT S) : V(S), LLVMTy(0) {}
372 bool operator==(const EVT VT) const {
373 if (V.SimpleTy == VT.V.SimpleTy) {
374 if (V.SimpleTy == MVT::INVALID_SIMPLE_VALUE_TYPE)
375 return LLVMTy == VT.LLVMTy;
380 bool operator!=(const EVT VT) const {
381 if (V.SimpleTy == VT.V.SimpleTy) {
382 if (V.SimpleTy == MVT::INVALID_SIMPLE_VALUE_TYPE)
383 return LLVMTy != VT.LLVMTy;
389 /// getFloatingPointVT - Returns the EVT that represents a floating point
390 /// type with the given number of bits. There are two floating point types
391 /// with 128 bits - this returns f128 rather than ppcf128.
392 static EVT getFloatingPointVT(unsigned BitWidth) {
393 return MVT::getFloatingPointVT(BitWidth);
396 /// getIntegerVT - Returns the EVT that represents an integer with the given
398 static EVT getIntegerVT(LLVMContext &Context, unsigned BitWidth) {
399 MVT M = MVT::getIntegerVT(BitWidth);
400 if (M.SimpleTy == MVT::INVALID_SIMPLE_VALUE_TYPE)
401 return getExtendedIntegerVT(Context, BitWidth);
406 /// getVectorVT - Returns the EVT that represents a vector NumElements in
407 /// length, where each element is of type VT.
408 static EVT getVectorVT(LLVMContext &Context, EVT VT, unsigned NumElements) {
409 MVT M = MVT::getVectorVT(VT.V, NumElements);
410 if (M.SimpleTy == MVT::INVALID_SIMPLE_VALUE_TYPE)
411 return getExtendedVectorVT(Context, VT, NumElements);
416 /// getIntVectorWithNumElements - Return any integer vector type that has
417 /// the specified number of elements.
418 static EVT getIntVectorWithNumElements(LLVMContext &C, unsigned NumElts) {
419 MVT M = MVT::getIntVectorWithNumElements(NumElts);
420 if (M.SimpleTy == MVT::INVALID_SIMPLE_VALUE_TYPE)
421 return getVectorVT(C, MVT::i8, NumElts);
426 /// isSimple - Test if the given EVT is simple (as opposed to being
428 bool isSimple() const {
429 return V.SimpleTy <= MVT::LastSimpleValueType;
432 /// isExtended - Test if the given EVT is extended (as opposed to
434 bool isExtended() const {
438 /// isFloatingPoint - Return true if this is a FP, or a vector FP type.
439 bool isFloatingPoint() const {
441 ((V >= MVT::f32 && V <= MVT::ppcf128) ||
442 (V >= MVT::v2f32 && V <= MVT::v4f64)) : isExtendedFloatingPoint();
445 /// isInteger - Return true if this is an integer, or a vector integer type.
446 bool isInteger() const {
448 ((V >= MVT::FIRST_INTEGER_VALUETYPE &&
449 V <= MVT::LAST_INTEGER_VALUETYPE) ||
450 (V >= MVT::v2i8 && V <= MVT::v4i64)) : isExtendedInteger();
453 /// isVector - Return true if this is a vector value type.
454 bool isVector() const {
456 (V >= MVT::FIRST_VECTOR_VALUETYPE && V <=
457 MVT::LAST_VECTOR_VALUETYPE) :
461 /// is64BitVector - Return true if this is a 64-bit vector type.
462 bool is64BitVector() const {
464 (V==MVT::v8i8 || V==MVT::v4i16 || V==MVT::v2i32 ||
465 V==MVT::v1i64 || V==MVT::v2f32) :
466 isExtended64BitVector();
469 /// is128BitVector - Return true if this is a 128-bit vector type.
470 bool is128BitVector() const {
472 (V==MVT::v16i8 || V==MVT::v8i16 || V==MVT::v4i32 ||
473 V==MVT::v2i64 || V==MVT::v4f32 || V==MVT::v2f64) :
474 isExtended128BitVector();
477 /// is256BitVector - Return true if this is a 256-bit vector type.
478 inline bool is256BitVector() const {
480 (V==MVT::v8f32 || V==MVT::v4f64 || V==MVT::v32i8 ||
481 V==MVT::v16i16 || V==MVT::v8i32 || V==MVT::v4i64) :
482 isExtended256BitVector();
485 /// isOverloaded - Return true if this is an overloaded type for TableGen.
486 bool isOverloaded() const {
487 return (V==MVT::iAny || V==MVT::fAny || V==MVT::vAny || V==MVT::iPTRAny);
490 /// isByteSized - Return true if the bit size is a multiple of 8.
491 bool isByteSized() const {
492 return (getSizeInBits() & 7) == 0;
495 /// isRound - Return true if the size is a power-of-two number of bytes.
496 bool isRound() const {
497 unsigned BitSize = getSizeInBits();
498 return BitSize >= 8 && !(BitSize & (BitSize - 1));
501 /// bitsEq - Return true if this has the same number of bits as VT.
502 bool bitsEq(EVT VT) const {
503 return getSizeInBits() == VT.getSizeInBits();
506 /// bitsGT - Return true if this has more bits than VT.
507 bool bitsGT(EVT VT) const {
508 return getSizeInBits() > VT.getSizeInBits();
511 /// bitsGE - Return true if this has no less bits than VT.
512 bool bitsGE(EVT VT) const {
513 return getSizeInBits() >= VT.getSizeInBits();
516 /// bitsLT - Return true if this has less bits than VT.
517 bool bitsLT(EVT VT) const {
518 return getSizeInBits() < VT.getSizeInBits();
521 /// bitsLE - Return true if this has no more bits than VT.
522 bool bitsLE(EVT VT) const {
523 return getSizeInBits() <= VT.getSizeInBits();
527 /// getSimpleVT - Return the SimpleValueType held in the specified
529 MVT getSimpleVT() const {
530 assert(isSimple() && "Expected a SimpleValueType!");
534 /// getScalarType - If this is a vector type, return the element type,
535 /// otherwise return this.
536 EVT getScalarType() const {
537 return isVector() ? getVectorElementType() : *this;
540 /// getVectorElementType - Given a vector type, return the type of
542 EVT getVectorElementType() const {
543 assert(isVector() && "Invalid vector type!");
545 return V.getVectorElementType();
547 return getExtendedVectorElementType();
550 /// getVectorNumElements - Given a vector type, return the number of
551 /// elements it contains.
552 unsigned getVectorNumElements() const {
553 assert(isVector() && "Invalid vector type!");
555 return V.getVectorNumElements();
557 return getExtendedVectorNumElements();
560 /// getSizeInBits - Return the size of the specified value type in bits.
561 unsigned getSizeInBits() const {
563 return V.getSizeInBits();
565 return getExtendedSizeInBits();
568 /// getStoreSize - Return the number of bytes overwritten by a store
569 /// of the specified value type.
570 unsigned getStoreSize() const {
571 return (getSizeInBits() + 7) / 8;
574 /// getStoreSizeInBits - Return the number of bits overwritten by a store
575 /// of the specified value type.
576 unsigned getStoreSizeInBits() const {
577 return getStoreSize() * 8;
580 /// getRoundIntegerType - Rounds the bit-width of the given integer EVT up
581 /// to the nearest power of two (and at least to eight), and returns the
582 /// integer EVT with that number of bits.
583 EVT getRoundIntegerType(LLVMContext &Context) const {
584 assert(isInteger() && !isVector() && "Invalid integer type!");
585 unsigned BitWidth = getSizeInBits();
589 return getIntegerVT(Context, 1 << Log2_32_Ceil(BitWidth));
592 /// isPow2VectorType - Retuns true if the given vector is a power of 2.
593 bool isPow2VectorType() const {
594 unsigned NElts = getVectorNumElements();
595 return !(NElts & (NElts - 1));
598 /// getPow2VectorType - Widens the length of the given vector EVT up to
599 /// the nearest power of 2 and returns that type.
600 EVT getPow2VectorType(LLVMContext &Context) const {
601 if (!isPow2VectorType()) {
602 unsigned NElts = getVectorNumElements();
603 unsigned Pow2NElts = 1 << Log2_32_Ceil(NElts);
604 return EVT::getVectorVT(Context, getVectorElementType(), Pow2NElts);
611 /// getEVTString - This function returns value type as a string,
613 std::string getEVTString() const;
615 /// getTypeForEVT - This method returns an LLVM type corresponding to the
616 /// specified EVT. For integer types, this returns an unsigned type. Note
617 /// that this will abort for types that cannot be represented.
618 const Type *getTypeForEVT(LLVMContext &Context) const;
620 /// getEVT - Return the value type corresponding to the specified type.
621 /// This returns all pointers as iPTR. If HandleUnknown is true, unknown
622 /// types are returned as Other, otherwise they are invalid.
623 static EVT getEVT(const Type *Ty, bool HandleUnknown = false);
625 intptr_t getRawBits() {
626 if (V.SimpleTy <= MVT::LastSimpleValueType)
629 return (intptr_t)(LLVMTy);
632 /// compareRawBits - A meaningless but well-behaved order, useful for
633 /// constructing containers.
634 struct compareRawBits {
635 bool operator()(EVT L, EVT R) const {
636 if (L.V.SimpleTy == R.V.SimpleTy)
637 return L.LLVMTy < R.LLVMTy;
639 return L.V.SimpleTy < R.V.SimpleTy;
644 // Methods for handling the Extended-type case in functions above.
645 // These are all out-of-line to prevent users of this header file
646 // from having a dependency on Type.h.
647 static EVT getExtendedIntegerVT(LLVMContext &C, unsigned BitWidth);
648 static EVT getExtendedVectorVT(LLVMContext &C, EVT VT,
649 unsigned NumElements);
650 bool isExtendedFloatingPoint() const;
651 bool isExtendedInteger() const;
652 bool isExtendedVector() const;
653 bool isExtended64BitVector() const;
654 bool isExtended128BitVector() const;
655 bool isExtended256BitVector() const;
656 EVT getExtendedVectorElementType() const;
657 unsigned getExtendedVectorNumElements() const;
658 unsigned getExtendedSizeInBits() const;
661 } // End llvm namespace