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_FP_VECTOR_VALUETYPE = v2f16,
86 LAST_FP_VECTOR_VALUETYPE = v4f64,
88 x86mmx = 35, // This is an X86 MMX value
90 Glue = 36, // This glues nodes together during pre-RA sched
92 isVoid = 37, // This has no value
94 Untyped = 38, // This value takes a register, but has
95 // unspecified type. The register class
96 // will be determined by the opcode.
98 LAST_VALUETYPE = 39, // This always remains at the end of the list.
100 // This is the current maximum for LAST_VALUETYPE.
101 // MVT::MAX_ALLOWED_VALUETYPE is used for asserts and to size bit vectors
102 // This value must be a multiple of 32.
103 MAX_ALLOWED_VALUETYPE = 64,
105 // Metadata - This is MDNode or MDString.
108 // iPTRAny - An int value the size of the pointer of the current
109 // target to any address space. This must only be used internal to
110 // tblgen. Other than for overloading, we treat iPTRAny the same as iPTR.
113 // vAny - A vector with any length and element size. This is used
114 // for intrinsics that have overloadings based on vector types.
115 // This is only for tblgen's consumption!
118 // fAny - Any floating-point or vector floating-point value. This is used
119 // for intrinsics that have overloadings based on floating-point types.
120 // This is only for tblgen's consumption!
123 // iAny - An integer or vector integer value of any bit width. This is
124 // used for intrinsics that have overloadings based on integer bit widths.
125 // This is only for tblgen's consumption!
128 // iPTR - An int value the size of the pointer of the current
129 // target. This should only be used internal to tblgen!
132 // LastSimpleValueType - The greatest valid SimpleValueType value.
133 LastSimpleValueType = 255,
135 // INVALID_SIMPLE_VALUE_TYPE - Simple value types greater than or equal
136 // to this are considered extended value types.
137 INVALID_SIMPLE_VALUE_TYPE = LastSimpleValueType + 1
140 SimpleValueType SimpleTy;
142 MVT() : SimpleTy((SimpleValueType)(INVALID_SIMPLE_VALUE_TYPE)) {}
143 MVT(SimpleValueType SVT) : SimpleTy(SVT) { }
145 bool operator>(const MVT& S) const { return SimpleTy > S.SimpleTy; }
146 bool operator<(const MVT& S) const { return SimpleTy < S.SimpleTy; }
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; }
152 /// isFloatingPoint - Return true if this is a FP, or a vector FP type.
153 bool isFloatingPoint() const {
154 return ((SimpleTy >= MVT::FIRST_FP_VALUETYPE &&
155 SimpleTy <= MVT::LAST_FP_VALUETYPE) ||
156 (SimpleTy >= MVT::FIRST_FP_VECTOR_VALUETYPE &&
157 SimpleTy <= MVT::LAST_FP_VECTOR_VALUETYPE));
160 /// isInteger - Return true if this is an integer, or a vector integer type.
161 bool isInteger() const {
162 return ((SimpleTy >= MVT::FIRST_INTEGER_VALUETYPE &&
163 SimpleTy <= MVT::LAST_INTEGER_VALUETYPE) ||
164 (SimpleTy >= MVT::v2i8 && SimpleTy <= MVT::v8i64));
167 /// isVector - Return true if this is a vector value type.
168 bool isVector() const {
169 return (SimpleTy >= MVT::FIRST_VECTOR_VALUETYPE &&
170 SimpleTy <= MVT::LAST_VECTOR_VALUETYPE);
173 /// isPow2VectorType - Returns true if the given vector is a power of 2.
174 bool isPow2VectorType() const {
175 unsigned NElts = getVectorNumElements();
176 return !(NElts & (NElts - 1));
179 /// getPow2VectorType - Widens the length of the given vector MVT up to
180 /// the nearest power of 2 and returns that type.
181 MVT getPow2VectorType() const {
182 if (isPow2VectorType())
185 unsigned NElts = getVectorNumElements();
186 unsigned Pow2NElts = 1 << Log2_32_Ceil(NElts);
187 return MVT::getVectorVT(getVectorElementType(), Pow2NElts);
190 /// getScalarType - If this is a vector type, return the element type,
191 /// otherwise return this.
192 MVT getScalarType() const {
193 return isVector() ? getVectorElementType() : *this;
196 MVT getVectorElementType() const {
199 return (MVT::SimpleValueType)(MVT::INVALID_SIMPLE_VALUE_TYPE);
204 case v32i8: return i8;
208 case v16i16: return i16;
211 case v8i32: return i32;
215 case v8i64: return i64;
216 case v2f16: return f16;
219 case v8f32: return f32;
221 case v4f64: return f64;
225 unsigned getVectorNumElements() const {
229 case v32i8: return 32;
231 case v16i16: return 16;
236 case v8f32: return 8;
242 case v4f64: return 4;
249 case v2f64: return 2;
250 case v1i64: return 1;
254 unsigned getSizeInBits() const {
257 llvm_unreachable("Value type size is target-dependent. Ask TLI.");
261 llvm_unreachable("Value type is overloaded.");
263 llvm_unreachable("getSizeInBits called on extended MVT.");
268 case v2i8: return 16;
273 case v2f16: return 32;
281 case v2f32: return 64;
282 case f80 : return 80;
291 case v2f64: return 128;
297 case v4f64: return 256;
298 case v8i64: return 512;
302 /// getStoreSize - Return the number of bytes overwritten by a store
303 /// of the specified value type.
304 unsigned getStoreSize() const {
305 return (getSizeInBits() + 7) / 8;
308 /// getStoreSizeInBits - Return the number of bits overwritten by a store
309 /// of the specified value type.
310 unsigned getStoreSizeInBits() const {
311 return getStoreSize() * 8;
314 static MVT getFloatingPointVT(unsigned BitWidth) {
317 llvm_unreachable("Bad bit width!");
331 static MVT getIntegerVT(unsigned BitWidth) {
334 return (MVT::SimpleValueType)(MVT::INVALID_SIMPLE_VALUE_TYPE);
350 static MVT getVectorVT(MVT VT, unsigned NumElements) {
351 switch (VT.SimpleTy) {
355 if (NumElements == 2) return MVT::v2i8;
356 if (NumElements == 4) return MVT::v4i8;
357 if (NumElements == 8) return MVT::v8i8;
358 if (NumElements == 16) return MVT::v16i8;
359 if (NumElements == 32) return MVT::v32i8;
362 if (NumElements == 2) return MVT::v2i16;
363 if (NumElements == 4) return MVT::v4i16;
364 if (NumElements == 8) return MVT::v8i16;
365 if (NumElements == 16) return MVT::v16i16;
368 if (NumElements == 2) return MVT::v2i32;
369 if (NumElements == 4) return MVT::v4i32;
370 if (NumElements == 8) return MVT::v8i32;
373 if (NumElements == 1) return MVT::v1i64;
374 if (NumElements == 2) return MVT::v2i64;
375 if (NumElements == 4) return MVT::v4i64;
376 if (NumElements == 8) return MVT::v8i64;
379 if (NumElements == 2) return MVT::v2f16;
382 if (NumElements == 2) return MVT::v2f32;
383 if (NumElements == 4) return MVT::v4f32;
384 if (NumElements == 8) return MVT::v8f32;
387 if (NumElements == 2) return MVT::v2f64;
388 if (NumElements == 4) return MVT::v4f64;
391 return (MVT::SimpleValueType)(MVT::INVALID_SIMPLE_VALUE_TYPE);
396 /// EVT - Extended Value Type. Capable of holding value types which are not
397 /// native for any processor (such as the i12345 type), as well as the types
398 /// a MVT can represent.
405 EVT() : V((MVT::SimpleValueType)(MVT::INVALID_SIMPLE_VALUE_TYPE)),
407 EVT(MVT::SimpleValueType SVT) : V(SVT), LLVMTy(0) { }
408 EVT(MVT S) : V(S), LLVMTy(0) {}
410 bool operator==(EVT VT) const {
411 return !(*this != VT);
413 bool operator!=(EVT VT) const {
414 if (V.SimpleTy != VT.V.SimpleTy)
416 if (V.SimpleTy == MVT::INVALID_SIMPLE_VALUE_TYPE)
417 return LLVMTy != VT.LLVMTy;
421 /// getFloatingPointVT - Returns the EVT that represents a floating point
422 /// type with the given number of bits. There are two floating point types
423 /// with 128 bits - this returns f128 rather than ppcf128.
424 static EVT getFloatingPointVT(unsigned BitWidth) {
425 return MVT::getFloatingPointVT(BitWidth);
428 /// getIntegerVT - Returns the EVT that represents an integer with the given
430 static EVT getIntegerVT(LLVMContext &Context, unsigned BitWidth) {
431 MVT M = MVT::getIntegerVT(BitWidth);
432 if (M.SimpleTy != MVT::INVALID_SIMPLE_VALUE_TYPE)
434 return getExtendedIntegerVT(Context, BitWidth);
437 /// getVectorVT - Returns the EVT that represents a vector NumElements in
438 /// length, where each element is of type VT.
439 static EVT getVectorVT(LLVMContext &Context, EVT VT, unsigned NumElements) {
440 MVT M = MVT::getVectorVT(VT.V, NumElements);
441 if (M.SimpleTy != MVT::INVALID_SIMPLE_VALUE_TYPE)
443 return getExtendedVectorVT(Context, VT, NumElements);
446 /// changeVectorElementTypeToInteger - Return a vector with the same number
447 /// of elements as this vector, but with the element type converted to an
448 /// integer type with the same bitwidth.
449 EVT changeVectorElementTypeToInteger() const {
451 return changeExtendedVectorElementTypeToInteger();
452 MVT EltTy = getSimpleVT().getVectorElementType();
453 unsigned BitWidth = EltTy.getSizeInBits();
454 MVT IntTy = MVT::getIntegerVT(BitWidth);
455 MVT VecTy = MVT::getVectorVT(IntTy, getVectorNumElements());
456 assert(VecTy != MVT::INVALID_SIMPLE_VALUE_TYPE &&
457 "Simple vector VT not representable by simple integer vector VT!");
461 /// isSimple - Test if the given EVT is simple (as opposed to being
463 bool isSimple() const {
464 return V.SimpleTy <= MVT::LastSimpleValueType;
467 /// isExtended - Test if the given EVT is extended (as opposed to
469 bool isExtended() const {
473 /// isFloatingPoint - Return true if this is a FP, or a vector FP type.
474 bool isFloatingPoint() const {
475 return isSimple() ? V.isFloatingPoint() : isExtendedFloatingPoint();
478 /// isInteger - Return true if this is an integer, or a vector integer type.
479 bool isInteger() const {
480 return isSimple() ? V.isInteger() : isExtendedInteger();
483 /// isVector - Return true if this is a vector value type.
484 bool isVector() const {
485 return isSimple() ? V.isVector() : isExtendedVector();
488 /// is64BitVector - Return true if this is a 64-bit vector type.
489 bool is64BitVector() const {
491 return isExtended64BitVector();
493 return (V == MVT::v8i8 || V==MVT::v4i16 || V==MVT::v2i32 ||
494 V == MVT::v1i64 || V==MVT::v2f32);
497 /// is128BitVector - Return true if this is a 128-bit vector type.
498 bool is128BitVector() const {
500 return isExtended128BitVector();
501 return (V==MVT::v16i8 || V==MVT::v8i16 || V==MVT::v4i32 ||
502 V==MVT::v2i64 || V==MVT::v4f32 || V==MVT::v2f64);
505 /// is256BitVector - Return true if this is a 256-bit vector type.
506 inline bool is256BitVector() const {
508 return isExtended256BitVector();
509 return (V == MVT::v8f32 || V == MVT::v4f64 || V == MVT::v32i8 ||
510 V == MVT::v16i16 || V == MVT::v8i32 || V == MVT::v4i64);
513 /// is512BitVector - Return true if this is a 512-bit vector type.
514 inline bool is512BitVector() const {
515 return isSimple() ? (V == MVT::v8i64) : isExtended512BitVector();
518 /// isOverloaded - Return true if this is an overloaded type for TableGen.
519 bool isOverloaded() const {
520 return (V==MVT::iAny || V==MVT::fAny || V==MVT::vAny || V==MVT::iPTRAny);
523 /// isByteSized - Return true if the bit size is a multiple of 8.
524 bool isByteSized() const {
525 return (getSizeInBits() & 7) == 0;
528 /// isRound - Return true if the size is a power-of-two number of bytes.
529 bool isRound() const {
530 unsigned BitSize = getSizeInBits();
531 return BitSize >= 8 && !(BitSize & (BitSize - 1));
534 /// bitsEq - Return true if this has the same number of bits as VT.
535 bool bitsEq(EVT VT) const {
536 if (EVT::operator==(VT)) return true;
537 return getSizeInBits() == VT.getSizeInBits();
540 /// bitsGT - Return true if this has more bits than VT.
541 bool bitsGT(EVT VT) const {
542 if (EVT::operator==(VT)) return false;
543 return getSizeInBits() > VT.getSizeInBits();
546 /// bitsGE - Return true if this has no less bits than VT.
547 bool bitsGE(EVT VT) const {
548 if (EVT::operator==(VT)) return true;
549 return getSizeInBits() >= VT.getSizeInBits();
552 /// bitsLT - Return true if this has less bits than VT.
553 bool bitsLT(EVT VT) const {
554 if (EVT::operator==(VT)) return false;
555 return getSizeInBits() < VT.getSizeInBits();
558 /// bitsLE - Return true if this has no more bits than VT.
559 bool bitsLE(EVT VT) const {
560 if (EVT::operator==(VT)) return true;
561 return getSizeInBits() <= VT.getSizeInBits();
565 /// getSimpleVT - Return the SimpleValueType held in the specified
567 MVT getSimpleVT() const {
568 assert(isSimple() && "Expected a SimpleValueType!");
572 /// getScalarType - If this is a vector type, return the element type,
573 /// otherwise return this.
574 EVT getScalarType() const {
575 return isVector() ? getVectorElementType() : *this;
578 /// getVectorElementType - Given a vector type, return the type of
580 EVT getVectorElementType() const {
581 assert(isVector() && "Invalid vector type!");
583 return V.getVectorElementType();
584 return getExtendedVectorElementType();
587 /// getVectorNumElements - Given a vector type, return the number of
588 /// elements it contains.
589 unsigned getVectorNumElements() const {
590 assert(isVector() && "Invalid vector type!");
592 return V.getVectorNumElements();
593 return getExtendedVectorNumElements();
596 /// getSizeInBits - Return the size of the specified value type in bits.
597 unsigned getSizeInBits() const {
599 return V.getSizeInBits();
600 return getExtendedSizeInBits();
603 /// getStoreSize - Return the number of bytes overwritten by a store
604 /// of the specified value type.
605 unsigned getStoreSize() const {
606 return (getSizeInBits() + 7) / 8;
609 /// getStoreSizeInBits - Return the number of bits overwritten by a store
610 /// of the specified value type.
611 unsigned getStoreSizeInBits() const {
612 return getStoreSize() * 8;
615 /// getRoundIntegerType - Rounds the bit-width of the given integer EVT up
616 /// to the nearest power of two (and at least to eight), and returns the
617 /// integer EVT with that number of bits.
618 EVT getRoundIntegerType(LLVMContext &Context) const {
619 assert(isInteger() && !isVector() && "Invalid integer type!");
620 unsigned BitWidth = getSizeInBits();
623 return getIntegerVT(Context, 1 << Log2_32_Ceil(BitWidth));
626 /// getHalfSizedIntegerVT - Finds the smallest simple value type that is
627 /// greater than or equal to half the width of this EVT. If no simple
628 /// value type can be found, an extended integer value type of half the
629 /// size (rounded up) is returned.
630 EVT getHalfSizedIntegerVT(LLVMContext &Context) const {
631 assert(isInteger() && !isVector() && "Invalid integer type!");
632 unsigned EVTSize = getSizeInBits();
633 for (unsigned IntVT = MVT::FIRST_INTEGER_VALUETYPE;
634 IntVT <= MVT::LAST_INTEGER_VALUETYPE; ++IntVT) {
635 EVT HalfVT = EVT((MVT::SimpleValueType)IntVT);
636 if (HalfVT.getSizeInBits() * 2 >= EVTSize)
639 return getIntegerVT(Context, (EVTSize + 1) / 2);
642 /// isPow2VectorType - Returns true if the given vector is a power of 2.
643 bool isPow2VectorType() const {
644 unsigned NElts = getVectorNumElements();
645 return !(NElts & (NElts - 1));
648 /// getPow2VectorType - Widens the length of the given vector EVT up to
649 /// the nearest power of 2 and returns that type.
650 EVT getPow2VectorType(LLVMContext &Context) const {
651 if (!isPow2VectorType()) {
652 unsigned NElts = getVectorNumElements();
653 unsigned Pow2NElts = 1 << Log2_32_Ceil(NElts);
654 return EVT::getVectorVT(Context, getVectorElementType(), Pow2NElts);
661 /// getEVTString - This function returns value type as a string,
663 std::string getEVTString() const;
665 /// getTypeForEVT - This method returns an LLVM type corresponding to the
666 /// specified EVT. For integer types, this returns an unsigned type. Note
667 /// that this will abort for types that cannot be represented.
668 Type *getTypeForEVT(LLVMContext &Context) const;
670 /// getEVT - Return the value type corresponding to the specified type.
671 /// This returns all pointers as iPTR. If HandleUnknown is true, unknown
672 /// types are returned as Other, otherwise they are invalid.
673 static EVT getEVT(Type *Ty, bool HandleUnknown = false);
675 intptr_t getRawBits() {
679 return (intptr_t)(LLVMTy);
682 /// compareRawBits - A meaningless but well-behaved order, useful for
683 /// constructing containers.
684 struct compareRawBits {
685 bool operator()(EVT L, EVT R) const {
686 if (L.V.SimpleTy == R.V.SimpleTy)
687 return L.LLVMTy < R.LLVMTy;
689 return L.V.SimpleTy < R.V.SimpleTy;
694 // Methods for handling the Extended-type case in functions above.
695 // These are all out-of-line to prevent users of this header file
696 // from having a dependency on Type.h.
697 EVT changeExtendedVectorElementTypeToInteger() const;
698 static EVT getExtendedIntegerVT(LLVMContext &C, unsigned BitWidth);
699 static EVT getExtendedVectorVT(LLVMContext &C, EVT VT,
700 unsigned NumElements);
701 bool isExtendedFloatingPoint() const;
702 bool isExtendedInteger() const;
703 bool isExtendedVector() const;
704 bool isExtended64BitVector() const;
705 bool isExtended128BitVector() const;
706 bool isExtended256BitVector() const;
707 bool isExtended512BitVector() const;
708 EVT getExtendedVectorElementType() const;
709 unsigned getExtendedVectorNumElements() const;
710 unsigned getExtendedSizeInBits() const;
713 } // End llvm namespace