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 v2f32 = 29, // 2 x f32
73 v4f32 = 30, // 4 x f32
74 v8f32 = 31, // 8 x f32
75 v2f64 = 32, // 2 x f64
76 v4f64 = 33, // 4 x f64
78 FIRST_VECTOR_VALUETYPE = v2i8,
79 LAST_VECTOR_VALUETYPE = v4f64,
81 x86mmx = 34, // This is an X86 MMX value
83 Glue = 35, // This glues nodes together during pre-RA sched
85 isVoid = 36, // This has no value
87 Untyped = 37, // This value takes a register, but has
88 // unspecified type. The register class
89 // will be determined by the opcode.
91 LAST_VALUETYPE = 38, // This always remains at the end of the list.
93 // This is the current maximum for LAST_VALUETYPE.
94 // MVT::MAX_ALLOWED_VALUETYPE is used for asserts and to size bit vectors
95 // This value must be a multiple of 32.
96 MAX_ALLOWED_VALUETYPE = 64,
98 // Metadata - This is MDNode or MDString.
101 // iPTRAny - An int value the size of the pointer of the current
102 // target to any address space. This must only be used internal to
103 // tblgen. Other than for overloading, we treat iPTRAny the same as iPTR.
106 // vAny - A vector with any length and element size. This is used
107 // for intrinsics that have overloadings based on vector types.
108 // This is only for tblgen's consumption!
111 // fAny - Any floating-point or vector floating-point value. This is used
112 // for intrinsics that have overloadings based on floating-point types.
113 // This is only for tblgen's consumption!
116 // iAny - An integer or vector integer value of any bit width. This is
117 // used for intrinsics that have overloadings based on integer bit widths.
118 // This is only for tblgen's consumption!
121 // iPTR - An int value the size of the pointer of the current
122 // target. This should only be used internal to tblgen!
125 // LastSimpleValueType - The greatest valid SimpleValueType value.
126 LastSimpleValueType = 255,
128 // INVALID_SIMPLE_VALUE_TYPE - Simple value types greater than or equal
129 // to this are considered extended value types.
130 INVALID_SIMPLE_VALUE_TYPE = LastSimpleValueType + 1
133 SimpleValueType SimpleTy;
135 MVT() : SimpleTy((SimpleValueType)(INVALID_SIMPLE_VALUE_TYPE)) {}
136 MVT(SimpleValueType SVT) : SimpleTy(SVT) { }
138 bool operator>(const MVT& S) const { return SimpleTy > S.SimpleTy; }
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; }
145 /// isFloatingPoint - Return true if this is a FP, or a vector FP type.
146 bool isFloatingPoint() const {
147 return ((SimpleTy >= MVT::f16 && SimpleTy <= MVT::ppcf128) ||
148 (SimpleTy >= MVT::v2f32 && SimpleTy <= MVT::v4f64));
151 /// isInteger - Return true if this is an integer, or a vector integer type.
152 bool isInteger() const {
153 return ((SimpleTy >= MVT::FIRST_INTEGER_VALUETYPE &&
154 SimpleTy <= MVT::LAST_INTEGER_VALUETYPE) ||
155 (SimpleTy >= MVT::v2i8 && SimpleTy <= MVT::v8i64));
158 /// isVector - Return true if this is a vector value type.
159 bool isVector() const {
160 return (SimpleTy >= MVT::FIRST_VECTOR_VALUETYPE &&
161 SimpleTy <= MVT::LAST_VECTOR_VALUETYPE);
164 /// isPow2VectorType - Returns true if the given vector is a power of 2.
165 bool isPow2VectorType() const {
166 unsigned NElts = getVectorNumElements();
167 return !(NElts & (NElts - 1));
170 /// getPow2VectorType - Widens the length of the given vector MVT up to
171 /// the nearest power of 2 and returns that type.
172 MVT getPow2VectorType() const {
173 if (isPow2VectorType())
176 unsigned NElts = getVectorNumElements();
177 unsigned Pow2NElts = 1 << Log2_32_Ceil(NElts);
178 return MVT::getVectorVT(getVectorElementType(), Pow2NElts);
181 /// getScalarType - If this is a vector type, return the element type,
182 /// otherwise return this.
183 MVT getScalarType() const {
184 return isVector() ? getVectorElementType() : *this;
187 MVT getVectorElementType() const {
190 return (MVT::SimpleValueType)(MVT::INVALID_SIMPLE_VALUE_TYPE);
195 case v32i8: return i8;
199 case v16i16: return i16;
202 case v8i32: return i32;
206 case v8i64: return i64;
209 case v8f32: return f32;
211 case v4f64: return f64;
215 unsigned getVectorNumElements() const {
219 case v32i8: return 32;
221 case v16i16: return 16;
226 case v8f32: return 8;
232 case v4f64: return 4;
238 case v2f64: return 2;
239 case v1i64: return 1;
243 unsigned getSizeInBits() const {
246 assert(0 && "Value type size is target-dependent. Ask TLI.");
250 assert(0 && "Value type is overloaded.");
252 assert(0 && "getSizeInBits called on extended MVT.");
257 case v2i8: return 16;
261 case v2i16: return 32;
269 case v2f32: return 64;
270 case f80 : return 80;
279 case v2f64: return 128;
285 case v4f64: return 256;
286 case v8i64: return 512;
290 /// getStoreSize - Return the number of bytes overwritten by a store
291 /// of the specified value type.
292 unsigned getStoreSize() const {
293 return (getSizeInBits() + 7) / 8;
296 /// getStoreSizeInBits - Return the number of bits overwritten by a store
297 /// of the specified value type.
298 unsigned getStoreSizeInBits() const {
299 return getStoreSize() * 8;
302 static MVT getFloatingPointVT(unsigned BitWidth) {
305 assert(false && "Bad bit width!");
319 static MVT getIntegerVT(unsigned BitWidth) {
322 return (MVT::SimpleValueType)(MVT::INVALID_SIMPLE_VALUE_TYPE);
338 static MVT getVectorVT(MVT VT, unsigned NumElements) {
339 switch (VT.SimpleTy) {
343 if (NumElements == 2) return MVT::v2i8;
344 if (NumElements == 4) return MVT::v4i8;
345 if (NumElements == 8) return MVT::v8i8;
346 if (NumElements == 16) return MVT::v16i8;
347 if (NumElements == 32) return MVT::v32i8;
350 if (NumElements == 2) return MVT::v2i16;
351 if (NumElements == 4) return MVT::v4i16;
352 if (NumElements == 8) return MVT::v8i16;
353 if (NumElements == 16) return MVT::v16i16;
356 if (NumElements == 2) return MVT::v2i32;
357 if (NumElements == 4) return MVT::v4i32;
358 if (NumElements == 8) return MVT::v8i32;
361 if (NumElements == 1) return MVT::v1i64;
362 if (NumElements == 2) return MVT::v2i64;
363 if (NumElements == 4) return MVT::v4i64;
364 if (NumElements == 8) return MVT::v8i64;
367 if (NumElements == 2) return MVT::v2f32;
368 if (NumElements == 4) return MVT::v4f32;
369 if (NumElements == 8) return MVT::v8f32;
372 if (NumElements == 2) return MVT::v2f64;
373 if (NumElements == 4) return MVT::v4f64;
376 return (MVT::SimpleValueType)(MVT::INVALID_SIMPLE_VALUE_TYPE);
381 /// EVT - Extended Value Type. Capable of holding value types which are not
382 /// native for any processor (such as the i12345 type), as well as the types
383 /// a MVT can represent.
390 EVT() : V((MVT::SimpleValueType)(MVT::INVALID_SIMPLE_VALUE_TYPE)),
392 EVT(MVT::SimpleValueType SVT) : V(SVT), LLVMTy(0) { }
393 EVT(MVT S) : V(S), LLVMTy(0) {}
395 bool operator==(EVT VT) const {
396 return !(*this != VT);
398 bool operator!=(EVT VT) const {
399 if (V.SimpleTy != VT.V.SimpleTy)
401 if (V.SimpleTy == MVT::INVALID_SIMPLE_VALUE_TYPE)
402 return LLVMTy != VT.LLVMTy;
406 /// getFloatingPointVT - Returns the EVT that represents a floating point
407 /// type with the given number of bits. There are two floating point types
408 /// with 128 bits - this returns f128 rather than ppcf128.
409 static EVT getFloatingPointVT(unsigned BitWidth) {
410 return MVT::getFloatingPointVT(BitWidth);
413 /// getIntegerVT - Returns the EVT that represents an integer with the given
415 static EVT getIntegerVT(LLVMContext &Context, unsigned BitWidth) {
416 MVT M = MVT::getIntegerVT(BitWidth);
417 if (M.SimpleTy != MVT::INVALID_SIMPLE_VALUE_TYPE)
419 return getExtendedIntegerVT(Context, BitWidth);
422 /// getVectorVT - Returns the EVT that represents a vector NumElements in
423 /// length, where each element is of type VT.
424 static EVT getVectorVT(LLVMContext &Context, EVT VT, unsigned NumElements) {
425 MVT M = MVT::getVectorVT(VT.V, NumElements);
426 if (M.SimpleTy != MVT::INVALID_SIMPLE_VALUE_TYPE)
428 return getExtendedVectorVT(Context, VT, NumElements);
431 /// getIntVectorWithNumElements - Return any integer vector type that has
432 /// the specified number of elements.
433 static EVT getIntVectorWithNumElements(LLVMContext &C, unsigned NumElts) {
435 default: return getVectorVT(C, MVT::i8, NumElts);
436 case 1: return MVT::v1i64;
437 case 2: return MVT::v2i32;
438 case 4: return MVT::v4i16;
439 case 8: return MVT::v8i8;
440 case 16: return MVT::v16i8;
442 return MVT::INVALID_SIMPLE_VALUE_TYPE;
445 /// changeVectorElementTypeToInteger - Return a vector with the same number
446 /// of elements as this vector, but with the element type converted to an
447 /// integer type with the same bitwidth.
448 EVT changeVectorElementTypeToInteger() const {
450 return changeExtendedVectorElementTypeToInteger();
451 MVT EltTy = getSimpleVT().getVectorElementType();
452 unsigned BitWidth = EltTy.getSizeInBits();
453 MVT IntTy = MVT::getIntegerVT(BitWidth);
454 MVT VecTy = MVT::getVectorVT(IntTy, getVectorNumElements());
455 assert(VecTy != MVT::INVALID_SIMPLE_VALUE_TYPE &&
456 "Simple vector VT not representable by simple integer vector VT!");
460 /// isSimple - Test if the given EVT is simple (as opposed to being
462 bool isSimple() const {
463 return V.SimpleTy <= MVT::LastSimpleValueType;
466 /// isExtended - Test if the given EVT is extended (as opposed to
468 bool isExtended() const {
472 /// isFloatingPoint - Return true if this is a FP, or a vector FP type.
473 bool isFloatingPoint() const {
474 return isSimple() ? V.isFloatingPoint() : isExtendedFloatingPoint();
477 /// isInteger - Return true if this is an integer, or a vector integer type.
478 bool isInteger() const {
479 return isSimple() ? V.isInteger() : isExtendedInteger();
482 /// isVector - Return true if this is a vector value type.
483 bool isVector() const {
484 return isSimple() ? V.isVector() : isExtendedVector();
487 /// is64BitVector - Return true if this is a 64-bit vector type.
488 bool is64BitVector() const {
490 return isExtended64BitVector();
492 return (V == MVT::v8i8 || V==MVT::v4i16 || V==MVT::v2i32 ||
493 V == MVT::v1i64 || V==MVT::v2f32);
496 /// is128BitVector - Return true if this is a 128-bit vector type.
497 bool is128BitVector() const {
499 return isExtended128BitVector();
500 return (V==MVT::v16i8 || V==MVT::v8i16 || V==MVT::v4i32 ||
501 V==MVT::v2i64 || V==MVT::v4f32 || V==MVT::v2f64);
504 /// is256BitVector - Return true if this is a 256-bit vector type.
505 inline bool is256BitVector() const {
507 return isExtended256BitVector();
508 return (V == MVT::v8f32 || V == MVT::v4f64 || V == MVT::v32i8 ||
509 V == MVT::v16i16 || V == MVT::v8i32 || V == MVT::v4i64);
512 /// is512BitVector - Return true if this is a 512-bit vector type.
513 inline bool is512BitVector() const {
514 return isSimple() ? (V == MVT::v8i64) : isExtended512BitVector();
517 /// isOverloaded - Return true if this is an overloaded type for TableGen.
518 bool isOverloaded() const {
519 return (V==MVT::iAny || V==MVT::fAny || V==MVT::vAny || V==MVT::iPTRAny);
522 /// isByteSized - Return true if the bit size is a multiple of 8.
523 bool isByteSized() const {
524 return (getSizeInBits() & 7) == 0;
527 /// isRound - Return true if the size is a power-of-two number of bytes.
528 bool isRound() const {
529 unsigned BitSize = getSizeInBits();
530 return BitSize >= 8 && !(BitSize & (BitSize - 1));
533 /// bitsEq - Return true if this has the same number of bits as VT.
534 bool bitsEq(EVT VT) const {
535 if (EVT::operator==(VT)) return true;
536 return getSizeInBits() == VT.getSizeInBits();
539 /// bitsGT - Return true if this has more bits than VT.
540 bool bitsGT(EVT VT) const {
541 if (EVT::operator==(VT)) return false;
542 return getSizeInBits() > VT.getSizeInBits();
545 /// bitsGE - Return true if this has no less bits than VT.
546 bool bitsGE(EVT VT) const {
547 if (EVT::operator==(VT)) return true;
548 return getSizeInBits() >= VT.getSizeInBits();
551 /// bitsLT - Return true if this has less bits than VT.
552 bool bitsLT(EVT VT) const {
553 if (EVT::operator==(VT)) return false;
554 return getSizeInBits() < VT.getSizeInBits();
557 /// bitsLE - Return true if this has no more bits than VT.
558 bool bitsLE(EVT VT) const {
559 if (EVT::operator==(VT)) return true;
560 return getSizeInBits() <= VT.getSizeInBits();
564 /// getSimpleVT - Return the SimpleValueType held in the specified
566 MVT getSimpleVT() const {
567 assert(isSimple() && "Expected a SimpleValueType!");
571 /// getScalarType - If this is a vector type, return the element type,
572 /// otherwise return this.
573 EVT getScalarType() const {
574 return isVector() ? getVectorElementType() : *this;
577 /// getVectorElementType - Given a vector type, return the type of
579 EVT getVectorElementType() const {
580 assert(isVector() && "Invalid vector type!");
582 return V.getVectorElementType();
583 return getExtendedVectorElementType();
586 /// getVectorNumElements - Given a vector type, return the number of
587 /// elements it contains.
588 unsigned getVectorNumElements() const {
589 assert(isVector() && "Invalid vector type!");
591 return V.getVectorNumElements();
592 return getExtendedVectorNumElements();
595 /// getSizeInBits - Return the size of the specified value type in bits.
596 unsigned getSizeInBits() const {
598 return V.getSizeInBits();
599 return getExtendedSizeInBits();
602 /// getStoreSize - Return the number of bytes overwritten by a store
603 /// of the specified value type.
604 unsigned getStoreSize() const {
605 return (getSizeInBits() + 7) / 8;
608 /// getStoreSizeInBits - Return the number of bits overwritten by a store
609 /// of the specified value type.
610 unsigned getStoreSizeInBits() const {
611 return getStoreSize() * 8;
614 /// getRoundIntegerType - Rounds the bit-width of the given integer EVT up
615 /// to the nearest power of two (and at least to eight), and returns the
616 /// integer EVT with that number of bits.
617 EVT getRoundIntegerType(LLVMContext &Context) const {
618 assert(isInteger() && !isVector() && "Invalid integer type!");
619 unsigned BitWidth = getSizeInBits();
622 return getIntegerVT(Context, 1 << Log2_32_Ceil(BitWidth));
625 /// getHalfSizedIntegerVT - Finds the smallest simple value type that is
626 /// greater than or equal to half the width of this EVT. If no simple
627 /// value type can be found, an extended integer value type of half the
628 /// size (rounded up) is returned.
629 EVT getHalfSizedIntegerVT(LLVMContext &Context) const {
630 assert(isInteger() && !isVector() && "Invalid integer type!");
631 unsigned EVTSize = getSizeInBits();
632 for (unsigned IntVT = MVT::FIRST_INTEGER_VALUETYPE;
633 IntVT <= MVT::LAST_INTEGER_VALUETYPE; ++IntVT) {
634 EVT HalfVT = EVT((MVT::SimpleValueType)IntVT);
635 if (HalfVT.getSizeInBits() * 2 >= EVTSize)
638 return getIntegerVT(Context, (EVTSize + 1) / 2);
641 /// isPow2VectorType - Returns true if the given vector is a power of 2.
642 bool isPow2VectorType() const {
643 unsigned NElts = getVectorNumElements();
644 return !(NElts & (NElts - 1));
647 /// getPow2VectorType - Widens the length of the given vector EVT up to
648 /// the nearest power of 2 and returns that type.
649 EVT getPow2VectorType(LLVMContext &Context) const {
650 if (!isPow2VectorType()) {
651 unsigned NElts = getVectorNumElements();
652 unsigned Pow2NElts = 1 << Log2_32_Ceil(NElts);
653 return EVT::getVectorVT(Context, getVectorElementType(), Pow2NElts);
660 /// getEVTString - This function returns value type as a string,
662 std::string getEVTString() const;
664 /// getTypeForEVT - This method returns an LLVM type corresponding to the
665 /// specified EVT. For integer types, this returns an unsigned type. Note
666 /// that this will abort for types that cannot be represented.
667 Type *getTypeForEVT(LLVMContext &Context) const;
669 /// getEVT - Return the value type corresponding to the specified type.
670 /// This returns all pointers as iPTR. If HandleUnknown is true, unknown
671 /// types are returned as Other, otherwise they are invalid.
672 static EVT getEVT(Type *Ty, bool HandleUnknown = false);
674 intptr_t getRawBits() {
678 return (intptr_t)(LLVMTy);
681 /// compareRawBits - A meaningless but well-behaved order, useful for
682 /// constructing containers.
683 struct compareRawBits {
684 bool operator()(EVT L, EVT R) const {
685 if (L.V.SimpleTy == R.V.SimpleTy)
686 return L.LLVMTy < R.LLVMTy;
688 return L.V.SimpleTy < R.V.SimpleTy;
693 // Methods for handling the Extended-type case in functions above.
694 // These are all out-of-line to prevent users of this header file
695 // from having a dependency on Type.h.
696 EVT changeExtendedVectorElementTypeToInteger() const;
697 static EVT getExtendedIntegerVT(LLVMContext &C, unsigned BitWidth);
698 static EVT getExtendedVectorVT(LLVMContext &C, EVT VT,
699 unsigned NumElements);
700 bool isExtendedFloatingPoint() const;
701 bool isExtendedInteger() const;
702 bool isExtendedVector() const;
703 bool isExtended64BitVector() const;
704 bool isExtended128BitVector() const;
705 bool isExtended256BitVector() const;
706 bool isExtended512BitVector() const;
707 EVT getExtendedVectorElementType() const;
708 unsigned getExtendedVectorNumElements() const;
709 unsigned getExtendedSizeInBits() const;
712 } // End llvm namespace