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 LAST_VALUETYPE = 36, // This always remains at the end of the list.
88 // This is the current maximum for LAST_VALUETYPE.
89 // MVT::MAX_ALLOWED_VALUETYPE is used for asserts and to size bit vectors
90 // This value must be a multiple of 32.
91 MAX_ALLOWED_VALUETYPE = 64,
93 // Metadata - This is MDNode or MDString.
96 // iPTRAny - An int value the size of the pointer of the current
97 // target to any address space. This must only be used internal to
98 // tblgen. Other than for overloading, we treat iPTRAny the same as iPTR.
101 // vAny - A vector with any length and element size. This is used
102 // for intrinsics that have overloadings based on vector types.
103 // This is only for tblgen's consumption!
106 // fAny - Any floating-point or vector floating-point value. This is used
107 // for intrinsics that have overloadings based on floating-point types.
108 // This is only for tblgen's consumption!
111 // iAny - An integer or vector integer value of any bit width. This is
112 // used for intrinsics that have overloadings based on integer bit widths.
113 // This is only for tblgen's consumption!
116 // iPTR - An int value the size of the pointer of the current
117 // target. This should only be used internal to tblgen!
120 // LastSimpleValueType - The greatest valid SimpleValueType value.
121 LastSimpleValueType = 255,
123 // INVALID_SIMPLE_VALUE_TYPE - Simple value types greater than or equal
124 // to this are considered extended value types.
125 INVALID_SIMPLE_VALUE_TYPE = LastSimpleValueType + 1
128 SimpleValueType SimpleTy;
130 MVT() : SimpleTy((SimpleValueType)(INVALID_SIMPLE_VALUE_TYPE)) {}
131 MVT(SimpleValueType SVT) : SimpleTy(SVT) { }
133 bool operator>(const MVT& S) const { return SimpleTy > S.SimpleTy; }
134 bool operator<(const MVT& S) const { return SimpleTy < S.SimpleTy; }
135 bool operator==(const MVT& S) const { return SimpleTy == S.SimpleTy; }
136 bool operator!=(const MVT& S) const { return SimpleTy != S.SimpleTy; }
137 bool operator>=(const MVT& S) const { return SimpleTy >= S.SimpleTy; }
138 bool operator<=(const MVT& S) const { return SimpleTy <= S.SimpleTy; }
140 /// isFloatingPoint - Return true if this is a FP, or a vector FP type.
141 bool isFloatingPoint() const {
142 return ((SimpleTy >= MVT::f32 && SimpleTy <= MVT::ppcf128) ||
143 (SimpleTy >= MVT::v2f32 && SimpleTy <= MVT::v4f64));
146 /// isInteger - Return true if this is an integer, or a vector integer type.
147 bool isInteger() const {
148 return ((SimpleTy >= MVT::FIRST_INTEGER_VALUETYPE &&
149 SimpleTy <= MVT::LAST_INTEGER_VALUETYPE) ||
150 (SimpleTy >= MVT::v2i8 && SimpleTy <= MVT::v8i64));
153 /// isVector - Return true if this is a vector value type.
154 bool isVector() const {
155 return (SimpleTy >= MVT::FIRST_VECTOR_VALUETYPE &&
156 SimpleTy <= MVT::LAST_VECTOR_VALUETYPE);
159 /// isPow2VectorType - Returns true if the given vector is a power of 2.
160 bool isPow2VectorType() const {
161 unsigned NElts = getVectorNumElements();
162 return !(NElts & (NElts - 1));
165 /// getPow2VectorType - Widens the length of the given vector MVT up to
166 /// the nearest power of 2 and returns that type.
167 MVT getPow2VectorType() const {
168 if (isPow2VectorType())
171 unsigned NElts = getVectorNumElements();
172 unsigned Pow2NElts = 1 << Log2_32_Ceil(NElts);
173 return MVT::getVectorVT(getVectorElementType(), Pow2NElts);
176 /// getScalarType - If this is a vector type, return the element type,
177 /// otherwise return this.
178 MVT getScalarType() const {
179 return isVector() ? getVectorElementType() : *this;
182 MVT getVectorElementType() const {
185 return (MVT::SimpleValueType)(MVT::INVALID_SIMPLE_VALUE_TYPE);
190 case v32i8: return i8;
194 case v16i16: return i16;
197 case v8i32: return i32;
201 case v8i64: return i64;
204 case v8f32: return f32;
206 case v4f64: return f64;
210 unsigned getVectorNumElements() const {
214 case v32i8: return 32;
216 case v16i16: return 16;
221 case v8f32: return 8;
227 case v4f64: return 4;
233 case v2f64: return 2;
234 case v1i64: return 1;
238 unsigned getSizeInBits() const {
241 assert(0 && "Value type size is target-dependent. Ask TLI.");
245 assert(0 && "Value type is overloaded.");
247 assert(0 && "getSizeInBits called on extended MVT.");
251 case v2i8: return 16;
255 case v2i16: return 32;
263 case v2f32: return 64;
264 case f80 : return 80;
273 case v2f64: return 128;
279 case v4f64: return 256;
280 case v8i64: return 512;
284 /// getStoreSize - Return the number of bytes overwritten by a store
285 /// of the specified value type.
286 unsigned getStoreSize() const {
287 return (getSizeInBits() + 7) / 8;
290 /// getStoreSizeInBits - Return the number of bits overwritten by a store
291 /// of the specified value type.
292 unsigned getStoreSizeInBits() const {
293 return getStoreSize() * 8;
296 static MVT getFloatingPointVT(unsigned BitWidth) {
299 assert(false && "Bad bit width!");
311 static MVT getIntegerVT(unsigned BitWidth) {
314 return (MVT::SimpleValueType)(MVT::INVALID_SIMPLE_VALUE_TYPE);
330 static MVT getVectorVT(MVT VT, unsigned NumElements) {
331 switch (VT.SimpleTy) {
335 if (NumElements == 2) return MVT::v2i8;
336 if (NumElements == 4) return MVT::v4i8;
337 if (NumElements == 8) return MVT::v8i8;
338 if (NumElements == 16) return MVT::v16i8;
339 if (NumElements == 32) return MVT::v32i8;
342 if (NumElements == 2) return MVT::v2i16;
343 if (NumElements == 4) return MVT::v4i16;
344 if (NumElements == 8) return MVT::v8i16;
345 if (NumElements == 16) return MVT::v16i16;
348 if (NumElements == 2) return MVT::v2i32;
349 if (NumElements == 4) return MVT::v4i32;
350 if (NumElements == 8) return MVT::v8i32;
353 if (NumElements == 1) return MVT::v1i64;
354 if (NumElements == 2) return MVT::v2i64;
355 if (NumElements == 4) return MVT::v4i64;
356 if (NumElements == 8) return MVT::v8i64;
359 if (NumElements == 2) return MVT::v2f32;
360 if (NumElements == 4) return MVT::v4f32;
361 if (NumElements == 8) return MVT::v8f32;
364 if (NumElements == 2) return MVT::v2f64;
365 if (NumElements == 4) return MVT::v4f64;
368 return (MVT::SimpleValueType)(MVT::INVALID_SIMPLE_VALUE_TYPE);
373 /// EVT - Extended Value Type. Capable of holding value types which are not
374 /// native for any processor (such as the i12345 type), as well as the types
375 /// a MVT can represent.
382 EVT() : V((MVT::SimpleValueType)(MVT::INVALID_SIMPLE_VALUE_TYPE)),
384 EVT(MVT::SimpleValueType SVT) : V(SVT), LLVMTy(0) { }
385 EVT(MVT S) : V(S), LLVMTy(0) {}
387 bool operator==(EVT VT) const {
388 return !(*this != VT);
390 bool operator!=(EVT VT) const {
391 if (V.SimpleTy != VT.V.SimpleTy)
393 if (V.SimpleTy == MVT::INVALID_SIMPLE_VALUE_TYPE)
394 return LLVMTy != VT.LLVMTy;
398 /// getFloatingPointVT - Returns the EVT that represents a floating point
399 /// type with the given number of bits. There are two floating point types
400 /// with 128 bits - this returns f128 rather than ppcf128.
401 static EVT getFloatingPointVT(unsigned BitWidth) {
402 return MVT::getFloatingPointVT(BitWidth);
405 /// getIntegerVT - Returns the EVT that represents an integer with the given
407 static EVT getIntegerVT(LLVMContext &Context, unsigned BitWidth) {
408 MVT M = MVT::getIntegerVT(BitWidth);
409 if (M.SimpleTy != MVT::INVALID_SIMPLE_VALUE_TYPE)
411 return getExtendedIntegerVT(Context, BitWidth);
414 /// getVectorVT - Returns the EVT that represents a vector NumElements in
415 /// length, where each element is of type VT.
416 static EVT getVectorVT(LLVMContext &Context, EVT VT, unsigned NumElements) {
417 MVT M = MVT::getVectorVT(VT.V, NumElements);
418 if (M.SimpleTy != MVT::INVALID_SIMPLE_VALUE_TYPE)
420 return getExtendedVectorVT(Context, VT, NumElements);
423 /// getIntVectorWithNumElements - Return any integer vector type that has
424 /// the specified number of elements.
425 static EVT getIntVectorWithNumElements(LLVMContext &C, unsigned NumElts) {
427 default: return getVectorVT(C, MVT::i8, NumElts);
428 case 1: return MVT::v1i64;
429 case 2: return MVT::v2i32;
430 case 4: return MVT::v4i16;
431 case 8: return MVT::v8i8;
432 case 16: return MVT::v16i8;
434 return MVT::INVALID_SIMPLE_VALUE_TYPE;
437 /// isSimple - Test if the given EVT is simple (as opposed to being
439 bool isSimple() const {
440 return V.SimpleTy <= MVT::LastSimpleValueType;
443 /// isExtended - Test if the given EVT is extended (as opposed to
445 bool isExtended() const {
449 /// isFloatingPoint - Return true if this is a FP, or a vector FP type.
450 bool isFloatingPoint() const {
451 return isSimple() ? V.isFloatingPoint() : isExtendedFloatingPoint();
454 /// isInteger - Return true if this is an integer, or a vector integer type.
455 bool isInteger() const {
456 return isSimple() ? V.isInteger() : isExtendedInteger();
459 /// isVector - Return true if this is a vector value type.
460 bool isVector() const {
461 return isSimple() ? V.isVector() : isExtendedVector();
464 /// is64BitVector - Return true if this is a 64-bit vector type.
465 bool is64BitVector() const {
467 return isExtended64BitVector();
469 return (V == MVT::v8i8 || V==MVT::v4i16 || V==MVT::v2i32 ||
470 V == MVT::v1i64 || V==MVT::v2f32);
473 /// is128BitVector - Return true if this is a 128-bit vector type.
474 bool is128BitVector() const {
476 return isExtended128BitVector();
477 return (V==MVT::v16i8 || V==MVT::v8i16 || V==MVT::v4i32 ||
478 V==MVT::v2i64 || V==MVT::v4f32 || V==MVT::v2f64);
481 /// is256BitVector - Return true if this is a 256-bit vector type.
482 inline bool is256BitVector() const {
484 return isExtended256BitVector();
485 return (V == MVT::v8f32 || V == MVT::v4f64 || V == MVT::v32i8 ||
486 V == MVT::v16i16 || V == MVT::v8i32 || V == MVT::v4i64);
489 /// is512BitVector - Return true if this is a 512-bit vector type.
490 inline bool is512BitVector() const {
491 return isSimple() ? (V == MVT::v8i64) : isExtended512BitVector();
494 /// isOverloaded - Return true if this is an overloaded type for TableGen.
495 bool isOverloaded() const {
496 return (V==MVT::iAny || V==MVT::fAny || V==MVT::vAny || V==MVT::iPTRAny);
499 /// isByteSized - Return true if the bit size is a multiple of 8.
500 bool isByteSized() const {
501 return (getSizeInBits() & 7) == 0;
504 /// isRound - Return true if the size is a power-of-two number of bytes.
505 bool isRound() const {
506 unsigned BitSize = getSizeInBits();
507 return BitSize >= 8 && !(BitSize & (BitSize - 1));
510 /// bitsEq - Return true if this has the same number of bits as VT.
511 bool bitsEq(EVT VT) const {
512 if (EVT::operator==(VT)) return true;
513 return getSizeInBits() == VT.getSizeInBits();
516 /// bitsGT - Return true if this has more bits than VT.
517 bool bitsGT(EVT VT) const {
518 if (EVT::operator==(VT)) return false;
519 return getSizeInBits() > VT.getSizeInBits();
522 /// bitsGE - Return true if this has no less bits than VT.
523 bool bitsGE(EVT VT) const {
524 if (EVT::operator==(VT)) return true;
525 return getSizeInBits() >= VT.getSizeInBits();
528 /// bitsLT - Return true if this has less bits than VT.
529 bool bitsLT(EVT VT) const {
530 if (EVT::operator==(VT)) return false;
531 return getSizeInBits() < VT.getSizeInBits();
534 /// bitsLE - Return true if this has no more bits than VT.
535 bool bitsLE(EVT VT) const {
536 if (EVT::operator==(VT)) return true;
537 return getSizeInBits() <= VT.getSizeInBits();
541 /// getSimpleVT - Return the SimpleValueType held in the specified
543 MVT getSimpleVT() const {
544 assert(isSimple() && "Expected a SimpleValueType!");
548 /// getScalarType - If this is a vector type, return the element type,
549 /// otherwise return this.
550 EVT getScalarType() const {
551 return isVector() ? getVectorElementType() : *this;
554 /// getVectorElementType - Given a vector type, return the type of
556 EVT getVectorElementType() const {
557 assert(isVector() && "Invalid vector type!");
559 return V.getVectorElementType();
560 return getExtendedVectorElementType();
563 /// getVectorNumElements - Given a vector type, return the number of
564 /// elements it contains.
565 unsigned getVectorNumElements() const {
566 assert(isVector() && "Invalid vector type!");
568 return V.getVectorNumElements();
569 return getExtendedVectorNumElements();
572 /// getSizeInBits - Return the size of the specified value type in bits.
573 unsigned getSizeInBits() const {
575 return V.getSizeInBits();
576 return getExtendedSizeInBits();
579 /// getStoreSize - Return the number of bytes overwritten by a store
580 /// of the specified value type.
581 unsigned getStoreSize() const {
582 return (getSizeInBits() + 7) / 8;
585 /// getStoreSizeInBits - Return the number of bits overwritten by a store
586 /// of the specified value type.
587 unsigned getStoreSizeInBits() const {
588 return getStoreSize() * 8;
591 /// getRoundIntegerType - Rounds the bit-width of the given integer EVT up
592 /// to the nearest power of two (and at least to eight), and returns the
593 /// integer EVT with that number of bits.
594 EVT getRoundIntegerType(LLVMContext &Context) const {
595 assert(isInteger() && !isVector() && "Invalid integer type!");
596 unsigned BitWidth = getSizeInBits();
599 return getIntegerVT(Context, 1 << Log2_32_Ceil(BitWidth));
602 /// getHalfSizedIntegerVT - Finds the smallest simple value type that is
603 /// greater than or equal to half the width of this EVT. If no simple
604 /// value type can be found, an extended integer value type of half the
605 /// size (rounded up) is returned.
606 EVT getHalfSizedIntegerVT(LLVMContext &Context) const {
607 assert(isInteger() && !isVector() && "Invalid integer type!");
608 unsigned EVTSize = getSizeInBits();
609 for (unsigned IntVT = MVT::FIRST_INTEGER_VALUETYPE;
610 IntVT <= MVT::LAST_INTEGER_VALUETYPE; ++IntVT) {
611 EVT HalfVT = EVT((MVT::SimpleValueType)IntVT);
612 if (HalfVT.getSizeInBits() * 2 >= EVTSize)
615 return getIntegerVT(Context, (EVTSize + 1) / 2);
618 /// isPow2VectorType - Returns true if the given vector is a power of 2.
619 bool isPow2VectorType() const {
620 unsigned NElts = getVectorNumElements();
621 return !(NElts & (NElts - 1));
624 /// getPow2VectorType - Widens the length of the given vector EVT up to
625 /// the nearest power of 2 and returns that type.
626 EVT getPow2VectorType(LLVMContext &Context) const {
627 if (!isPow2VectorType()) {
628 unsigned NElts = getVectorNumElements();
629 unsigned Pow2NElts = 1 << Log2_32_Ceil(NElts);
630 return EVT::getVectorVT(Context, getVectorElementType(), Pow2NElts);
637 /// getEVTString - This function returns value type as a string,
639 std::string getEVTString() const;
641 /// getTypeForEVT - This method returns an LLVM type corresponding to the
642 /// specified EVT. For integer types, this returns an unsigned type. Note
643 /// that this will abort for types that cannot be represented.
644 const Type *getTypeForEVT(LLVMContext &Context) const;
646 /// getEVT - Return the value type corresponding to the specified type.
647 /// This returns all pointers as iPTR. If HandleUnknown is true, unknown
648 /// types are returned as Other, otherwise they are invalid.
649 static EVT getEVT(const Type *Ty, bool HandleUnknown = false);
651 intptr_t getRawBits() {
655 return (intptr_t)(LLVMTy);
658 /// compareRawBits - A meaningless but well-behaved order, useful for
659 /// constructing containers.
660 struct compareRawBits {
661 bool operator()(EVT L, EVT R) const {
662 if (L.V.SimpleTy == R.V.SimpleTy)
663 return L.LLVMTy < R.LLVMTy;
665 return L.V.SimpleTy < R.V.SimpleTy;
670 // Methods for handling the Extended-type case in functions above.
671 // These are all out-of-line to prevent users of this header file
672 // from having a dependency on Type.h.
673 static EVT getExtendedIntegerVT(LLVMContext &C, unsigned BitWidth);
674 static EVT getExtendedVectorVT(LLVMContext &C, EVT VT,
675 unsigned NumElements);
676 bool isExtendedFloatingPoint() const;
677 bool isExtendedInteger() const;
678 bool isExtendedVector() const;
679 bool isExtended64BitVector() const;
680 bool isExtended128BitVector() const;
681 bool isExtended256BitVector() const;
682 bool isExtended512BitVector() const;
683 EVT getExtendedVectorElementType() const;
684 unsigned getExtendedVectorNumElements() const;
685 unsigned getExtendedSizeInBits() const;
688 } // End llvm namespace