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
19 #include "llvm/Support/DataTypes.h"
20 #include "llvm/Support/ErrorHandling.h"
21 #include "llvm/Support/MathExtras.h"
30 /// MVT - Machine Value Type. Every type that is supported natively by some
31 /// processor targeted by LLVM occurs here. This means that any legal value
32 /// type can be represented by a MVT.
35 enum SimpleValueType {
36 // If you change this numbering, you must change the values in
37 // ValueTypes.td as well!
38 Other = 0, // This is a non-standard value
39 i1 = 1, // This is a 1 bit integer value
40 i8 = 2, // This is an 8 bit integer value
41 i16 = 3, // This is a 16 bit integer value
42 i32 = 4, // This is a 32 bit integer value
43 i64 = 5, // This is a 64 bit integer value
44 i128 = 6, // This is a 128 bit integer value
46 FIRST_INTEGER_VALUETYPE = i1,
47 LAST_INTEGER_VALUETYPE = i128,
49 f16 = 7, // This is a 16 bit floating point value
50 f32 = 8, // This is a 32 bit floating point value
51 f64 = 9, // This is a 64 bit floating point value
52 f80 = 10, // This is a 80 bit floating point value
53 f128 = 11, // This is a 128 bit floating point value
54 ppcf128 = 12, // This is a PPC 128-bit floating point value
56 FIRST_FP_VALUETYPE = f16,
57 LAST_FP_VALUETYPE = ppcf128,
62 v16i8 = 16, // 16 x i8
63 v32i8 = 17, // 32 x i8
64 v2i16 = 18, // 2 x i16
65 v4i16 = 19, // 4 x i16
66 v8i16 = 20, // 8 x i16
67 v16i16 = 21, // 16 x i16
68 v2i32 = 22, // 2 x i32
69 v4i32 = 23, // 4 x i32
70 v8i32 = 24, // 8 x i32
71 v16i32 = 25, // 16 x i32
72 v1i64 = 26, // 1 x i64
73 v2i64 = 27, // 2 x i64
74 v4i64 = 28, // 4 x i64
75 v8i64 = 29, // 8 x i64
76 v16i64 = 30, // 16 x i64
78 v2f16 = 31, // 2 x f16
79 v2f32 = 32, // 2 x f32
80 v4f32 = 33, // 4 x f32
81 v8f32 = 34, // 8 x f32
82 v2f64 = 35, // 2 x f64
83 v4f64 = 36, // 4 x f64
85 FIRST_VECTOR_VALUETYPE = v2i8,
86 LAST_VECTOR_VALUETYPE = v4f64,
87 FIRST_INTEGER_VECTOR_VALUETYPE = v2i8,
88 LAST_INTEGER_VECTOR_VALUETYPE = v16i64,
89 FIRST_FP_VECTOR_VALUETYPE = v2f16,
90 LAST_FP_VECTOR_VALUETYPE = v4f64,
92 x86mmx = 37, // This is an X86 MMX value
94 Glue = 38, // This glues nodes together during pre-RA sched
96 isVoid = 39, // This has no value
98 Untyped = 40, // This value takes a register, but has
99 // unspecified type. The register class
100 // will be determined by the opcode.
102 LAST_VALUETYPE = 41, // This always remains at the end of the list.
104 // This is the current maximum for LAST_VALUETYPE.
105 // MVT::MAX_ALLOWED_VALUETYPE is used for asserts and to size bit vectors
106 // This value must be a multiple of 32.
107 MAX_ALLOWED_VALUETYPE = 64,
109 // Metadata - This is MDNode or MDString.
112 // iPTRAny - An int value the size of the pointer of the current
113 // target to any address space. This must only be used internal to
114 // tblgen. Other than for overloading, we treat iPTRAny the same as iPTR.
117 // vAny - A vector with any length and element size. This is used
118 // for intrinsics that have overloadings based on vector types.
119 // This is only for tblgen's consumption!
122 // fAny - Any floating-point or vector floating-point value. This is used
123 // for intrinsics that have overloadings based on floating-point types.
124 // This is only for tblgen's consumption!
127 // iAny - An integer or vector integer value of any bit width. This is
128 // used for intrinsics that have overloadings based on integer bit widths.
129 // This is only for tblgen's consumption!
132 // iPTR - An int value the size of the pointer of the current
133 // target. This should only be used internal to tblgen!
136 // LastSimpleValueType - The greatest valid SimpleValueType value.
137 LastSimpleValueType = 255,
139 // INVALID_SIMPLE_VALUE_TYPE - Simple value types greater than or equal
140 // to this are considered extended value types.
141 INVALID_SIMPLE_VALUE_TYPE = LastSimpleValueType + 1
144 SimpleValueType SimpleTy;
146 MVT() : SimpleTy((SimpleValueType)(INVALID_SIMPLE_VALUE_TYPE)) {}
147 MVT(SimpleValueType SVT) : SimpleTy(SVT) { }
149 bool operator>(const MVT& S) const { return SimpleTy > S.SimpleTy; }
150 bool operator<(const MVT& S) const { return SimpleTy < S.SimpleTy; }
151 bool operator==(const MVT& S) const { return SimpleTy == S.SimpleTy; }
152 bool operator!=(const MVT& S) const { return SimpleTy != S.SimpleTy; }
153 bool operator>=(const MVT& S) const { return SimpleTy >= S.SimpleTy; }
154 bool operator<=(const MVT& S) const { return SimpleTy <= S.SimpleTy; }
156 /// isFloatingPoint - Return true if this is a FP, or a vector FP type.
157 bool isFloatingPoint() const {
158 return ((SimpleTy >= MVT::FIRST_FP_VALUETYPE &&
159 SimpleTy <= MVT::LAST_FP_VALUETYPE) ||
160 (SimpleTy >= MVT::FIRST_FP_VECTOR_VALUETYPE &&
161 SimpleTy <= MVT::LAST_FP_VECTOR_VALUETYPE));
164 /// isInteger - Return true if this is an integer, or a vector integer type.
165 bool isInteger() const {
166 return ((SimpleTy >= MVT::FIRST_INTEGER_VALUETYPE &&
167 SimpleTy <= MVT::LAST_INTEGER_VALUETYPE) ||
168 (SimpleTy >= MVT::FIRST_INTEGER_VECTOR_VALUETYPE &&
169 SimpleTy <= MVT::LAST_INTEGER_VECTOR_VALUETYPE));
172 /// isVector - Return true if this is a vector value type.
173 bool isVector() const {
174 return (SimpleTy >= MVT::FIRST_VECTOR_VALUETYPE &&
175 SimpleTy <= MVT::LAST_VECTOR_VALUETYPE);
178 /// is64BitVector - Return true if this is a 64-bit vector type.
179 bool is64BitVector() const {
180 return (SimpleTy == MVT::v8i8 || SimpleTy == MVT::v4i16 ||
181 SimpleTy == MVT::v2i32 || SimpleTy == MVT::v1i64 ||
182 SimpleTy == MVT::v2f32);
185 /// is128BitVector - Return true if this is a 128-bit vector type.
186 bool is128BitVector() const {
187 return (SimpleTy == MVT::v16i8 || SimpleTy == MVT::v8i16 ||
188 SimpleTy == MVT::v4i32 || SimpleTy == MVT::v2i64 ||
189 SimpleTy == MVT::v4f32 || SimpleTy == MVT::v2f64);
192 /// is256BitVector - Return true if this is a 256-bit vector type.
193 bool is256BitVector() const {
194 return (SimpleTy == MVT::v8f32 || SimpleTy == MVT::v4f64 ||
195 SimpleTy == MVT::v32i8 || SimpleTy == MVT::v16i16 ||
196 SimpleTy == MVT::v8i32 || SimpleTy == MVT::v4i64);
199 /// is512BitVector - Return true if this is a 512-bit vector type.
200 bool is512BitVector() const {
201 return (SimpleTy == MVT::v8i64 || SimpleTy == MVT::v16i32);
204 /// is1024BitVector - Return true if this is a 1024-bit vector type.
205 bool is1024BitVector() const {
206 return (SimpleTy == MVT::v16i64);
209 /// isPow2VectorType - Returns true if the given vector is a power of 2.
210 bool isPow2VectorType() const {
211 unsigned NElts = getVectorNumElements();
212 return !(NElts & (NElts - 1));
215 /// getPow2VectorType - Widens the length of the given vector MVT up to
216 /// the nearest power of 2 and returns that type.
217 MVT getPow2VectorType() const {
218 if (isPow2VectorType())
221 unsigned NElts = getVectorNumElements();
222 unsigned Pow2NElts = 1 << Log2_32_Ceil(NElts);
223 return MVT::getVectorVT(getVectorElementType(), Pow2NElts);
226 /// getScalarType - If this is a vector type, return the element type,
227 /// otherwise return this.
228 MVT getScalarType() const {
229 return isVector() ? getVectorElementType() : *this;
232 MVT getVectorElementType() const {
235 llvm_unreachable("Not a vector MVT!");
240 case v32i8: return i8;
244 case v16i16: return i16;
248 case v16i32: return i32;
253 case v16i64: return i64;
254 case v2f16: return f16;
257 case v8f32: return f32;
259 case v4f64: return f64;
263 unsigned getVectorNumElements() const {
266 llvm_unreachable("Not a vector MVT!");
267 case v32i8: return 32;
271 case v16i64:return 16;
276 case v8f32: return 8;
282 case v4f64: return 4;
289 case v2f64: return 2;
290 case v1i64: return 1;
294 unsigned getSizeInBits() const {
297 llvm_unreachable("Value type size is target-dependent. Ask TLI.");
301 llvm_unreachable("Value type is overloaded.");
303 llvm_unreachable("getSizeInBits called on extended MVT.");
308 case v2i8: return 16;
313 case v2f16: return 32;
321 case v2f32: return 64;
322 case f80 : return 80;
331 case v2f64: return 128;
337 case v4f64: return 256;
339 case v8i64: return 512;
340 case v16i64:return 1024;
344 /// getStoreSize - Return the number of bytes overwritten by a store
345 /// of the specified value type.
346 unsigned getStoreSize() const {
347 return (getSizeInBits() + 7) / 8;
350 /// getStoreSizeInBits - Return the number of bits overwritten by a store
351 /// of the specified value type.
352 unsigned getStoreSizeInBits() const {
353 return getStoreSize() * 8;
356 static MVT getFloatingPointVT(unsigned BitWidth) {
359 llvm_unreachable("Bad bit width!");
373 static MVT getIntegerVT(unsigned BitWidth) {
376 return (MVT::SimpleValueType)(MVT::INVALID_SIMPLE_VALUE_TYPE);
392 static MVT getVectorVT(MVT VT, unsigned NumElements) {
393 switch (VT.SimpleTy) {
397 if (NumElements == 2) return MVT::v2i8;
398 if (NumElements == 4) return MVT::v4i8;
399 if (NumElements == 8) return MVT::v8i8;
400 if (NumElements == 16) return MVT::v16i8;
401 if (NumElements == 32) return MVT::v32i8;
404 if (NumElements == 2) return MVT::v2i16;
405 if (NumElements == 4) return MVT::v4i16;
406 if (NumElements == 8) return MVT::v8i16;
407 if (NumElements == 16) return MVT::v16i16;
410 if (NumElements == 2) return MVT::v2i32;
411 if (NumElements == 4) return MVT::v4i32;
412 if (NumElements == 8) return MVT::v8i32;
413 if (NumElements == 16) return MVT::v16i32;
416 if (NumElements == 1) return MVT::v1i64;
417 if (NumElements == 2) return MVT::v2i64;
418 if (NumElements == 4) return MVT::v4i64;
419 if (NumElements == 8) return MVT::v8i64;
420 if (NumElements == 16) return MVT::v16i64;
423 if (NumElements == 2) return MVT::v2f16;
426 if (NumElements == 2) return MVT::v2f32;
427 if (NumElements == 4) return MVT::v4f32;
428 if (NumElements == 8) return MVT::v8f32;
431 if (NumElements == 2) return MVT::v2f64;
432 if (NumElements == 4) return MVT::v4f64;
435 return (MVT::SimpleValueType)(MVT::INVALID_SIMPLE_VALUE_TYPE);
440 /// EVT - Extended Value Type. Capable of holding value types which are not
441 /// native for any processor (such as the i12345 type), as well as the types
442 /// a MVT can represent.
449 EVT() : V((MVT::SimpleValueType)(MVT::INVALID_SIMPLE_VALUE_TYPE)),
451 EVT(MVT::SimpleValueType SVT) : V(SVT), LLVMTy(0) { }
452 EVT(MVT S) : V(S), LLVMTy(0) {}
454 bool operator==(EVT VT) const {
455 return !(*this != VT);
457 bool operator!=(EVT VT) const {
458 if (V.SimpleTy != VT.V.SimpleTy)
460 if (V.SimpleTy == MVT::INVALID_SIMPLE_VALUE_TYPE)
461 return LLVMTy != VT.LLVMTy;
465 /// getFloatingPointVT - Returns the EVT that represents a floating point
466 /// type with the given number of bits. There are two floating point types
467 /// with 128 bits - this returns f128 rather than ppcf128.
468 static EVT getFloatingPointVT(unsigned BitWidth) {
469 return MVT::getFloatingPointVT(BitWidth);
472 /// getIntegerVT - Returns the EVT that represents an integer with the given
474 static EVT getIntegerVT(LLVMContext &Context, unsigned BitWidth) {
475 MVT M = MVT::getIntegerVT(BitWidth);
476 if (M.SimpleTy != MVT::INVALID_SIMPLE_VALUE_TYPE)
478 return getExtendedIntegerVT(Context, BitWidth);
481 /// getVectorVT - Returns the EVT that represents a vector NumElements in
482 /// length, where each element is of type VT.
483 static EVT getVectorVT(LLVMContext &Context, EVT VT, unsigned NumElements) {
484 MVT M = MVT::getVectorVT(VT.V, NumElements);
485 if (M.SimpleTy != MVT::INVALID_SIMPLE_VALUE_TYPE)
487 return getExtendedVectorVT(Context, VT, NumElements);
490 /// changeVectorElementTypeToInteger - Return a vector with the same number
491 /// of elements as this vector, but with the element type converted to an
492 /// integer type with the same bitwidth.
493 EVT changeVectorElementTypeToInteger() const {
495 return changeExtendedVectorElementTypeToInteger();
496 MVT EltTy = getSimpleVT().getVectorElementType();
497 unsigned BitWidth = EltTy.getSizeInBits();
498 MVT IntTy = MVT::getIntegerVT(BitWidth);
499 MVT VecTy = MVT::getVectorVT(IntTy, getVectorNumElements());
500 assert(VecTy != MVT::INVALID_SIMPLE_VALUE_TYPE &&
501 "Simple vector VT not representable by simple integer vector VT!");
505 /// isSimple - Test if the given EVT is simple (as opposed to being
507 bool isSimple() const {
508 return V.SimpleTy <= MVT::LastSimpleValueType;
511 /// isExtended - Test if the given EVT is extended (as opposed to
513 bool isExtended() const {
517 /// isFloatingPoint - Return true if this is a FP, or a vector FP type.
518 bool isFloatingPoint() const {
519 return isSimple() ? V.isFloatingPoint() : isExtendedFloatingPoint();
522 /// isInteger - Return true if this is an integer, or a vector integer type.
523 bool isInteger() const {
524 return isSimple() ? V.isInteger() : isExtendedInteger();
527 /// isVector - Return true if this is a vector value type.
528 bool isVector() const {
529 return isSimple() ? V.isVector() : isExtendedVector();
532 /// is64BitVector - Return true if this is a 64-bit vector type.
533 bool is64BitVector() const {
534 return isSimple() ? V.is64BitVector() : isExtended64BitVector();
537 /// is128BitVector - Return true if this is a 128-bit vector type.
538 bool is128BitVector() const {
539 return isSimple() ? V.is128BitVector() : isExtended128BitVector();
542 /// is256BitVector - Return true if this is a 256-bit vector type.
543 bool is256BitVector() const {
544 return isSimple() ? V.is256BitVector() : isExtended256BitVector();
547 /// is512BitVector - Return true if this is a 512-bit vector type.
548 bool is512BitVector() const {
549 return isSimple() ? V.is512BitVector() : isExtended512BitVector();
552 /// is1024BitVector - Return true if this is a 1024-bit vector type.
553 bool is1024BitVector() const {
554 return isSimple() ? V.is1024BitVector() : isExtended1024BitVector();
557 /// isOverloaded - Return true if this is an overloaded type for TableGen.
558 bool isOverloaded() const {
559 return (V==MVT::iAny || V==MVT::fAny || V==MVT::vAny || V==MVT::iPTRAny);
562 /// isByteSized - Return true if the bit size is a multiple of 8.
563 bool isByteSized() const {
564 return (getSizeInBits() & 7) == 0;
567 /// isRound - Return true if the size is a power-of-two number of bytes.
568 bool isRound() const {
569 unsigned BitSize = getSizeInBits();
570 return BitSize >= 8 && !(BitSize & (BitSize - 1));
573 /// bitsEq - Return true if this has the same number of bits as VT.
574 bool bitsEq(EVT VT) const {
575 if (EVT::operator==(VT)) return true;
576 return getSizeInBits() == VT.getSizeInBits();
579 /// bitsGT - Return true if this has more bits than VT.
580 bool bitsGT(EVT VT) const {
581 if (EVT::operator==(VT)) return false;
582 return getSizeInBits() > VT.getSizeInBits();
585 /// bitsGE - Return true if this has no less bits than VT.
586 bool bitsGE(EVT VT) const {
587 if (EVT::operator==(VT)) return true;
588 return getSizeInBits() >= VT.getSizeInBits();
591 /// bitsLT - Return true if this has less bits than VT.
592 bool bitsLT(EVT VT) const {
593 if (EVT::operator==(VT)) return false;
594 return getSizeInBits() < VT.getSizeInBits();
597 /// bitsLE - Return true if this has no more bits than VT.
598 bool bitsLE(EVT VT) const {
599 if (EVT::operator==(VT)) return true;
600 return getSizeInBits() <= VT.getSizeInBits();
604 /// getSimpleVT - Return the SimpleValueType held in the specified
606 MVT getSimpleVT() const {
607 assert(isSimple() && "Expected a SimpleValueType!");
611 /// getScalarType - If this is a vector type, return the element type,
612 /// otherwise return this.
613 EVT getScalarType() const {
614 return isVector() ? getVectorElementType() : *this;
617 /// getVectorElementType - Given a vector type, return the type of
619 EVT getVectorElementType() const {
620 assert(isVector() && "Invalid vector type!");
622 return V.getVectorElementType();
623 return getExtendedVectorElementType();
626 /// getVectorNumElements - Given a vector type, return the number of
627 /// elements it contains.
628 unsigned getVectorNumElements() const {
629 assert(isVector() && "Invalid vector type!");
631 return V.getVectorNumElements();
632 return getExtendedVectorNumElements();
635 /// getSizeInBits - Return the size of the specified value type in bits.
636 unsigned getSizeInBits() const {
638 return V.getSizeInBits();
639 return getExtendedSizeInBits();
642 /// getStoreSize - Return the number of bytes overwritten by a store
643 /// of the specified value type.
644 unsigned getStoreSize() const {
645 return (getSizeInBits() + 7) / 8;
648 /// getStoreSizeInBits - Return the number of bits overwritten by a store
649 /// of the specified value type.
650 unsigned getStoreSizeInBits() const {
651 return getStoreSize() * 8;
654 /// getRoundIntegerType - Rounds the bit-width of the given integer EVT up
655 /// to the nearest power of two (and at least to eight), and returns the
656 /// integer EVT with that number of bits.
657 EVT getRoundIntegerType(LLVMContext &Context) const {
658 assert(isInteger() && !isVector() && "Invalid integer type!");
659 unsigned BitWidth = getSizeInBits();
662 return getIntegerVT(Context, 1 << Log2_32_Ceil(BitWidth));
665 /// getHalfSizedIntegerVT - Finds the smallest simple value type that is
666 /// greater than or equal to half the width of this EVT. If no simple
667 /// value type can be found, an extended integer value type of half the
668 /// size (rounded up) is returned.
669 EVT getHalfSizedIntegerVT(LLVMContext &Context) const {
670 assert(isInteger() && !isVector() && "Invalid integer type!");
671 unsigned EVTSize = getSizeInBits();
672 for (unsigned IntVT = MVT::FIRST_INTEGER_VALUETYPE;
673 IntVT <= MVT::LAST_INTEGER_VALUETYPE; ++IntVT) {
674 EVT HalfVT = EVT((MVT::SimpleValueType)IntVT);
675 if (HalfVT.getSizeInBits() * 2 >= EVTSize)
678 return getIntegerVT(Context, (EVTSize + 1) / 2);
681 /// isPow2VectorType - Returns true if the given vector is a power of 2.
682 bool isPow2VectorType() const {
683 unsigned NElts = getVectorNumElements();
684 return !(NElts & (NElts - 1));
687 /// getPow2VectorType - Widens the length of the given vector EVT up to
688 /// the nearest power of 2 and returns that type.
689 EVT getPow2VectorType(LLVMContext &Context) const {
690 if (!isPow2VectorType()) {
691 unsigned NElts = getVectorNumElements();
692 unsigned Pow2NElts = 1 << Log2_32_Ceil(NElts);
693 return EVT::getVectorVT(Context, getVectorElementType(), Pow2NElts);
700 /// getEVTString - This function returns value type as a string,
702 std::string getEVTString() const;
704 /// getTypeForEVT - This method returns an LLVM type corresponding to the
705 /// specified EVT. For integer types, this returns an unsigned type. Note
706 /// that this will abort for types that cannot be represented.
707 Type *getTypeForEVT(LLVMContext &Context) const;
709 /// getEVT - Return the value type corresponding to the specified type.
710 /// This returns all pointers as iPTR. If HandleUnknown is true, unknown
711 /// types are returned as Other, otherwise they are invalid.
712 static EVT getEVT(Type *Ty, bool HandleUnknown = false);
714 intptr_t getRawBits() {
718 return (intptr_t)(LLVMTy);
721 /// compareRawBits - A meaningless but well-behaved order, useful for
722 /// constructing containers.
723 struct compareRawBits {
724 bool operator()(EVT L, EVT R) const {
725 if (L.V.SimpleTy == R.V.SimpleTy)
726 return L.LLVMTy < R.LLVMTy;
728 return L.V.SimpleTy < R.V.SimpleTy;
733 // Methods for handling the Extended-type case in functions above.
734 // These are all out-of-line to prevent users of this header file
735 // from having a dependency on Type.h.
736 EVT changeExtendedVectorElementTypeToInteger() const;
737 static EVT getExtendedIntegerVT(LLVMContext &C, unsigned BitWidth);
738 static EVT getExtendedVectorVT(LLVMContext &C, EVT VT,
739 unsigned NumElements);
740 bool isExtendedFloatingPoint() const;
741 bool isExtendedInteger() const;
742 bool isExtendedVector() const;
743 bool isExtended64BitVector() const;
744 bool isExtended128BitVector() const;
745 bool isExtended256BitVector() const;
746 bool isExtended512BitVector() const;
747 bool isExtended1024BitVector() const;
748 EVT getExtendedVectorElementType() const;
749 unsigned getExtendedVectorNumElements() const;
750 unsigned getExtendedSizeInBits() const;
753 } // End llvm namespace