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/System/DataTypes.h"
22 #include "llvm/Support/MathExtras.h"
29 class MVT { // MVT = Machine Value Type
31 enum SimpleValueType {
32 // If you change this numbering, you must change the values in
33 // ValueTypes.td as well!
34 Other = 0, // This is a non-standard value
35 i1 = 1, // This is a 1 bit integer value
36 i8 = 2, // This is an 8 bit integer value
37 i16 = 3, // This is a 16 bit integer value
38 i32 = 4, // This is a 32 bit integer value
39 i64 = 5, // This is a 64 bit integer value
40 i128 = 6, // This is a 128 bit integer value
42 FIRST_INTEGER_VALUETYPE = i1,
43 LAST_INTEGER_VALUETYPE = i128,
45 f32 = 7, // This is a 32 bit floating point value
46 f64 = 8, // This is a 64 bit floating point value
47 f80 = 9, // This is a 80 bit floating point value
48 f128 = 10, // This is a 128 bit floating point value
49 ppcf128 = 11, // This is a PPC 128-bit floating point value
54 v16i8 = 15, // 16 x i8
55 v32i8 = 16, // 32 x i8
56 v2i16 = 17, // 2 x i16
57 v4i16 = 18, // 4 x i16
58 v8i16 = 19, // 8 x i16
59 v16i16 = 20, // 16 x i16
60 v2i32 = 21, // 2 x i32
61 v4i32 = 22, // 4 x i32
62 v8i32 = 23, // 8 x i32
63 v1i64 = 24, // 1 x i64
64 v2i64 = 25, // 2 x i64
65 v4i64 = 26, // 4 x i64
66 v8i64 = 27, // 8 x i64
68 v2f32 = 28, // 2 x f32
69 v4f32 = 29, // 4 x f32
70 v8f32 = 30, // 8 x f32
71 v2f64 = 31, // 2 x f64
72 v4f64 = 32, // 4 x f64
74 FIRST_VECTOR_VALUETYPE = v2i8,
75 LAST_VECTOR_VALUETYPE = v4f64,
77 Flag = 33, // This glues nodes together during pre-RA sched
79 isVoid = 34, // This has no value
81 LAST_VALUETYPE = 34, // This always remains at the end of the list.
83 // This is the current maximum for LAST_VALUETYPE.
84 // EVT::MAX_ALLOWED_VALUETYPE is used for asserts and to size bit vectors
85 // This value must be a multiple of 32.
86 MAX_ALLOWED_VALUETYPE = 64,
88 // Metadata - This is MDNode or MDString.
91 // iPTRAny - An int value the size of the pointer of the current
92 // target to any address space. This must only be used internal to
93 // tblgen. Other than for overloading, we treat iPTRAny the same as iPTR.
96 // vAny - A vector with any length and element size. This is used
97 // for intrinsics that have overloadings based on vector types.
98 // This is only for tblgen's consumption!
101 // fAny - Any floating-point or vector floating-point value. This is used
102 // for intrinsics that have overloadings based on floating-point types.
103 // This is only for tblgen's consumption!
106 // iAny - An integer or vector integer value of any bit width. This is
107 // used for intrinsics that have overloadings based on integer bit widths.
108 // This is only for tblgen's consumption!
111 // iPTR - An int value the size of the pointer of the current
112 // target. This should only be used internal to tblgen!
115 // LastSimpleValueType - The greatest valid SimpleValueType value.
116 LastSimpleValueType = 255,
118 // INVALID_SIMPLE_VALUE_TYPE - Simple value types greater than or equal
119 // to this are considered extended value types.
120 INVALID_SIMPLE_VALUE_TYPE = LastSimpleValueType + 1
123 SimpleValueType SimpleTy;
125 MVT() : SimpleTy((SimpleValueType)(INVALID_SIMPLE_VALUE_TYPE)) {}
126 MVT(SimpleValueType SVT) : SimpleTy(SVT) { }
128 bool operator>(const MVT& S) const { return SimpleTy > S.SimpleTy; }
129 bool operator<(const MVT& S) const { return SimpleTy < S.SimpleTy; }
130 bool operator==(const MVT& S) const { return SimpleTy == S.SimpleTy; }
131 bool operator>=(const MVT& S) const { return SimpleTy >= S.SimpleTy; }
132 bool operator<=(const MVT& S) const { return SimpleTy <= S.SimpleTy; }
134 /// isFloatingPoint - Return true if this is a FP, or a vector FP type.
135 bool isFloatingPoint() const {
136 return ((SimpleTy >= MVT::f32 && SimpleTy <= MVT::ppcf128) ||
137 (SimpleTy >= MVT::v2f32 && SimpleTy <= MVT::v4f64));
140 /// isInteger - Return true if this is an integer, or a vector integer type.
141 bool isInteger() const {
142 return ((SimpleTy >= MVT::FIRST_INTEGER_VALUETYPE &&
143 SimpleTy <= MVT::LAST_INTEGER_VALUETYPE) ||
144 (SimpleTy >= MVT::v2i8 && SimpleTy <= MVT::v8i64));
147 /// isVector - Return true if this is a vector value type.
148 bool isVector() const {
149 return (SimpleTy >= MVT::FIRST_VECTOR_VALUETYPE &&
150 SimpleTy <= MVT::LAST_VECTOR_VALUETYPE);
153 /// isPow2VectorType - Returns true if the given vector is a power of 2.
154 bool isPow2VectorType() const {
155 unsigned NElts = getVectorNumElements();
156 return !(NElts & (NElts - 1));
159 /// getPow2VectorType - Widens the length of the given vector EVT up to
160 /// the nearest power of 2 and returns that type.
161 MVT getPow2VectorType() const {
162 if (!isPow2VectorType()) {
163 unsigned NElts = getVectorNumElements();
164 unsigned Pow2NElts = 1 << Log2_32_Ceil(NElts);
165 return MVT::getVectorVT(getVectorElementType(), Pow2NElts);
172 /// getScalarType - If this is a vector type, return the element type,
173 /// otherwise return this.
174 MVT getScalarType() const {
175 return isVector() ? getVectorElementType() : *this;
178 MVT getVectorElementType() const {
181 return (MVT::SimpleValueType)(MVT::INVALID_SIMPLE_VALUE_TYPE);
186 case v32i8: return i8;
190 case v16i16: return i16;
193 case v8i32: return i32;
197 case v8i64: return i64;
200 case v8f32: return f32;
202 case v4f64: return f64;
206 unsigned getVectorNumElements() const {
210 case v32i8: return 32;
212 case v16i16: return 16;
217 case v8f32: return 8;
223 case v4f64: return 4;
229 case v2f64: return 2;
230 case v1i64: return 1;
234 unsigned getSizeInBits() const {
237 assert(0 && "Value type size is target-dependent. Ask TLI.");
241 assert(0 && "Value type is overloaded.");
243 assert(0 && "getSizeInBits called on extended MVT.");
247 case v2i8: return 16;
251 case v2i16: return 32;
258 case v2f32: return 64;
259 case f80 : return 80;
268 case v2f64: return 128;
274 case v4f64: return 256;
275 case v8i64: return 512;
279 static MVT getFloatingPointVT(unsigned BitWidth) {
282 assert(false && "Bad bit width!");
294 static MVT getIntegerVT(unsigned BitWidth) {
297 return (MVT::SimpleValueType)(MVT::INVALID_SIMPLE_VALUE_TYPE);
313 static MVT getVectorVT(MVT VT, unsigned NumElements) {
314 switch (VT.SimpleTy) {
318 if (NumElements == 2) return MVT::v2i8;
319 if (NumElements == 4) return MVT::v4i8;
320 if (NumElements == 8) return MVT::v8i8;
321 if (NumElements == 16) return MVT::v16i8;
322 if (NumElements == 32) return MVT::v32i8;
325 if (NumElements == 2) return MVT::v2i16;
326 if (NumElements == 4) return MVT::v4i16;
327 if (NumElements == 8) return MVT::v8i16;
328 if (NumElements == 16) return MVT::v16i16;
331 if (NumElements == 2) return MVT::v2i32;
332 if (NumElements == 4) return MVT::v4i32;
333 if (NumElements == 8) return MVT::v8i32;
336 if (NumElements == 1) return MVT::v1i64;
337 if (NumElements == 2) return MVT::v2i64;
338 if (NumElements == 4) return MVT::v4i64;
339 if (NumElements == 8) return MVT::v8i64;
342 if (NumElements == 2) return MVT::v2f32;
343 if (NumElements == 4) return MVT::v4f32;
344 if (NumElements == 8) return MVT::v8f32;
347 if (NumElements == 2) return MVT::v2f64;
348 if (NumElements == 4) return MVT::v4f64;
351 return (MVT::SimpleValueType)(MVT::INVALID_SIMPLE_VALUE_TYPE);
354 static MVT getIntVectorWithNumElements(unsigned NumElts) {
356 default: return (MVT::SimpleValueType)(MVT::INVALID_SIMPLE_VALUE_TYPE);
357 case 1: return MVT::v1i64;
358 case 2: return MVT::v2i32;
359 case 4: return MVT::v4i16;
360 case 8: return MVT::v8i8;
361 case 16: return MVT::v16i8;
366 struct EVT { // EVT = Extended Value Type
372 EVT() : V((MVT::SimpleValueType)(MVT::INVALID_SIMPLE_VALUE_TYPE)),
374 EVT(MVT::SimpleValueType SVT) : V(SVT), LLVMTy(0) { }
375 EVT(MVT S) : V(S), LLVMTy(0) {}
377 bool operator==(const EVT VT) const {
378 if (V.SimpleTy == VT.V.SimpleTy) {
379 if (V.SimpleTy == MVT::INVALID_SIMPLE_VALUE_TYPE)
380 return LLVMTy == VT.LLVMTy;
385 bool operator!=(const EVT VT) const {
386 if (V.SimpleTy == VT.V.SimpleTy) {
387 if (V.SimpleTy == MVT::INVALID_SIMPLE_VALUE_TYPE)
388 return LLVMTy != VT.LLVMTy;
394 /// getFloatingPointVT - Returns the EVT that represents a floating point
395 /// type with the given number of bits. There are two floating point types
396 /// with 128 bits - this returns f128 rather than ppcf128.
397 static EVT getFloatingPointVT(unsigned BitWidth) {
398 return MVT::getFloatingPointVT(BitWidth);
401 /// getIntegerVT - Returns the EVT that represents an integer with the given
403 static EVT getIntegerVT(LLVMContext &Context, unsigned BitWidth) {
404 MVT M = MVT::getIntegerVT(BitWidth);
405 if (M.SimpleTy == MVT::INVALID_SIMPLE_VALUE_TYPE)
406 return getExtendedIntegerVT(Context, BitWidth);
411 /// getVectorVT - Returns the EVT that represents a vector NumElements in
412 /// length, where each element is of type VT.
413 static EVT getVectorVT(LLVMContext &Context, EVT VT, unsigned NumElements) {
414 MVT M = MVT::getVectorVT(VT.V, NumElements);
415 if (M.SimpleTy == MVT::INVALID_SIMPLE_VALUE_TYPE)
416 return getExtendedVectorVT(Context, VT, NumElements);
421 /// getIntVectorWithNumElements - Return any integer vector type that has
422 /// the specified number of elements.
423 static EVT getIntVectorWithNumElements(LLVMContext &C, unsigned NumElts) {
424 MVT M = MVT::getIntVectorWithNumElements(NumElts);
425 if (M.SimpleTy == MVT::INVALID_SIMPLE_VALUE_TYPE)
426 return getVectorVT(C, MVT::i8, NumElts);
431 /// isSimple - Test if the given EVT is simple (as opposed to being
433 bool isSimple() const {
434 return V.SimpleTy <= MVT::LastSimpleValueType;
437 /// isExtended - Test if the given EVT is extended (as opposed to
439 bool isExtended() const {
443 /// isFloatingPoint - Return true if this is a FP, or a vector FP type.
444 bool isFloatingPoint() const {
445 return isSimple() ? V.isFloatingPoint() : isExtendedFloatingPoint();
448 /// isInteger - Return true if this is an integer, or a vector integer type.
449 bool isInteger() const {
450 return isSimple() ? V.isInteger() : isExtendedInteger();
453 /// isVector - Return true if this is a vector value type.
454 bool isVector() const {
455 return isSimple() ? V.isVector() : isExtendedVector();
458 /// is64BitVector - Return true if this is a 64-bit vector type.
459 bool is64BitVector() const {
461 (V==MVT::v8i8 || V==MVT::v4i16 || V==MVT::v2i32 ||
462 V==MVT::v1i64 || V==MVT::v2f32) :
463 isExtended64BitVector();
466 /// is128BitVector - Return true if this is a 128-bit vector type.
467 bool is128BitVector() const {
469 (V==MVT::v16i8 || V==MVT::v8i16 || V==MVT::v4i32 ||
470 V==MVT::v2i64 || V==MVT::v4f32 || V==MVT::v2f64) :
471 isExtended128BitVector();
474 /// is256BitVector - Return true if this is a 256-bit vector type.
475 inline bool is256BitVector() const {
477 ? (V==MVT::v8f32 || V==MVT::v4f64 || V==MVT::v32i8 ||
478 V==MVT::v16i16 || V==MVT::v8i32 || V==MVT::v4i64)
479 : isExtended256BitVector();
482 /// is512BitVector - Return true if this is a 512-bit vector type.
483 inline bool is512BitVector() const {
484 return isSimple() ? (V == MVT::v8i64) : isExtended512BitVector();
487 /// isOverloaded - Return true if this is an overloaded type for TableGen.
488 bool isOverloaded() const {
489 return (V==MVT::iAny || V==MVT::fAny || V==MVT::vAny || V==MVT::iPTRAny);
492 /// isByteSized - Return true if the bit size is a multiple of 8.
493 bool isByteSized() const {
494 return (getSizeInBits() & 7) == 0;
497 /// isRound - Return true if the size is a power-of-two number of bytes.
498 bool isRound() const {
499 unsigned BitSize = getSizeInBits();
500 return BitSize >= 8 && !(BitSize & (BitSize - 1));
503 /// bitsEq - Return true if this has the same number of bits as VT.
504 bool bitsEq(EVT VT) const {
505 if (EVT::operator==(VT)) return true;
506 return getSizeInBits() == VT.getSizeInBits();
509 /// bitsGT - Return true if this has more bits than VT.
510 bool bitsGT(EVT VT) const {
511 if (EVT::operator==(VT)) return false;
512 return getSizeInBits() > VT.getSizeInBits();
515 /// bitsGE - Return true if this has no less bits than VT.
516 bool bitsGE(EVT VT) const {
517 if (EVT::operator==(VT)) return true;
518 return getSizeInBits() >= VT.getSizeInBits();
521 /// bitsLT - Return true if this has less bits than VT.
522 bool bitsLT(EVT VT) const {
523 if (EVT::operator==(VT)) return false;
524 return getSizeInBits() < VT.getSizeInBits();
527 /// bitsLE - Return true if this has no more bits than VT.
528 bool bitsLE(EVT VT) const {
529 if (EVT::operator==(VT)) return true;
530 return getSizeInBits() <= VT.getSizeInBits();
534 /// getSimpleVT - Return the SimpleValueType held in the specified
536 MVT getSimpleVT() const {
537 assert(isSimple() && "Expected a SimpleValueType!");
541 /// getScalarType - If this is a vector type, return the element type,
542 /// otherwise return this.
543 EVT getScalarType() const {
544 return isVector() ? getVectorElementType() : *this;
547 /// getVectorElementType - Given a vector type, return the type of
549 EVT getVectorElementType() const {
550 assert(isVector() && "Invalid vector type!");
552 return V.getVectorElementType();
554 return getExtendedVectorElementType();
557 /// getVectorNumElements - Given a vector type, return the number of
558 /// elements it contains.
559 unsigned getVectorNumElements() const {
560 assert(isVector() && "Invalid vector type!");
562 return V.getVectorNumElements();
564 return getExtendedVectorNumElements();
567 /// getSizeInBits - Return the size of the specified value type in bits.
568 unsigned getSizeInBits() const {
570 return V.getSizeInBits();
572 return getExtendedSizeInBits();
575 /// getStoreSize - Return the number of bytes overwritten by a store
576 /// of the specified value type.
577 unsigned getStoreSize() const {
578 return (getSizeInBits() + 7) / 8;
581 /// getStoreSizeInBits - Return the number of bits overwritten by a store
582 /// of the specified value type.
583 unsigned getStoreSizeInBits() const {
584 return getStoreSize() * 8;
587 /// getRoundIntegerType - Rounds the bit-width of the given integer EVT up
588 /// to the nearest power of two (and at least to eight), and returns the
589 /// integer EVT with that number of bits.
590 EVT getRoundIntegerType(LLVMContext &Context) const {
591 assert(isInteger() && !isVector() && "Invalid integer type!");
592 unsigned BitWidth = getSizeInBits();
596 return getIntegerVT(Context, 1 << Log2_32_Ceil(BitWidth));
599 /// getHalfSizedIntegerVT - Finds the smallest simple value type that is
600 /// greater than or equal to half the width of this EVT. If no simple
601 /// value type can be found, an extended integer value type of half the
602 /// size (rounded up) is returned.
603 EVT getHalfSizedIntegerVT(LLVMContext &Context) const {
604 assert(isInteger() && !isVector() && "Invalid integer type!");
605 unsigned EVTSize = getSizeInBits();
606 for (unsigned IntVT = MVT::FIRST_INTEGER_VALUETYPE;
607 IntVT <= MVT::LAST_INTEGER_VALUETYPE;
609 EVT HalfVT = EVT((MVT::SimpleValueType)IntVT);
610 if(HalfVT.getSizeInBits() * 2 >= EVTSize) {
614 return getIntegerVT(Context, (EVTSize + 1) / 2);
617 /// isPow2VectorType - Returns true if the given vector is a power of 2.
618 bool isPow2VectorType() const {
619 unsigned NElts = getVectorNumElements();
620 return !(NElts & (NElts - 1));
623 /// getPow2VectorType - Widens the length of the given vector EVT up to
624 /// the nearest power of 2 and returns that type.
625 EVT getPow2VectorType(LLVMContext &Context) const {
626 if (!isPow2VectorType()) {
627 unsigned NElts = getVectorNumElements();
628 unsigned Pow2NElts = 1 << Log2_32_Ceil(NElts);
629 return EVT::getVectorVT(Context, getVectorElementType(), Pow2NElts);
636 /// getEVTString - This function returns value type as a string,
638 std::string getEVTString() const;
640 /// getTypeForEVT - This method returns an LLVM type corresponding to the
641 /// specified EVT. For integer types, this returns an unsigned type. Note
642 /// that this will abort for types that cannot be represented.
643 const Type *getTypeForEVT(LLVMContext &Context) const;
645 /// getEVT - Return the value type corresponding to the specified type.
646 /// This returns all pointers as iPTR. If HandleUnknown is true, unknown
647 /// types are returned as Other, otherwise they are invalid.
648 static EVT getEVT(const Type *Ty, bool HandleUnknown = false);
650 intptr_t getRawBits() {
654 return (intptr_t)(LLVMTy);
657 /// compareRawBits - A meaningless but well-behaved order, useful for
658 /// constructing containers.
659 struct compareRawBits {
660 bool operator()(EVT L, EVT R) const {
661 if (L.V.SimpleTy == R.V.SimpleTy)
662 return L.LLVMTy < R.LLVMTy;
664 return L.V.SimpleTy < R.V.SimpleTy;
669 // Methods for handling the Extended-type case in functions above.
670 // These are all out-of-line to prevent users of this header file
671 // from having a dependency on Type.h.
672 static EVT getExtendedIntegerVT(LLVMContext &C, unsigned BitWidth);
673 static EVT getExtendedVectorVT(LLVMContext &C, EVT VT,
674 unsigned NumElements);
675 bool isExtendedFloatingPoint() const;
676 bool isExtendedInteger() const;
677 bool isExtendedVector() const;
678 bool isExtended64BitVector() const;
679 bool isExtended128BitVector() const;
680 bool isExtended256BitVector() const;
681 bool isExtended512BitVector() const;
682 EVT getExtendedVectorElementType() const;
683 unsigned getExtendedVectorNumElements() const;
684 unsigned getExtendedSizeInBits() const;
687 } // End llvm namespace