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 /// isPow2VectorType - Returns true if the given vector is a power of 2.
179 bool isPow2VectorType() const {
180 unsigned NElts = getVectorNumElements();
181 return !(NElts & (NElts - 1));
184 /// getPow2VectorType - Widens the length of the given vector MVT up to
185 /// the nearest power of 2 and returns that type.
186 MVT getPow2VectorType() const {
187 if (isPow2VectorType())
190 unsigned NElts = getVectorNumElements();
191 unsigned Pow2NElts = 1 << Log2_32_Ceil(NElts);
192 return MVT::getVectorVT(getVectorElementType(), Pow2NElts);
195 /// getScalarType - If this is a vector type, return the element type,
196 /// otherwise return this.
197 MVT getScalarType() const {
198 return isVector() ? getVectorElementType() : *this;
201 MVT getVectorElementType() const {
204 llvm_unreachable("Not a vector MVT!");
209 case v32i8: return i8;
213 case v16i16: return i16;
217 case v16i32: return i32;
222 case v16i64: return i64;
223 case v2f16: return f16;
226 case v8f32: return f32;
228 case v4f64: return f64;
232 unsigned getVectorNumElements() const {
235 llvm_unreachable("Not a vector MVT!");
236 case v32i8: return 32;
240 case v16i64:return 16;
245 case v8f32: return 8;
251 case v4f64: return 4;
258 case v2f64: return 2;
259 case v1i64: return 1;
263 unsigned getSizeInBits() const {
266 llvm_unreachable("Value type size is target-dependent. Ask TLI.");
270 llvm_unreachable("Value type is overloaded.");
272 llvm_unreachable("getSizeInBits called on extended MVT.");
277 case v2i8: return 16;
282 case v2f16: return 32;
290 case v2f32: return 64;
291 case f80 : return 80;
300 case v2f64: return 128;
306 case v4f64: return 256;
308 case v8i64: return 512;
309 case v16i64:return 1024;
313 /// getStoreSize - Return the number of bytes overwritten by a store
314 /// of the specified value type.
315 unsigned getStoreSize() const {
316 return (getSizeInBits() + 7) / 8;
319 /// getStoreSizeInBits - Return the number of bits overwritten by a store
320 /// of the specified value type.
321 unsigned getStoreSizeInBits() const {
322 return getStoreSize() * 8;
325 static MVT getFloatingPointVT(unsigned BitWidth) {
328 llvm_unreachable("Bad bit width!");
342 static MVT getIntegerVT(unsigned BitWidth) {
345 return (MVT::SimpleValueType)(MVT::INVALID_SIMPLE_VALUE_TYPE);
361 static MVT getVectorVT(MVT VT, unsigned NumElements) {
362 switch (VT.SimpleTy) {
366 if (NumElements == 2) return MVT::v2i8;
367 if (NumElements == 4) return MVT::v4i8;
368 if (NumElements == 8) return MVT::v8i8;
369 if (NumElements == 16) return MVT::v16i8;
370 if (NumElements == 32) return MVT::v32i8;
373 if (NumElements == 2) return MVT::v2i16;
374 if (NumElements == 4) return MVT::v4i16;
375 if (NumElements == 8) return MVT::v8i16;
376 if (NumElements == 16) return MVT::v16i16;
379 if (NumElements == 2) return MVT::v2i32;
380 if (NumElements == 4) return MVT::v4i32;
381 if (NumElements == 8) return MVT::v8i32;
382 if (NumElements == 16) return MVT::v16i32;
385 if (NumElements == 1) return MVT::v1i64;
386 if (NumElements == 2) return MVT::v2i64;
387 if (NumElements == 4) return MVT::v4i64;
388 if (NumElements == 8) return MVT::v8i64;
389 if (NumElements == 16) return MVT::v16i64;
392 if (NumElements == 2) return MVT::v2f16;
395 if (NumElements == 2) return MVT::v2f32;
396 if (NumElements == 4) return MVT::v4f32;
397 if (NumElements == 8) return MVT::v8f32;
400 if (NumElements == 2) return MVT::v2f64;
401 if (NumElements == 4) return MVT::v4f64;
404 return (MVT::SimpleValueType)(MVT::INVALID_SIMPLE_VALUE_TYPE);
409 /// EVT - Extended Value Type. Capable of holding value types which are not
410 /// native for any processor (such as the i12345 type), as well as the types
411 /// a MVT can represent.
418 EVT() : V((MVT::SimpleValueType)(MVT::INVALID_SIMPLE_VALUE_TYPE)),
420 EVT(MVT::SimpleValueType SVT) : V(SVT), LLVMTy(0) { }
421 EVT(MVT S) : V(S), LLVMTy(0) {}
423 bool operator==(EVT VT) const {
424 return !(*this != VT);
426 bool operator!=(EVT VT) const {
427 if (V.SimpleTy != VT.V.SimpleTy)
429 if (V.SimpleTy == MVT::INVALID_SIMPLE_VALUE_TYPE)
430 return LLVMTy != VT.LLVMTy;
434 /// getFloatingPointVT - Returns the EVT that represents a floating point
435 /// type with the given number of bits. There are two floating point types
436 /// with 128 bits - this returns f128 rather than ppcf128.
437 static EVT getFloatingPointVT(unsigned BitWidth) {
438 return MVT::getFloatingPointVT(BitWidth);
441 /// getIntegerVT - Returns the EVT that represents an integer with the given
443 static EVT getIntegerVT(LLVMContext &Context, unsigned BitWidth) {
444 MVT M = MVT::getIntegerVT(BitWidth);
445 if (M.SimpleTy != MVT::INVALID_SIMPLE_VALUE_TYPE)
447 return getExtendedIntegerVT(Context, BitWidth);
450 /// getVectorVT - Returns the EVT that represents a vector NumElements in
451 /// length, where each element is of type VT.
452 static EVT getVectorVT(LLVMContext &Context, EVT VT, unsigned NumElements) {
453 MVT M = MVT::getVectorVT(VT.V, NumElements);
454 if (M.SimpleTy != MVT::INVALID_SIMPLE_VALUE_TYPE)
456 return getExtendedVectorVT(Context, VT, NumElements);
459 /// changeVectorElementTypeToInteger - Return a vector with the same number
460 /// of elements as this vector, but with the element type converted to an
461 /// integer type with the same bitwidth.
462 EVT changeVectorElementTypeToInteger() const {
464 return changeExtendedVectorElementTypeToInteger();
465 MVT EltTy = getSimpleVT().getVectorElementType();
466 unsigned BitWidth = EltTy.getSizeInBits();
467 MVT IntTy = MVT::getIntegerVT(BitWidth);
468 MVT VecTy = MVT::getVectorVT(IntTy, getVectorNumElements());
469 assert(VecTy != MVT::INVALID_SIMPLE_VALUE_TYPE &&
470 "Simple vector VT not representable by simple integer vector VT!");
474 /// isSimple - Test if the given EVT is simple (as opposed to being
476 bool isSimple() const {
477 return V.SimpleTy <= MVT::LastSimpleValueType;
480 /// isExtended - Test if the given EVT is extended (as opposed to
482 bool isExtended() const {
486 /// isFloatingPoint - Return true if this is a FP, or a vector FP type.
487 bool isFloatingPoint() const {
488 return isSimple() ? V.isFloatingPoint() : isExtendedFloatingPoint();
491 /// isInteger - Return true if this is an integer, or a vector integer type.
492 bool isInteger() const {
493 return isSimple() ? V.isInteger() : isExtendedInteger();
496 /// isVector - Return true if this is a vector value type.
497 bool isVector() const {
498 return isSimple() ? V.isVector() : isExtendedVector();
501 /// is64BitVector - Return true if this is a 64-bit vector type.
502 bool is64BitVector() const {
504 return isExtended64BitVector();
506 return (V == MVT::v8i8 || V==MVT::v4i16 || V==MVT::v2i32 ||
507 V == MVT::v1i64 || V==MVT::v2f32);
510 /// is128BitVector - Return true if this is a 128-bit vector type.
511 bool is128BitVector() const {
513 return isExtended128BitVector();
514 return (V==MVT::v16i8 || V==MVT::v8i16 || V==MVT::v4i32 ||
515 V==MVT::v2i64 || V==MVT::v4f32 || V==MVT::v2f64);
518 /// is256BitVector - Return true if this is a 256-bit vector type.
519 bool is256BitVector() const {
521 return isExtended256BitVector();
522 return (V == MVT::v8f32 || V == MVT::v4f64 || V == MVT::v32i8 ||
523 V == MVT::v16i16 || V == MVT::v8i32 || V == MVT::v4i64);
526 /// is512BitVector - Return true if this is a 512-bit vector type.
527 inline bool is512BitVector() const {
528 return isSimple() ? (V == MVT::v8i64
529 || V == MVT::v16i32) : isExtended512BitVector();
532 /// is1024BitVector - Return true if this is a 1024-bit vector type.
533 inline bool is1024BitVector() const {
534 return isSimple() ? (V == MVT::v16i64) : isExtended1024BitVector();
537 /// isOverloaded - Return true if this is an overloaded type for TableGen.
538 bool isOverloaded() const {
539 return (V==MVT::iAny || V==MVT::fAny || V==MVT::vAny || V==MVT::iPTRAny);
542 /// isByteSized - Return true if the bit size is a multiple of 8.
543 bool isByteSized() const {
544 return (getSizeInBits() & 7) == 0;
547 /// isRound - Return true if the size is a power-of-two number of bytes.
548 bool isRound() const {
549 unsigned BitSize = getSizeInBits();
550 return BitSize >= 8 && !(BitSize & (BitSize - 1));
553 /// bitsEq - Return true if this has the same number of bits as VT.
554 bool bitsEq(EVT VT) const {
555 if (EVT::operator==(VT)) return true;
556 return getSizeInBits() == VT.getSizeInBits();
559 /// bitsGT - Return true if this has more bits than VT.
560 bool bitsGT(EVT VT) const {
561 if (EVT::operator==(VT)) return false;
562 return getSizeInBits() > VT.getSizeInBits();
565 /// bitsGE - Return true if this has no less bits than VT.
566 bool bitsGE(EVT VT) const {
567 if (EVT::operator==(VT)) return true;
568 return getSizeInBits() >= VT.getSizeInBits();
571 /// bitsLT - Return true if this has less bits than VT.
572 bool bitsLT(EVT VT) const {
573 if (EVT::operator==(VT)) return false;
574 return getSizeInBits() < VT.getSizeInBits();
577 /// bitsLE - Return true if this has no more bits than VT.
578 bool bitsLE(EVT VT) const {
579 if (EVT::operator==(VT)) return true;
580 return getSizeInBits() <= VT.getSizeInBits();
584 /// getSimpleVT - Return the SimpleValueType held in the specified
586 MVT getSimpleVT() const {
587 assert(isSimple() && "Expected a SimpleValueType!");
591 /// getScalarType - If this is a vector type, return the element type,
592 /// otherwise return this.
593 EVT getScalarType() const {
594 return isVector() ? getVectorElementType() : *this;
597 /// getVectorElementType - Given a vector type, return the type of
599 EVT getVectorElementType() const {
600 assert(isVector() && "Invalid vector type!");
602 return V.getVectorElementType();
603 return getExtendedVectorElementType();
606 /// getVectorNumElements - Given a vector type, return the number of
607 /// elements it contains.
608 unsigned getVectorNumElements() const {
609 assert(isVector() && "Invalid vector type!");
611 return V.getVectorNumElements();
612 return getExtendedVectorNumElements();
615 /// getSizeInBits - Return the size of the specified value type in bits.
616 unsigned getSizeInBits() const {
618 return V.getSizeInBits();
619 return getExtendedSizeInBits();
622 /// getStoreSize - Return the number of bytes overwritten by a store
623 /// of the specified value type.
624 unsigned getStoreSize() const {
625 return (getSizeInBits() + 7) / 8;
628 /// getStoreSizeInBits - Return the number of bits overwritten by a store
629 /// of the specified value type.
630 unsigned getStoreSizeInBits() const {
631 return getStoreSize() * 8;
634 /// getRoundIntegerType - Rounds the bit-width of the given integer EVT up
635 /// to the nearest power of two (and at least to eight), and returns the
636 /// integer EVT with that number of bits.
637 EVT getRoundIntegerType(LLVMContext &Context) const {
638 assert(isInteger() && !isVector() && "Invalid integer type!");
639 unsigned BitWidth = getSizeInBits();
642 return getIntegerVT(Context, 1 << Log2_32_Ceil(BitWidth));
645 /// getHalfSizedIntegerVT - Finds the smallest simple value type that is
646 /// greater than or equal to half the width of this EVT. If no simple
647 /// value type can be found, an extended integer value type of half the
648 /// size (rounded up) is returned.
649 EVT getHalfSizedIntegerVT(LLVMContext &Context) const {
650 assert(isInteger() && !isVector() && "Invalid integer type!");
651 unsigned EVTSize = getSizeInBits();
652 for (unsigned IntVT = MVT::FIRST_INTEGER_VALUETYPE;
653 IntVT <= MVT::LAST_INTEGER_VALUETYPE; ++IntVT) {
654 EVT HalfVT = EVT((MVT::SimpleValueType)IntVT);
655 if (HalfVT.getSizeInBits() * 2 >= EVTSize)
658 return getIntegerVT(Context, (EVTSize + 1) / 2);
661 /// isPow2VectorType - Returns true if the given vector is a power of 2.
662 bool isPow2VectorType() const {
663 unsigned NElts = getVectorNumElements();
664 return !(NElts & (NElts - 1));
667 /// getPow2VectorType - Widens the length of the given vector EVT up to
668 /// the nearest power of 2 and returns that type.
669 EVT getPow2VectorType(LLVMContext &Context) const {
670 if (!isPow2VectorType()) {
671 unsigned NElts = getVectorNumElements();
672 unsigned Pow2NElts = 1 << Log2_32_Ceil(NElts);
673 return EVT::getVectorVT(Context, getVectorElementType(), Pow2NElts);
680 /// getEVTString - This function returns value type as a string,
682 std::string getEVTString() const;
684 /// getTypeForEVT - This method returns an LLVM type corresponding to the
685 /// specified EVT. For integer types, this returns an unsigned type. Note
686 /// that this will abort for types that cannot be represented.
687 Type *getTypeForEVT(LLVMContext &Context) const;
689 /// getEVT - Return the value type corresponding to the specified type.
690 /// This returns all pointers as iPTR. If HandleUnknown is true, unknown
691 /// types are returned as Other, otherwise they are invalid.
692 static EVT getEVT(Type *Ty, bool HandleUnknown = false);
694 intptr_t getRawBits() {
698 return (intptr_t)(LLVMTy);
701 /// compareRawBits - A meaningless but well-behaved order, useful for
702 /// constructing containers.
703 struct compareRawBits {
704 bool operator()(EVT L, EVT R) const {
705 if (L.V.SimpleTy == R.V.SimpleTy)
706 return L.LLVMTy < R.LLVMTy;
708 return L.V.SimpleTy < R.V.SimpleTy;
713 // Methods for handling the Extended-type case in functions above.
714 // These are all out-of-line to prevent users of this header file
715 // from having a dependency on Type.h.
716 EVT changeExtendedVectorElementTypeToInteger() const;
717 static EVT getExtendedIntegerVT(LLVMContext &C, unsigned BitWidth);
718 static EVT getExtendedVectorVT(LLVMContext &C, EVT VT,
719 unsigned NumElements);
720 bool isExtendedFloatingPoint() const;
721 bool isExtendedInteger() const;
722 bool isExtendedVector() const;
723 bool isExtended64BitVector() const;
724 bool isExtended128BitVector() const;
725 bool isExtended256BitVector() const;
726 bool isExtended512BitVector() const;
727 bool isExtended1024BitVector() const;
728 EVT getExtendedVectorElementType() const;
729 unsigned getExtendedVectorNumElements() const;
730 unsigned getExtendedSizeInBits() const;
733 } // End llvm namespace