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/Support/DataTypes.h"
22 #include "llvm/Support/MathExtras.h"
29 /// MVT - Machine Value Type. Every type that is supported natively by some
30 /// processor targeted by LLVM occurs here. This means that any legal value
31 /// type can be represented by a MVT.
34 enum SimpleValueType {
35 // If you change this numbering, you must change the values in
36 // ValueTypes.td as well!
37 Other = 0, // This is a non-standard value
38 i1 = 1, // This is a 1 bit integer value
39 i8 = 2, // This is an 8 bit integer value
40 i16 = 3, // This is a 16 bit integer value
41 i32 = 4, // This is a 32 bit integer value
42 i64 = 5, // This is a 64 bit integer value
43 i128 = 6, // This is a 128 bit integer value
45 FIRST_INTEGER_VALUETYPE = i1,
46 LAST_INTEGER_VALUETYPE = i128,
48 f16 = 7, // This is a 16 bit floating point value
49 f32 = 8, // This is a 32 bit floating point value
50 f64 = 9, // This is a 64 bit floating point value
51 f80 = 10, // This is a 80 bit floating point value
52 f128 = 11, // This is a 128 bit floating point value
53 ppcf128 = 12, // This is a PPC 128-bit floating point value
58 v16i8 = 16, // 16 x i8
59 v32i8 = 17, // 32 x i8
60 v2i16 = 18, // 2 x i16
61 v4i16 = 19, // 4 x i16
62 v8i16 = 20, // 8 x i16
63 v16i16 = 21, // 16 x i16
64 v2i32 = 22, // 2 x i32
65 v4i32 = 23, // 4 x i32
66 v8i32 = 24, // 8 x i32
67 v1i64 = 25, // 1 x i64
68 v2i64 = 26, // 2 x i64
69 v4i64 = 27, // 4 x i64
70 v8i64 = 28, // 8 x i64
72 v2f16 = 29, // 2 x f16
73 v2f32 = 30, // 2 x f32
74 v4f32 = 31, // 4 x f32
75 v8f32 = 32, // 8 x f32
76 v2f64 = 33, // 2 x f64
77 v4f64 = 34, // 4 x f64
79 FIRST_VECTOR_VALUETYPE = v2i8,
80 LAST_VECTOR_VALUETYPE = v4f64,
82 x86mmx = 35, // This is an X86 MMX value
84 Glue = 36, // This glues nodes together during pre-RA sched
86 isVoid = 37, // This has no value
88 Untyped = 38, // This value takes a register, but has
89 // unspecified type. The register class
90 // will be determined by the opcode.
92 LAST_VALUETYPE = 39, // This always remains at the end of the list.
94 // This is the current maximum for LAST_VALUETYPE.
95 // MVT::MAX_ALLOWED_VALUETYPE is used for asserts and to size bit vectors
96 // This value must be a multiple of 32.
97 MAX_ALLOWED_VALUETYPE = 64,
99 // Metadata - This is MDNode or MDString.
102 // iPTRAny - An int value the size of the pointer of the current
103 // target to any address space. This must only be used internal to
104 // tblgen. Other than for overloading, we treat iPTRAny the same as iPTR.
107 // vAny - A vector with any length and element size. This is used
108 // for intrinsics that have overloadings based on vector types.
109 // This is only for tblgen's consumption!
112 // fAny - Any floating-point or vector floating-point value. This is used
113 // for intrinsics that have overloadings based on floating-point types.
114 // This is only for tblgen's consumption!
117 // iAny - An integer or vector integer value of any bit width. This is
118 // used for intrinsics that have overloadings based on integer bit widths.
119 // This is only for tblgen's consumption!
122 // iPTR - An int value the size of the pointer of the current
123 // target. This should only be used internal to tblgen!
126 // LastSimpleValueType - The greatest valid SimpleValueType value.
127 LastSimpleValueType = 255,
129 // INVALID_SIMPLE_VALUE_TYPE - Simple value types greater than or equal
130 // to this are considered extended value types.
131 INVALID_SIMPLE_VALUE_TYPE = LastSimpleValueType + 1
134 SimpleValueType SimpleTy;
136 MVT() : SimpleTy((SimpleValueType)(INVALID_SIMPLE_VALUE_TYPE)) {}
137 MVT(SimpleValueType SVT) : SimpleTy(SVT) { }
139 bool operator>(const MVT& S) const { return SimpleTy > S.SimpleTy; }
140 bool operator<(const MVT& S) const { return SimpleTy < S.SimpleTy; }
141 bool operator==(const MVT& S) const { return SimpleTy == S.SimpleTy; }
142 bool operator!=(const MVT& S) const { return SimpleTy != S.SimpleTy; }
143 bool operator>=(const MVT& S) const { return SimpleTy >= S.SimpleTy; }
144 bool operator<=(const MVT& S) const { return SimpleTy <= S.SimpleTy; }
146 /// isFloatingPoint - Return true if this is a FP, or a vector FP type.
147 bool isFloatingPoint() const {
148 return ((SimpleTy >= MVT::f16 && SimpleTy <= MVT::ppcf128) ||
149 (SimpleTy >= MVT::v2f32 && SimpleTy <= MVT::v4f64));
152 /// isInteger - Return true if this is an integer, or a vector integer type.
153 bool isInteger() const {
154 return ((SimpleTy >= MVT::FIRST_INTEGER_VALUETYPE &&
155 SimpleTy <= MVT::LAST_INTEGER_VALUETYPE) ||
156 (SimpleTy >= MVT::v2i8 && SimpleTy <= MVT::v8i64));
159 /// isVector - Return true if this is a vector value type.
160 bool isVector() const {
161 return (SimpleTy >= MVT::FIRST_VECTOR_VALUETYPE &&
162 SimpleTy <= MVT::LAST_VECTOR_VALUETYPE);
165 /// isPow2VectorType - Returns true if the given vector is a power of 2.
166 bool isPow2VectorType() const {
167 unsigned NElts = getVectorNumElements();
168 return !(NElts & (NElts - 1));
171 /// getPow2VectorType - Widens the length of the given vector MVT up to
172 /// the nearest power of 2 and returns that type.
173 MVT getPow2VectorType() const {
174 if (isPow2VectorType())
177 unsigned NElts = getVectorNumElements();
178 unsigned Pow2NElts = 1 << Log2_32_Ceil(NElts);
179 return MVT::getVectorVT(getVectorElementType(), Pow2NElts);
182 /// getScalarType - If this is a vector type, return the element type,
183 /// otherwise return this.
184 MVT getScalarType() const {
185 return isVector() ? getVectorElementType() : *this;
188 MVT getVectorElementType() const {
191 return (MVT::SimpleValueType)(MVT::INVALID_SIMPLE_VALUE_TYPE);
196 case v32i8: return i8;
200 case v16i16: return i16;
203 case v8i32: return i32;
207 case v8i64: return i64;
208 case v2f16: return f16;
211 case v8f32: return f32;
213 case v4f64: return f64;
217 unsigned getVectorNumElements() const {
221 case v32i8: return 32;
223 case v16i16: return 16;
228 case v8f32: return 8;
234 case v4f64: return 4;
241 case v2f64: return 2;
242 case v1i64: return 1;
246 unsigned getSizeInBits() const {
249 assert(0 && "Value type size is target-dependent. Ask TLI.");
253 assert(0 && "Value type is overloaded.");
255 assert(0 && "getSizeInBits called on extended MVT.");
260 case v2i8: return 16;
265 case v2f16: return 32;
273 case v2f32: return 64;
274 case f80 : return 80;
283 case v2f64: return 128;
289 case v4f64: return 256;
290 case v8i64: return 512;
294 /// getStoreSize - Return the number of bytes overwritten by a store
295 /// of the specified value type.
296 unsigned getStoreSize() const {
297 return (getSizeInBits() + 7) / 8;
300 /// getStoreSizeInBits - Return the number of bits overwritten by a store
301 /// of the specified value type.
302 unsigned getStoreSizeInBits() const {
303 return getStoreSize() * 8;
306 static MVT getFloatingPointVT(unsigned BitWidth) {
309 assert(false && "Bad bit width!");
323 static MVT getIntegerVT(unsigned BitWidth) {
326 return (MVT::SimpleValueType)(MVT::INVALID_SIMPLE_VALUE_TYPE);
342 static MVT getVectorVT(MVT VT, unsigned NumElements) {
343 switch (VT.SimpleTy) {
347 if (NumElements == 2) return MVT::v2i8;
348 if (NumElements == 4) return MVT::v4i8;
349 if (NumElements == 8) return MVT::v8i8;
350 if (NumElements == 16) return MVT::v16i8;
351 if (NumElements == 32) return MVT::v32i8;
354 if (NumElements == 2) return MVT::v2i16;
355 if (NumElements == 4) return MVT::v4i16;
356 if (NumElements == 8) return MVT::v8i16;
357 if (NumElements == 16) return MVT::v16i16;
360 if (NumElements == 2) return MVT::v2i32;
361 if (NumElements == 4) return MVT::v4i32;
362 if (NumElements == 8) return MVT::v8i32;
365 if (NumElements == 1) return MVT::v1i64;
366 if (NumElements == 2) return MVT::v2i64;
367 if (NumElements == 4) return MVT::v4i64;
368 if (NumElements == 8) return MVT::v8i64;
371 if (NumElements == 2) return MVT::v2f16;
374 if (NumElements == 2) return MVT::v2f32;
375 if (NumElements == 4) return MVT::v4f32;
376 if (NumElements == 8) return MVT::v8f32;
379 if (NumElements == 2) return MVT::v2f64;
380 if (NumElements == 4) return MVT::v4f64;
383 return (MVT::SimpleValueType)(MVT::INVALID_SIMPLE_VALUE_TYPE);
388 /// EVT - Extended Value Type. Capable of holding value types which are not
389 /// native for any processor (such as the i12345 type), as well as the types
390 /// a MVT can represent.
397 EVT() : V((MVT::SimpleValueType)(MVT::INVALID_SIMPLE_VALUE_TYPE)),
399 EVT(MVT::SimpleValueType SVT) : V(SVT), LLVMTy(0) { }
400 EVT(MVT S) : V(S), LLVMTy(0) {}
402 bool operator==(EVT VT) const {
403 return !(*this != VT);
405 bool operator!=(EVT VT) const {
406 if (V.SimpleTy != VT.V.SimpleTy)
408 if (V.SimpleTy == MVT::INVALID_SIMPLE_VALUE_TYPE)
409 return LLVMTy != VT.LLVMTy;
413 /// getFloatingPointVT - Returns the EVT that represents a floating point
414 /// type with the given number of bits. There are two floating point types
415 /// with 128 bits - this returns f128 rather than ppcf128.
416 static EVT getFloatingPointVT(unsigned BitWidth) {
417 return MVT::getFloatingPointVT(BitWidth);
420 /// getIntegerVT - Returns the EVT that represents an integer with the given
422 static EVT getIntegerVT(LLVMContext &Context, unsigned BitWidth) {
423 MVT M = MVT::getIntegerVT(BitWidth);
424 if (M.SimpleTy != MVT::INVALID_SIMPLE_VALUE_TYPE)
426 return getExtendedIntegerVT(Context, BitWidth);
429 /// getVectorVT - Returns the EVT that represents a vector NumElements in
430 /// length, where each element is of type VT.
431 static EVT getVectorVT(LLVMContext &Context, EVT VT, unsigned NumElements) {
432 MVT M = MVT::getVectorVT(VT.V, NumElements);
433 if (M.SimpleTy != MVT::INVALID_SIMPLE_VALUE_TYPE)
435 return getExtendedVectorVT(Context, VT, NumElements);
438 /// getIntVectorWithNumElements - Return any integer vector type that has
439 /// the specified number of elements.
440 static EVT getIntVectorWithNumElements(LLVMContext &C, unsigned NumElts) {
442 default: return getVectorVT(C, MVT::i8, NumElts);
443 case 1: return MVT::v1i64;
444 case 2: return MVT::v2i32;
445 case 4: return MVT::v4i16;
446 case 8: return MVT::v8i8;
447 case 16: return MVT::v16i8;
451 /// changeVectorElementTypeToInteger - Return a vector with the same number
452 /// of elements as this vector, but with the element type converted to an
453 /// integer type with the same bitwidth.
454 EVT changeVectorElementTypeToInteger() const {
456 return changeExtendedVectorElementTypeToInteger();
457 MVT EltTy = getSimpleVT().getVectorElementType();
458 unsigned BitWidth = EltTy.getSizeInBits();
459 MVT IntTy = MVT::getIntegerVT(BitWidth);
460 MVT VecTy = MVT::getVectorVT(IntTy, getVectorNumElements());
461 assert(VecTy != MVT::INVALID_SIMPLE_VALUE_TYPE &&
462 "Simple vector VT not representable by simple integer vector VT!");
466 /// isSimple - Test if the given EVT is simple (as opposed to being
468 bool isSimple() const {
469 return V.SimpleTy <= MVT::LastSimpleValueType;
472 /// isExtended - Test if the given EVT is extended (as opposed to
474 bool isExtended() const {
478 /// isFloatingPoint - Return true if this is a FP, or a vector FP type.
479 bool isFloatingPoint() const {
480 return isSimple() ? V.isFloatingPoint() : isExtendedFloatingPoint();
483 /// isInteger - Return true if this is an integer, or a vector integer type.
484 bool isInteger() const {
485 return isSimple() ? V.isInteger() : isExtendedInteger();
488 /// isVector - Return true if this is a vector value type.
489 bool isVector() const {
490 return isSimple() ? V.isVector() : isExtendedVector();
493 /// is64BitVector - Return true if this is a 64-bit vector type.
494 bool is64BitVector() const {
496 return isExtended64BitVector();
498 return (V == MVT::v8i8 || V==MVT::v4i16 || V==MVT::v2i32 ||
499 V == MVT::v1i64 || V==MVT::v2f32);
502 /// is128BitVector - Return true if this is a 128-bit vector type.
503 bool is128BitVector() const {
505 return isExtended128BitVector();
506 return (V==MVT::v16i8 || V==MVT::v8i16 || V==MVT::v4i32 ||
507 V==MVT::v2i64 || V==MVT::v4f32 || V==MVT::v2f64);
510 /// is256BitVector - Return true if this is a 256-bit vector type.
511 inline bool is256BitVector() const {
513 return isExtended256BitVector();
514 return (V == MVT::v8f32 || V == MVT::v4f64 || V == MVT::v32i8 ||
515 V == MVT::v16i16 || V == MVT::v8i32 || V == MVT::v4i64);
518 /// is512BitVector - Return true if this is a 512-bit vector type.
519 inline bool is512BitVector() const {
520 return isSimple() ? (V == MVT::v8i64) : isExtended512BitVector();
523 /// isOverloaded - Return true if this is an overloaded type for TableGen.
524 bool isOverloaded() const {
525 return (V==MVT::iAny || V==MVT::fAny || V==MVT::vAny || V==MVT::iPTRAny);
528 /// isByteSized - Return true if the bit size is a multiple of 8.
529 bool isByteSized() const {
530 return (getSizeInBits() & 7) == 0;
533 /// isRound - Return true if the size is a power-of-two number of bytes.
534 bool isRound() const {
535 unsigned BitSize = getSizeInBits();
536 return BitSize >= 8 && !(BitSize & (BitSize - 1));
539 /// bitsEq - Return true if this has the same number of bits as VT.
540 bool bitsEq(EVT VT) const {
541 if (EVT::operator==(VT)) return true;
542 return getSizeInBits() == VT.getSizeInBits();
545 /// bitsGT - Return true if this has more bits than VT.
546 bool bitsGT(EVT VT) const {
547 if (EVT::operator==(VT)) return false;
548 return getSizeInBits() > VT.getSizeInBits();
551 /// bitsGE - Return true if this has no less bits than VT.
552 bool bitsGE(EVT VT) const {
553 if (EVT::operator==(VT)) return true;
554 return getSizeInBits() >= VT.getSizeInBits();
557 /// bitsLT - Return true if this has less bits than VT.
558 bool bitsLT(EVT VT) const {
559 if (EVT::operator==(VT)) return false;
560 return getSizeInBits() < VT.getSizeInBits();
563 /// bitsLE - Return true if this has no more bits than VT.
564 bool bitsLE(EVT VT) const {
565 if (EVT::operator==(VT)) return true;
566 return getSizeInBits() <= VT.getSizeInBits();
570 /// getSimpleVT - Return the SimpleValueType held in the specified
572 MVT getSimpleVT() const {
573 assert(isSimple() && "Expected a SimpleValueType!");
577 /// getScalarType - If this is a vector type, return the element type,
578 /// otherwise return this.
579 EVT getScalarType() const {
580 return isVector() ? getVectorElementType() : *this;
583 /// getVectorElementType - Given a vector type, return the type of
585 EVT getVectorElementType() const {
586 assert(isVector() && "Invalid vector type!");
588 return V.getVectorElementType();
589 return getExtendedVectorElementType();
592 /// getVectorNumElements - Given a vector type, return the number of
593 /// elements it contains.
594 unsigned getVectorNumElements() const {
595 assert(isVector() && "Invalid vector type!");
597 return V.getVectorNumElements();
598 return getExtendedVectorNumElements();
601 /// getSizeInBits - Return the size of the specified value type in bits.
602 unsigned getSizeInBits() const {
604 return V.getSizeInBits();
605 return getExtendedSizeInBits();
608 /// getStoreSize - Return the number of bytes overwritten by a store
609 /// of the specified value type.
610 unsigned getStoreSize() const {
611 return (getSizeInBits() + 7) / 8;
614 /// getStoreSizeInBits - Return the number of bits overwritten by a store
615 /// of the specified value type.
616 unsigned getStoreSizeInBits() const {
617 return getStoreSize() * 8;
620 /// getRoundIntegerType - Rounds the bit-width of the given integer EVT up
621 /// to the nearest power of two (and at least to eight), and returns the
622 /// integer EVT with that number of bits.
623 EVT getRoundIntegerType(LLVMContext &Context) const {
624 assert(isInteger() && !isVector() && "Invalid integer type!");
625 unsigned BitWidth = getSizeInBits();
628 return getIntegerVT(Context, 1 << Log2_32_Ceil(BitWidth));
631 /// getHalfSizedIntegerVT - Finds the smallest simple value type that is
632 /// greater than or equal to half the width of this EVT. If no simple
633 /// value type can be found, an extended integer value type of half the
634 /// size (rounded up) is returned.
635 EVT getHalfSizedIntegerVT(LLVMContext &Context) const {
636 assert(isInteger() && !isVector() && "Invalid integer type!");
637 unsigned EVTSize = getSizeInBits();
638 for (unsigned IntVT = MVT::FIRST_INTEGER_VALUETYPE;
639 IntVT <= MVT::LAST_INTEGER_VALUETYPE; ++IntVT) {
640 EVT HalfVT = EVT((MVT::SimpleValueType)IntVT);
641 if (HalfVT.getSizeInBits() * 2 >= EVTSize)
644 return getIntegerVT(Context, (EVTSize + 1) / 2);
647 /// isPow2VectorType - Returns true if the given vector is a power of 2.
648 bool isPow2VectorType() const {
649 unsigned NElts = getVectorNumElements();
650 return !(NElts & (NElts - 1));
653 /// getPow2VectorType - Widens the length of the given vector EVT up to
654 /// the nearest power of 2 and returns that type.
655 EVT getPow2VectorType(LLVMContext &Context) const {
656 if (!isPow2VectorType()) {
657 unsigned NElts = getVectorNumElements();
658 unsigned Pow2NElts = 1 << Log2_32_Ceil(NElts);
659 return EVT::getVectorVT(Context, getVectorElementType(), Pow2NElts);
666 /// getEVTString - This function returns value type as a string,
668 std::string getEVTString() const;
670 /// getTypeForEVT - This method returns an LLVM type corresponding to the
671 /// specified EVT. For integer types, this returns an unsigned type. Note
672 /// that this will abort for types that cannot be represented.
673 Type *getTypeForEVT(LLVMContext &Context) const;
675 /// getEVT - Return the value type corresponding to the specified type.
676 /// This returns all pointers as iPTR. If HandleUnknown is true, unknown
677 /// types are returned as Other, otherwise they are invalid.
678 static EVT getEVT(Type *Ty, bool HandleUnknown = false);
680 intptr_t getRawBits() {
684 return (intptr_t)(LLVMTy);
687 /// compareRawBits - A meaningless but well-behaved order, useful for
688 /// constructing containers.
689 struct compareRawBits {
690 bool operator()(EVT L, EVT R) const {
691 if (L.V.SimpleTy == R.V.SimpleTy)
692 return L.LLVMTy < R.LLVMTy;
694 return L.V.SimpleTy < R.V.SimpleTy;
699 // Methods for handling the Extended-type case in functions above.
700 // These are all out-of-line to prevent users of this header file
701 // from having a dependency on Type.h.
702 EVT changeExtendedVectorElementTypeToInteger() const;
703 static EVT getExtendedIntegerVT(LLVMContext &C, unsigned BitWidth);
704 static EVT getExtendedVectorVT(LLVMContext &C, EVT VT,
705 unsigned NumElements);
706 bool isExtendedFloatingPoint() const;
707 bool isExtendedInteger() const;
708 bool isExtendedVector() const;
709 bool isExtended64BitVector() const;
710 bool isExtended128BitVector() const;
711 bool isExtended256BitVector() const;
712 bool isExtended512BitVector() const;
713 EVT getExtendedVectorElementType() const;
714 unsigned getExtendedVectorNumElements() const;
715 unsigned getExtendedSizeInBits() const;
718 } // End llvm namespace