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 f32 = 7, // This is a 32 bit floating point value
49 f64 = 8, // This is a 64 bit floating point value
50 f80 = 9, // This is a 80 bit floating point value
51 f128 = 10, // This is a 128 bit floating point value
52 ppcf128 = 11, // This is a PPC 128-bit floating point value
57 v16i8 = 15, // 16 x i8
58 v32i8 = 16, // 32 x i8
59 v2i16 = 17, // 2 x i16
60 v4i16 = 18, // 4 x i16
61 v8i16 = 19, // 8 x i16
62 v16i16 = 20, // 16 x i16
63 v2i32 = 21, // 2 x i32
64 v4i32 = 22, // 4 x i32
65 v8i32 = 23, // 8 x i32
66 v1i64 = 24, // 1 x i64
67 v2i64 = 25, // 2 x i64
68 v4i64 = 26, // 4 x i64
69 v8i64 = 27, // 8 x i64
71 v2f32 = 28, // 2 x f32
72 v4f32 = 29, // 4 x f32
73 v8f32 = 30, // 8 x f32
74 v2f64 = 31, // 2 x f64
75 v4f64 = 32, // 4 x f64
77 FIRST_VECTOR_VALUETYPE = v2i8,
78 LAST_VECTOR_VALUETYPE = v4f64,
80 x86mmx = 33, // This is an X86 MMX value
82 Glue = 34, // This glues nodes together during pre-RA sched
84 isVoid = 35, // This has no value
86 Untyped = 36, // This value takes a register, but has
87 // unspecified type. The register class
88 // will be determined by the opcode.
90 LAST_VALUETYPE = 37, // This always remains at the end of the list.
92 // This is the current maximum for LAST_VALUETYPE.
93 // MVT::MAX_ALLOWED_VALUETYPE is used for asserts and to size bit vectors
94 // This value must be a multiple of 32.
95 MAX_ALLOWED_VALUETYPE = 64,
97 // Metadata - This is MDNode or MDString.
100 // iPTRAny - An int value the size of the pointer of the current
101 // target to any address space. This must only be used internal to
102 // tblgen. Other than for overloading, we treat iPTRAny the same as iPTR.
105 // vAny - A vector with any length and element size. This is used
106 // for intrinsics that have overloadings based on vector types.
107 // This is only for tblgen's consumption!
110 // fAny - Any floating-point or vector floating-point value. This is used
111 // for intrinsics that have overloadings based on floating-point types.
112 // This is only for tblgen's consumption!
115 // iAny - An integer or vector integer value of any bit width. This is
116 // used for intrinsics that have overloadings based on integer bit widths.
117 // This is only for tblgen's consumption!
120 // iPTR - An int value the size of the pointer of the current
121 // target. This should only be used internal to tblgen!
124 // LastSimpleValueType - The greatest valid SimpleValueType value.
125 LastSimpleValueType = 255,
127 // INVALID_SIMPLE_VALUE_TYPE - Simple value types greater than or equal
128 // to this are considered extended value types.
129 INVALID_SIMPLE_VALUE_TYPE = LastSimpleValueType + 1
132 SimpleValueType SimpleTy;
134 MVT() : SimpleTy((SimpleValueType)(INVALID_SIMPLE_VALUE_TYPE)) {}
135 MVT(SimpleValueType SVT) : SimpleTy(SVT) { }
137 bool operator>(const MVT& S) const { return SimpleTy > S.SimpleTy; }
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; }
144 /// isFloatingPoint - Return true if this is a FP, or a vector FP type.
145 bool isFloatingPoint() const {
146 return ((SimpleTy >= MVT::f32 && SimpleTy <= MVT::ppcf128) ||
147 (SimpleTy >= MVT::v2f32 && SimpleTy <= MVT::v4f64));
150 /// isInteger - Return true if this is an integer, or a vector integer type.
151 bool isInteger() const {
152 return ((SimpleTy >= MVT::FIRST_INTEGER_VALUETYPE &&
153 SimpleTy <= MVT::LAST_INTEGER_VALUETYPE) ||
154 (SimpleTy >= MVT::v2i8 && SimpleTy <= MVT::v8i64));
157 /// isVector - Return true if this is a vector value type.
158 bool isVector() const {
159 return (SimpleTy >= MVT::FIRST_VECTOR_VALUETYPE &&
160 SimpleTy <= MVT::LAST_VECTOR_VALUETYPE);
163 /// isPow2VectorType - Returns true if the given vector is a power of 2.
164 bool isPow2VectorType() const {
165 unsigned NElts = getVectorNumElements();
166 return !(NElts & (NElts - 1));
169 /// getPow2VectorType - Widens the length of the given vector MVT up to
170 /// the nearest power of 2 and returns that type.
171 MVT getPow2VectorType() const {
172 if (isPow2VectorType())
175 unsigned NElts = getVectorNumElements();
176 unsigned Pow2NElts = 1 << Log2_32_Ceil(NElts);
177 return MVT::getVectorVT(getVectorElementType(), Pow2NElts);
180 /// getScalarType - If this is a vector type, return the element type,
181 /// otherwise return this.
182 MVT getScalarType() const {
183 return isVector() ? getVectorElementType() : *this;
186 MVT getVectorElementType() const {
189 return (MVT::SimpleValueType)(MVT::INVALID_SIMPLE_VALUE_TYPE);
194 case v32i8: return i8;
198 case v16i16: return i16;
201 case v8i32: return i32;
205 case v8i64: return i64;
208 case v8f32: return f32;
210 case v4f64: return f64;
214 unsigned getVectorNumElements() const {
218 case v32i8: return 32;
220 case v16i16: return 16;
225 case v8f32: return 8;
231 case v4f64: return 4;
237 case v2f64: return 2;
238 case v1i64: return 1;
242 unsigned getSizeInBits() const {
245 assert(0 && "Value type size is target-dependent. Ask TLI.");
249 assert(0 && "Value type is overloaded.");
251 assert(0 && "getSizeInBits called on extended MVT.");
255 case v2i8: return 16;
259 case v2i16: return 32;
267 case v2f32: return 64;
268 case f80 : return 80;
277 case v2f64: return 128;
283 case v4f64: return 256;
284 case v8i64: return 512;
288 /// getStoreSize - Return the number of bytes overwritten by a store
289 /// of the specified value type.
290 unsigned getStoreSize() const {
291 return (getSizeInBits() + 7) / 8;
294 /// getStoreSizeInBits - Return the number of bits overwritten by a store
295 /// of the specified value type.
296 unsigned getStoreSizeInBits() const {
297 return getStoreSize() * 8;
300 static MVT getFloatingPointVT(unsigned BitWidth) {
303 assert(false && "Bad bit width!");
315 static MVT getIntegerVT(unsigned BitWidth) {
318 return (MVT::SimpleValueType)(MVT::INVALID_SIMPLE_VALUE_TYPE);
334 static MVT getVectorVT(MVT VT, unsigned NumElements) {
335 switch (VT.SimpleTy) {
339 if (NumElements == 2) return MVT::v2i8;
340 if (NumElements == 4) return MVT::v4i8;
341 if (NumElements == 8) return MVT::v8i8;
342 if (NumElements == 16) return MVT::v16i8;
343 if (NumElements == 32) return MVT::v32i8;
346 if (NumElements == 2) return MVT::v2i16;
347 if (NumElements == 4) return MVT::v4i16;
348 if (NumElements == 8) return MVT::v8i16;
349 if (NumElements == 16) return MVT::v16i16;
352 if (NumElements == 2) return MVT::v2i32;
353 if (NumElements == 4) return MVT::v4i32;
354 if (NumElements == 8) return MVT::v8i32;
357 if (NumElements == 1) return MVT::v1i64;
358 if (NumElements == 2) return MVT::v2i64;
359 if (NumElements == 4) return MVT::v4i64;
360 if (NumElements == 8) return MVT::v8i64;
363 if (NumElements == 2) return MVT::v2f32;
364 if (NumElements == 4) return MVT::v4f32;
365 if (NumElements == 8) return MVT::v8f32;
368 if (NumElements == 2) return MVT::v2f64;
369 if (NumElements == 4) return MVT::v4f64;
372 return (MVT::SimpleValueType)(MVT::INVALID_SIMPLE_VALUE_TYPE);
377 /// EVT - Extended Value Type. Capable of holding value types which are not
378 /// native for any processor (such as the i12345 type), as well as the types
379 /// a MVT can represent.
386 EVT() : V((MVT::SimpleValueType)(MVT::INVALID_SIMPLE_VALUE_TYPE)),
388 EVT(MVT::SimpleValueType SVT) : V(SVT), LLVMTy(0) { }
389 EVT(MVT S) : V(S), LLVMTy(0) {}
391 bool operator==(EVT VT) const {
392 return !(*this != VT);
394 bool operator!=(EVT VT) const {
395 if (V.SimpleTy != VT.V.SimpleTy)
397 if (V.SimpleTy == MVT::INVALID_SIMPLE_VALUE_TYPE)
398 return LLVMTy != VT.LLVMTy;
402 /// getFloatingPointVT - Returns the EVT that represents a floating point
403 /// type with the given number of bits. There are two floating point types
404 /// with 128 bits - this returns f128 rather than ppcf128.
405 static EVT getFloatingPointVT(unsigned BitWidth) {
406 return MVT::getFloatingPointVT(BitWidth);
409 /// getIntegerVT - Returns the EVT that represents an integer with the given
411 static EVT getIntegerVT(LLVMContext &Context, unsigned BitWidth) {
412 MVT M = MVT::getIntegerVT(BitWidth);
413 if (M.SimpleTy != MVT::INVALID_SIMPLE_VALUE_TYPE)
415 return getExtendedIntegerVT(Context, BitWidth);
418 /// getVectorVT - Returns the EVT that represents a vector NumElements in
419 /// length, where each element is of type VT.
420 static EVT getVectorVT(LLVMContext &Context, EVT VT, unsigned NumElements) {
421 MVT M = MVT::getVectorVT(VT.V, NumElements);
422 if (M.SimpleTy != MVT::INVALID_SIMPLE_VALUE_TYPE)
424 return getExtendedVectorVT(Context, VT, NumElements);
427 /// getIntVectorWithNumElements - Return any integer vector type that has
428 /// the specified number of elements.
429 static EVT getIntVectorWithNumElements(LLVMContext &C, unsigned NumElts) {
431 default: return getVectorVT(C, MVT::i8, NumElts);
432 case 1: return MVT::v1i64;
433 case 2: return MVT::v2i32;
434 case 4: return MVT::v4i16;
435 case 8: return MVT::v8i8;
436 case 16: return MVT::v16i8;
438 return MVT::INVALID_SIMPLE_VALUE_TYPE;
441 /// changeVectorElementTypeToInteger - Return a vector with the same number
442 /// of elements as this vector, but with the element type converted to an
443 /// integer type with the same bitwidth.
444 EVT changeVectorElementTypeToInteger() const {
446 return changeExtendedVectorElementTypeToInteger();
447 MVT EltTy = getSimpleVT().getVectorElementType();
448 unsigned BitWidth = EltTy.getSizeInBits();
449 MVT IntTy = MVT::getIntegerVT(BitWidth);
450 MVT VecTy = MVT::getVectorVT(IntTy, getVectorNumElements());
451 assert(VecTy != MVT::INVALID_SIMPLE_VALUE_TYPE &&
452 "Simple vector VT not representable by simple integer vector VT!");
456 /// isSimple - Test if the given EVT is simple (as opposed to being
458 bool isSimple() const {
459 return V.SimpleTy <= MVT::LastSimpleValueType;
462 /// isExtended - Test if the given EVT is extended (as opposed to
464 bool isExtended() const {
468 /// isFloatingPoint - Return true if this is a FP, or a vector FP type.
469 bool isFloatingPoint() const {
470 return isSimple() ? V.isFloatingPoint() : isExtendedFloatingPoint();
473 /// isInteger - Return true if this is an integer, or a vector integer type.
474 bool isInteger() const {
475 return isSimple() ? V.isInteger() : isExtendedInteger();
478 /// isVector - Return true if this is a vector value type.
479 bool isVector() const {
480 return isSimple() ? V.isVector() : isExtendedVector();
483 /// is64BitVector - Return true if this is a 64-bit vector type.
484 bool is64BitVector() const {
486 return isExtended64BitVector();
488 return (V == MVT::v8i8 || V==MVT::v4i16 || V==MVT::v2i32 ||
489 V == MVT::v1i64 || V==MVT::v2f32);
492 /// is128BitVector - Return true if this is a 128-bit vector type.
493 bool is128BitVector() const {
495 return isExtended128BitVector();
496 return (V==MVT::v16i8 || V==MVT::v8i16 || V==MVT::v4i32 ||
497 V==MVT::v2i64 || V==MVT::v4f32 || V==MVT::v2f64);
500 /// is256BitVector - Return true if this is a 256-bit vector type.
501 inline bool is256BitVector() const {
503 return isExtended256BitVector();
504 return (V == MVT::v8f32 || V == MVT::v4f64 || V == MVT::v32i8 ||
505 V == MVT::v16i16 || V == MVT::v8i32 || V == MVT::v4i64);
508 /// is512BitVector - Return true if this is a 512-bit vector type.
509 inline bool is512BitVector() const {
510 return isSimple() ? (V == MVT::v8i64) : isExtended512BitVector();
513 /// isOverloaded - Return true if this is an overloaded type for TableGen.
514 bool isOverloaded() const {
515 return (V==MVT::iAny || V==MVT::fAny || V==MVT::vAny || V==MVT::iPTRAny);
518 /// isByteSized - Return true if the bit size is a multiple of 8.
519 bool isByteSized() const {
520 return (getSizeInBits() & 7) == 0;
523 /// isRound - Return true if the size is a power-of-two number of bytes.
524 bool isRound() const {
525 unsigned BitSize = getSizeInBits();
526 return BitSize >= 8 && !(BitSize & (BitSize - 1));
529 /// bitsEq - Return true if this has the same number of bits as VT.
530 bool bitsEq(EVT VT) const {
531 if (EVT::operator==(VT)) return true;
532 return getSizeInBits() == VT.getSizeInBits();
535 /// bitsGT - Return true if this has more bits than VT.
536 bool bitsGT(EVT VT) const {
537 if (EVT::operator==(VT)) return false;
538 return getSizeInBits() > VT.getSizeInBits();
541 /// bitsGE - Return true if this has no less bits than VT.
542 bool bitsGE(EVT VT) const {
543 if (EVT::operator==(VT)) return true;
544 return getSizeInBits() >= VT.getSizeInBits();
547 /// bitsLT - Return true if this has less bits than VT.
548 bool bitsLT(EVT VT) const {
549 if (EVT::operator==(VT)) return false;
550 return getSizeInBits() < VT.getSizeInBits();
553 /// bitsLE - Return true if this has no more bits than VT.
554 bool bitsLE(EVT VT) const {
555 if (EVT::operator==(VT)) return true;
556 return getSizeInBits() <= VT.getSizeInBits();
560 /// getSimpleVT - Return the SimpleValueType held in the specified
562 MVT getSimpleVT() const {
563 assert(isSimple() && "Expected a SimpleValueType!");
567 /// getScalarType - If this is a vector type, return the element type,
568 /// otherwise return this.
569 EVT getScalarType() const {
570 return isVector() ? getVectorElementType() : *this;
573 /// getVectorElementType - Given a vector type, return the type of
575 EVT getVectorElementType() const {
576 assert(isVector() && "Invalid vector type!");
578 return V.getVectorElementType();
579 return getExtendedVectorElementType();
582 /// getVectorNumElements - Given a vector type, return the number of
583 /// elements it contains.
584 unsigned getVectorNumElements() const {
585 assert(isVector() && "Invalid vector type!");
587 return V.getVectorNumElements();
588 return getExtendedVectorNumElements();
591 /// getSizeInBits - Return the size of the specified value type in bits.
592 unsigned getSizeInBits() const {
594 return V.getSizeInBits();
595 return getExtendedSizeInBits();
598 /// getStoreSize - Return the number of bytes overwritten by a store
599 /// of the specified value type.
600 unsigned getStoreSize() const {
601 return (getSizeInBits() + 7) / 8;
604 /// getStoreSizeInBits - Return the number of bits overwritten by a store
605 /// of the specified value type.
606 unsigned getStoreSizeInBits() const {
607 return getStoreSize() * 8;
610 /// getRoundIntegerType - Rounds the bit-width of the given integer EVT up
611 /// to the nearest power of two (and at least to eight), and returns the
612 /// integer EVT with that number of bits.
613 EVT getRoundIntegerType(LLVMContext &Context) const {
614 assert(isInteger() && !isVector() && "Invalid integer type!");
615 unsigned BitWidth = getSizeInBits();
618 return getIntegerVT(Context, 1 << Log2_32_Ceil(BitWidth));
621 /// getHalfSizedIntegerVT - Finds the smallest simple value type that is
622 /// greater than or equal to half the width of this EVT. If no simple
623 /// value type can be found, an extended integer value type of half the
624 /// size (rounded up) is returned.
625 EVT getHalfSizedIntegerVT(LLVMContext &Context) const {
626 assert(isInteger() && !isVector() && "Invalid integer type!");
627 unsigned EVTSize = getSizeInBits();
628 for (unsigned IntVT = MVT::FIRST_INTEGER_VALUETYPE;
629 IntVT <= MVT::LAST_INTEGER_VALUETYPE; ++IntVT) {
630 EVT HalfVT = EVT((MVT::SimpleValueType)IntVT);
631 if (HalfVT.getSizeInBits() * 2 >= EVTSize)
634 return getIntegerVT(Context, (EVTSize + 1) / 2);
637 /// isPow2VectorType - Returns true if the given vector is a power of 2.
638 bool isPow2VectorType() const {
639 unsigned NElts = getVectorNumElements();
640 return !(NElts & (NElts - 1));
643 /// getPow2VectorType - Widens the length of the given vector EVT up to
644 /// the nearest power of 2 and returns that type.
645 EVT getPow2VectorType(LLVMContext &Context) const {
646 if (!isPow2VectorType()) {
647 unsigned NElts = getVectorNumElements();
648 unsigned Pow2NElts = 1 << Log2_32_Ceil(NElts);
649 return EVT::getVectorVT(Context, getVectorElementType(), Pow2NElts);
656 /// getEVTString - This function returns value type as a string,
658 std::string getEVTString() const;
660 /// getTypeForEVT - This method returns an LLVM type corresponding to the
661 /// specified EVT. For integer types, this returns an unsigned type. Note
662 /// that this will abort for types that cannot be represented.
663 Type *getTypeForEVT(LLVMContext &Context) const;
665 /// getEVT - Return the value type corresponding to the specified type.
666 /// This returns all pointers as iPTR. If HandleUnknown is true, unknown
667 /// types are returned as Other, otherwise they are invalid.
668 static EVT getEVT(Type *Ty, bool HandleUnknown = false);
670 intptr_t getRawBits() {
674 return (intptr_t)(LLVMTy);
677 /// compareRawBits - A meaningless but well-behaved order, useful for
678 /// constructing containers.
679 struct compareRawBits {
680 bool operator()(EVT L, EVT R) const {
681 if (L.V.SimpleTy == R.V.SimpleTy)
682 return L.LLVMTy < R.LLVMTy;
684 return L.V.SimpleTy < R.V.SimpleTy;
689 // Methods for handling the Extended-type case in functions above.
690 // These are all out-of-line to prevent users of this header file
691 // from having a dependency on Type.h.
692 EVT changeExtendedVectorElementTypeToInteger() const;
693 static EVT getExtendedIntegerVT(LLVMContext &C, unsigned BitWidth);
694 static EVT getExtendedVectorVT(LLVMContext &C, EVT VT,
695 unsigned NumElements);
696 bool isExtendedFloatingPoint() const;
697 bool isExtendedInteger() const;
698 bool isExtendedVector() const;
699 bool isExtended64BitVector() const;
700 bool isExtended128BitVector() const;
701 bool isExtended256BitVector() const;
702 bool isExtended512BitVector() const;
703 EVT getExtendedVectorElementType() const;
704 unsigned getExtendedVectorNumElements() const;
705 unsigned getExtendedSizeInBits() const;
708 } // End llvm namespace