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 v1i64 = 25, // 1 x i64
72 v2i64 = 26, // 2 x i64
73 v4i64 = 27, // 4 x i64
74 v8i64 = 28, // 8 x i64
76 v2f16 = 29, // 2 x f16
77 v2f32 = 30, // 2 x f32
78 v4f32 = 31, // 4 x f32
79 v8f32 = 32, // 8 x f32
80 v2f64 = 33, // 2 x f64
81 v4f64 = 34, // 4 x f64
83 FIRST_VECTOR_VALUETYPE = v2i8,
84 LAST_VECTOR_VALUETYPE = v4f64,
85 FIRST_INTEGER_VECTOR_VALUETYPE = v2i8,
86 LAST_INTEGER_VECTOR_VALUETYPE = v8i64,
87 FIRST_FP_VECTOR_VALUETYPE = v2f16,
88 LAST_FP_VECTOR_VALUETYPE = v4f64,
90 x86mmx = 35, // This is an X86 MMX value
92 Glue = 36, // This glues nodes together during pre-RA sched
94 isVoid = 37, // This has no value
96 Untyped = 38, // This value takes a register, but has
97 // unspecified type. The register class
98 // will be determined by the opcode.
100 LAST_VALUETYPE = 39, // This always remains at the end of the list.
102 // This is the current maximum for LAST_VALUETYPE.
103 // MVT::MAX_ALLOWED_VALUETYPE is used for asserts and to size bit vectors
104 // This value must be a multiple of 32.
105 MAX_ALLOWED_VALUETYPE = 64,
107 // Metadata - This is MDNode or MDString.
110 // iPTRAny - An int value the size of the pointer of the current
111 // target to any address space. This must only be used internal to
112 // tblgen. Other than for overloading, we treat iPTRAny the same as iPTR.
115 // vAny - A vector with any length and element size. This is used
116 // for intrinsics that have overloadings based on vector types.
117 // This is only for tblgen's consumption!
120 // fAny - Any floating-point or vector floating-point value. This is used
121 // for intrinsics that have overloadings based on floating-point types.
122 // This is only for tblgen's consumption!
125 // iAny - An integer or vector integer value of any bit width. This is
126 // used for intrinsics that have overloadings based on integer bit widths.
127 // This is only for tblgen's consumption!
130 // iPTR - An int value the size of the pointer of the current
131 // target. This should only be used internal to tblgen!
134 // LastSimpleValueType - The greatest valid SimpleValueType value.
135 LastSimpleValueType = 255,
137 // INVALID_SIMPLE_VALUE_TYPE - Simple value types greater than or equal
138 // to this are considered extended value types.
139 INVALID_SIMPLE_VALUE_TYPE = LastSimpleValueType + 1
142 SimpleValueType SimpleTy;
144 MVT() : SimpleTy((SimpleValueType)(INVALID_SIMPLE_VALUE_TYPE)) {}
145 MVT(SimpleValueType SVT) : SimpleTy(SVT) { }
147 bool operator>(const MVT& S) const { return SimpleTy > S.SimpleTy; }
148 bool operator<(const MVT& S) const { return SimpleTy < S.SimpleTy; }
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; }
154 /// isFloatingPoint - Return true if this is a FP, or a vector FP type.
155 bool isFloatingPoint() const {
156 return ((SimpleTy >= MVT::FIRST_FP_VALUETYPE &&
157 SimpleTy <= MVT::LAST_FP_VALUETYPE) ||
158 (SimpleTy >= MVT::FIRST_FP_VECTOR_VALUETYPE &&
159 SimpleTy <= MVT::LAST_FP_VECTOR_VALUETYPE));
162 /// isInteger - Return true if this is an integer, or a vector integer type.
163 bool isInteger() const {
164 return ((SimpleTy >= MVT::FIRST_INTEGER_VALUETYPE &&
165 SimpleTy <= MVT::LAST_INTEGER_VALUETYPE) ||
166 (SimpleTy >= MVT::FIRST_INTEGER_VECTOR_VALUETYPE &&
167 SimpleTy <= MVT::LAST_INTEGER_VECTOR_VALUETYPE));
170 /// isVector - Return true if this is a vector value type.
171 bool isVector() const {
172 return (SimpleTy >= MVT::FIRST_VECTOR_VALUETYPE &&
173 SimpleTy <= MVT::LAST_VECTOR_VALUETYPE);
176 /// isPow2VectorType - Returns true if the given vector is a power of 2.
177 bool isPow2VectorType() const {
178 unsigned NElts = getVectorNumElements();
179 return !(NElts & (NElts - 1));
182 /// getPow2VectorType - Widens the length of the given vector MVT up to
183 /// the nearest power of 2 and returns that type.
184 MVT getPow2VectorType() const {
185 if (isPow2VectorType())
188 unsigned NElts = getVectorNumElements();
189 unsigned Pow2NElts = 1 << Log2_32_Ceil(NElts);
190 return MVT::getVectorVT(getVectorElementType(), Pow2NElts);
193 /// getScalarType - If this is a vector type, return the element type,
194 /// otherwise return this.
195 MVT getScalarType() const {
196 return isVector() ? getVectorElementType() : *this;
199 MVT getVectorElementType() const {
202 llvm_unreachable("Not a vector MVT!");
207 case v32i8: return i8;
211 case v16i16: return i16;
214 case v8i32: return i32;
218 case v8i64: return i64;
219 case v2f16: return f16;
222 case v8f32: return f32;
224 case v4f64: return f64;
228 unsigned getVectorNumElements() const {
231 llvm_unreachable("Not a vector MVT!");
232 case v32i8: return 32;
234 case v16i16: return 16;
239 case v8f32: return 8;
245 case v4f64: return 4;
252 case v2f64: return 2;
253 case v1i64: return 1;
257 unsigned getSizeInBits() const {
260 llvm_unreachable("Value type size is target-dependent. Ask TLI.");
264 llvm_unreachable("Value type is overloaded.");
266 llvm_unreachable("getSizeInBits called on extended MVT.");
271 case v2i8: return 16;
276 case v2f16: return 32;
284 case v2f32: return 64;
285 case f80 : return 80;
294 case v2f64: return 128;
300 case v4f64: return 256;
301 case v8i64: return 512;
305 /// getStoreSize - Return the number of bytes overwritten by a store
306 /// of the specified value type.
307 unsigned getStoreSize() const {
308 return (getSizeInBits() + 7) / 8;
311 /// getStoreSizeInBits - Return the number of bits overwritten by a store
312 /// of the specified value type.
313 unsigned getStoreSizeInBits() const {
314 return getStoreSize() * 8;
317 static MVT getFloatingPointVT(unsigned BitWidth) {
320 llvm_unreachable("Bad bit width!");
334 static MVT getIntegerVT(unsigned BitWidth) {
337 return (MVT::SimpleValueType)(MVT::INVALID_SIMPLE_VALUE_TYPE);
353 static MVT getVectorVT(MVT VT, unsigned NumElements) {
354 switch (VT.SimpleTy) {
358 if (NumElements == 2) return MVT::v2i8;
359 if (NumElements == 4) return MVT::v4i8;
360 if (NumElements == 8) return MVT::v8i8;
361 if (NumElements == 16) return MVT::v16i8;
362 if (NumElements == 32) return MVT::v32i8;
365 if (NumElements == 2) return MVT::v2i16;
366 if (NumElements == 4) return MVT::v4i16;
367 if (NumElements == 8) return MVT::v8i16;
368 if (NumElements == 16) return MVT::v16i16;
371 if (NumElements == 2) return MVT::v2i32;
372 if (NumElements == 4) return MVT::v4i32;
373 if (NumElements == 8) return MVT::v8i32;
376 if (NumElements == 1) return MVT::v1i64;
377 if (NumElements == 2) return MVT::v2i64;
378 if (NumElements == 4) return MVT::v4i64;
379 if (NumElements == 8) return MVT::v8i64;
382 if (NumElements == 2) return MVT::v2f16;
385 if (NumElements == 2) return MVT::v2f32;
386 if (NumElements == 4) return MVT::v4f32;
387 if (NumElements == 8) return MVT::v8f32;
390 if (NumElements == 2) return MVT::v2f64;
391 if (NumElements == 4) return MVT::v4f64;
394 return (MVT::SimpleValueType)(MVT::INVALID_SIMPLE_VALUE_TYPE);
399 /// EVT - Extended Value Type. Capable of holding value types which are not
400 /// native for any processor (such as the i12345 type), as well as the types
401 /// a MVT can represent.
408 EVT() : V((MVT::SimpleValueType)(MVT::INVALID_SIMPLE_VALUE_TYPE)),
410 EVT(MVT::SimpleValueType SVT) : V(SVT), LLVMTy(0) { }
411 EVT(MVT S) : V(S), LLVMTy(0) {}
413 bool operator==(EVT VT) const {
414 return !(*this != VT);
416 bool operator!=(EVT VT) const {
417 if (V.SimpleTy != VT.V.SimpleTy)
419 if (V.SimpleTy == MVT::INVALID_SIMPLE_VALUE_TYPE)
420 return LLVMTy != VT.LLVMTy;
424 /// getFloatingPointVT - Returns the EVT that represents a floating point
425 /// type with the given number of bits. There are two floating point types
426 /// with 128 bits - this returns f128 rather than ppcf128.
427 static EVT getFloatingPointVT(unsigned BitWidth) {
428 return MVT::getFloatingPointVT(BitWidth);
431 /// getIntegerVT - Returns the EVT that represents an integer with the given
433 static EVT getIntegerVT(LLVMContext &Context, unsigned BitWidth) {
434 MVT M = MVT::getIntegerVT(BitWidth);
435 if (M.SimpleTy != MVT::INVALID_SIMPLE_VALUE_TYPE)
437 return getExtendedIntegerVT(Context, BitWidth);
440 /// getVectorVT - Returns the EVT that represents a vector NumElements in
441 /// length, where each element is of type VT.
442 static EVT getVectorVT(LLVMContext &Context, EVT VT, unsigned NumElements) {
443 MVT M = MVT::getVectorVT(VT.V, NumElements);
444 if (M.SimpleTy != MVT::INVALID_SIMPLE_VALUE_TYPE)
446 return getExtendedVectorVT(Context, VT, NumElements);
449 /// changeVectorElementTypeToInteger - Return a vector with the same number
450 /// of elements as this vector, but with the element type converted to an
451 /// integer type with the same bitwidth.
452 EVT changeVectorElementTypeToInteger() const {
454 return changeExtendedVectorElementTypeToInteger();
455 MVT EltTy = getSimpleVT().getVectorElementType();
456 unsigned BitWidth = EltTy.getSizeInBits();
457 MVT IntTy = MVT::getIntegerVT(BitWidth);
458 MVT VecTy = MVT::getVectorVT(IntTy, getVectorNumElements());
459 assert(VecTy != MVT::INVALID_SIMPLE_VALUE_TYPE &&
460 "Simple vector VT not representable by simple integer vector VT!");
464 /// isSimple - Test if the given EVT is simple (as opposed to being
466 bool isSimple() const {
467 return V.SimpleTy <= MVT::LastSimpleValueType;
470 /// isExtended - Test if the given EVT is extended (as opposed to
472 bool isExtended() const {
476 /// isFloatingPoint - Return true if this is a FP, or a vector FP type.
477 bool isFloatingPoint() const {
478 return isSimple() ? V.isFloatingPoint() : isExtendedFloatingPoint();
481 /// isInteger - Return true if this is an integer, or a vector integer type.
482 bool isInteger() const {
483 return isSimple() ? V.isInteger() : isExtendedInteger();
486 /// isVector - Return true if this is a vector value type.
487 bool isVector() const {
488 return isSimple() ? V.isVector() : isExtendedVector();
491 /// is64BitVector - Return true if this is a 64-bit vector type.
492 bool is64BitVector() const {
494 return isExtended64BitVector();
496 return (V == MVT::v8i8 || V==MVT::v4i16 || V==MVT::v2i32 ||
497 V == MVT::v1i64 || V==MVT::v2f32);
500 /// is128BitVector - Return true if this is a 128-bit vector type.
501 bool is128BitVector() const {
503 return isExtended128BitVector();
504 return (V==MVT::v16i8 || V==MVT::v8i16 || V==MVT::v4i32 ||
505 V==MVT::v2i64 || V==MVT::v4f32 || V==MVT::v2f64);
508 /// is256BitVector - Return true if this is a 256-bit vector type.
509 bool is256BitVector() const {
511 return isExtended256BitVector();
512 return (V == MVT::v8f32 || V == MVT::v4f64 || V == MVT::v32i8 ||
513 V == MVT::v16i16 || V == MVT::v8i32 || V == MVT::v4i64);
516 /// is512BitVector - Return true if this is a 512-bit vector type.
517 bool is512BitVector() const {
518 return isSimple() ? (V == MVT::v8i64) : isExtended512BitVector();
521 /// isOverloaded - Return true if this is an overloaded type for TableGen.
522 bool isOverloaded() const {
523 return (V==MVT::iAny || V==MVT::fAny || V==MVT::vAny || V==MVT::iPTRAny);
526 /// isByteSized - Return true if the bit size is a multiple of 8.
527 bool isByteSized() const {
528 return (getSizeInBits() & 7) == 0;
531 /// isRound - Return true if the size is a power-of-two number of bytes.
532 bool isRound() const {
533 unsigned BitSize = getSizeInBits();
534 return BitSize >= 8 && !(BitSize & (BitSize - 1));
537 /// bitsEq - Return true if this has the same number of bits as VT.
538 bool bitsEq(EVT VT) const {
539 if (EVT::operator==(VT)) return true;
540 return getSizeInBits() == VT.getSizeInBits();
543 /// bitsGT - Return true if this has more bits than VT.
544 bool bitsGT(EVT VT) const {
545 if (EVT::operator==(VT)) return false;
546 return getSizeInBits() > VT.getSizeInBits();
549 /// bitsGE - Return true if this has no less bits than VT.
550 bool bitsGE(EVT VT) const {
551 if (EVT::operator==(VT)) return true;
552 return getSizeInBits() >= VT.getSizeInBits();
555 /// bitsLT - Return true if this has less bits than VT.
556 bool bitsLT(EVT VT) const {
557 if (EVT::operator==(VT)) return false;
558 return getSizeInBits() < VT.getSizeInBits();
561 /// bitsLE - Return true if this has no more bits than VT.
562 bool bitsLE(EVT VT) const {
563 if (EVT::operator==(VT)) return true;
564 return getSizeInBits() <= VT.getSizeInBits();
568 /// getSimpleVT - Return the SimpleValueType held in the specified
570 MVT getSimpleVT() const {
571 assert(isSimple() && "Expected a SimpleValueType!");
575 /// getScalarType - If this is a vector type, return the element type,
576 /// otherwise return this.
577 EVT getScalarType() const {
578 return isVector() ? getVectorElementType() : *this;
581 /// getVectorElementType - Given a vector type, return the type of
583 EVT getVectorElementType() const {
584 assert(isVector() && "Invalid vector type!");
586 return V.getVectorElementType();
587 return getExtendedVectorElementType();
590 /// getVectorNumElements - Given a vector type, return the number of
591 /// elements it contains.
592 unsigned getVectorNumElements() const {
593 assert(isVector() && "Invalid vector type!");
595 return V.getVectorNumElements();
596 return getExtendedVectorNumElements();
599 /// getSizeInBits - Return the size of the specified value type in bits.
600 unsigned getSizeInBits() const {
602 return V.getSizeInBits();
603 return getExtendedSizeInBits();
606 /// getStoreSize - Return the number of bytes overwritten by a store
607 /// of the specified value type.
608 unsigned getStoreSize() const {
609 return (getSizeInBits() + 7) / 8;
612 /// getStoreSizeInBits - Return the number of bits overwritten by a store
613 /// of the specified value type.
614 unsigned getStoreSizeInBits() const {
615 return getStoreSize() * 8;
618 /// getRoundIntegerType - Rounds the bit-width of the given integer EVT up
619 /// to the nearest power of two (and at least to eight), and returns the
620 /// integer EVT with that number of bits.
621 EVT getRoundIntegerType(LLVMContext &Context) const {
622 assert(isInteger() && !isVector() && "Invalid integer type!");
623 unsigned BitWidth = getSizeInBits();
626 return getIntegerVT(Context, 1 << Log2_32_Ceil(BitWidth));
629 /// getHalfSizedIntegerVT - Finds the smallest simple value type that is
630 /// greater than or equal to half the width of this EVT. If no simple
631 /// value type can be found, an extended integer value type of half the
632 /// size (rounded up) is returned.
633 EVT getHalfSizedIntegerVT(LLVMContext &Context) const {
634 assert(isInteger() && !isVector() && "Invalid integer type!");
635 unsigned EVTSize = getSizeInBits();
636 for (unsigned IntVT = MVT::FIRST_INTEGER_VALUETYPE;
637 IntVT <= MVT::LAST_INTEGER_VALUETYPE; ++IntVT) {
638 EVT HalfVT = EVT((MVT::SimpleValueType)IntVT);
639 if (HalfVT.getSizeInBits() * 2 >= EVTSize)
642 return getIntegerVT(Context, (EVTSize + 1) / 2);
645 /// isPow2VectorType - Returns true if the given vector is a power of 2.
646 bool isPow2VectorType() const {
647 unsigned NElts = getVectorNumElements();
648 return !(NElts & (NElts - 1));
651 /// getPow2VectorType - Widens the length of the given vector EVT up to
652 /// the nearest power of 2 and returns that type.
653 EVT getPow2VectorType(LLVMContext &Context) const {
654 if (!isPow2VectorType()) {
655 unsigned NElts = getVectorNumElements();
656 unsigned Pow2NElts = 1 << Log2_32_Ceil(NElts);
657 return EVT::getVectorVT(Context, getVectorElementType(), Pow2NElts);
664 /// getEVTString - This function returns value type as a string,
666 std::string getEVTString() const;
668 /// getTypeForEVT - This method returns an LLVM type corresponding to the
669 /// specified EVT. For integer types, this returns an unsigned type. Note
670 /// that this will abort for types that cannot be represented.
671 Type *getTypeForEVT(LLVMContext &Context) const;
673 /// getEVT - Return the value type corresponding to the specified type.
674 /// This returns all pointers as iPTR. If HandleUnknown is true, unknown
675 /// types are returned as Other, otherwise they are invalid.
676 static EVT getEVT(Type *Ty, bool HandleUnknown = false);
678 intptr_t getRawBits() {
682 return (intptr_t)(LLVMTy);
685 /// compareRawBits - A meaningless but well-behaved order, useful for
686 /// constructing containers.
687 struct compareRawBits {
688 bool operator()(EVT L, EVT R) const {
689 if (L.V.SimpleTy == R.V.SimpleTy)
690 return L.LLVMTy < R.LLVMTy;
692 return L.V.SimpleTy < R.V.SimpleTy;
697 // Methods for handling the Extended-type case in functions above.
698 // These are all out-of-line to prevent users of this header file
699 // from having a dependency on Type.h.
700 EVT changeExtendedVectorElementTypeToInteger() const;
701 static EVT getExtendedIntegerVT(LLVMContext &C, unsigned BitWidth);
702 static EVT getExtendedVectorVT(LLVMContext &C, EVT VT,
703 unsigned NumElements);
704 bool isExtendedFloatingPoint() const;
705 bool isExtendedInteger() const;
706 bool isExtendedVector() const;
707 bool isExtended64BitVector() const;
708 bool isExtended128BitVector() const;
709 bool isExtended256BitVector() const;
710 bool isExtended512BitVector() const;
711 EVT getExtendedVectorElementType() const;
712 unsigned getExtendedVectorNumElements() const;
713 unsigned getExtendedSizeInBits() const;
716 } // End llvm namespace