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 /// 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 Flag = 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 // EVT::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; }
139 /// isFloatingPoint - Return true if this is a FP, or a vector FP type.
140 bool isFloatingPoint() const {
141 return ((SimpleTy >= MVT::f32 && SimpleTy <= MVT::ppcf128) ||
142 (SimpleTy >= MVT::v2f32 && SimpleTy <= MVT::v4f64));
145 /// isInteger - Return true if this is an integer, or a vector integer type.
146 bool isInteger() const {
147 return ((SimpleTy >= MVT::FIRST_INTEGER_VALUETYPE &&
148 SimpleTy <= MVT::LAST_INTEGER_VALUETYPE) ||
149 (SimpleTy >= MVT::v2i8 && SimpleTy <= MVT::v8i64));
152 /// isVector - Return true if this is a vector value type.
153 bool isVector() const {
154 return (SimpleTy >= MVT::FIRST_VECTOR_VALUETYPE &&
155 SimpleTy <= MVT::LAST_VECTOR_VALUETYPE);
158 /// isPow2VectorType - Returns true if the given vector is a power of 2.
159 bool isPow2VectorType() const {
160 unsigned NElts = getVectorNumElements();
161 return !(NElts & (NElts - 1));
164 /// getPow2VectorType - Widens the length of the given vector MVT up to
165 /// the nearest power of 2 and returns that type.
166 MVT getPow2VectorType() const {
167 if (isPow2VectorType())
170 unsigned NElts = getVectorNumElements();
171 unsigned Pow2NElts = 1 << Log2_32_Ceil(NElts);
172 return MVT::getVectorVT(getVectorElementType(), Pow2NElts);
175 /// getScalarType - If this is a vector type, return the element type,
176 /// otherwise return this.
177 MVT getScalarType() const {
178 return isVector() ? getVectorElementType() : *this;
181 MVT getVectorElementType() const {
184 return (MVT::SimpleValueType)(MVT::INVALID_SIMPLE_VALUE_TYPE);
189 case v32i8: return i8;
193 case v16i16: return i16;
196 case v8i32: return i32;
200 case v8i64: return i64;
203 case v8f32: return f32;
205 case v4f64: return f64;
209 unsigned getVectorNumElements() const {
213 case v32i8: return 32;
215 case v16i16: return 16;
220 case v8f32: return 8;
226 case v4f64: return 4;
232 case v2f64: return 2;
233 case v1i64: return 1;
237 unsigned getSizeInBits() const {
240 assert(0 && "Value type size is target-dependent. Ask TLI.");
244 assert(0 && "Value type is overloaded.");
246 assert(0 && "getSizeInBits called on extended MVT.");
250 case v2i8: return 16;
254 case v2i16: return 32;
262 case v2f32: return 64;
263 case f80 : return 80;
272 case v2f64: return 128;
278 case v4f64: return 256;
279 case v8i64: return 512;
283 static MVT getFloatingPointVT(unsigned BitWidth) {
286 assert(false && "Bad bit width!");
298 static MVT getIntegerVT(unsigned BitWidth) {
301 return (MVT::SimpleValueType)(MVT::INVALID_SIMPLE_VALUE_TYPE);
317 static MVT getVectorVT(MVT VT, unsigned NumElements) {
318 switch (VT.SimpleTy) {
322 if (NumElements == 2) return MVT::v2i8;
323 if (NumElements == 4) return MVT::v4i8;
324 if (NumElements == 8) return MVT::v8i8;
325 if (NumElements == 16) return MVT::v16i8;
326 if (NumElements == 32) return MVT::v32i8;
329 if (NumElements == 2) return MVT::v2i16;
330 if (NumElements == 4) return MVT::v4i16;
331 if (NumElements == 8) return MVT::v8i16;
332 if (NumElements == 16) return MVT::v16i16;
335 if (NumElements == 2) return MVT::v2i32;
336 if (NumElements == 4) return MVT::v4i32;
337 if (NumElements == 8) return MVT::v8i32;
340 if (NumElements == 1) return MVT::v1i64;
341 if (NumElements == 2) return MVT::v2i64;
342 if (NumElements == 4) return MVT::v4i64;
343 if (NumElements == 8) return MVT::v8i64;
346 if (NumElements == 2) return MVT::v2f32;
347 if (NumElements == 4) return MVT::v4f32;
348 if (NumElements == 8) return MVT::v8f32;
351 if (NumElements == 2) return MVT::v2f64;
352 if (NumElements == 4) return MVT::v4f64;
355 return (MVT::SimpleValueType)(MVT::INVALID_SIMPLE_VALUE_TYPE);
360 /// EVT - Extended Value Type. Capable of holding value types which are not
361 /// native for any processor (such as the i12345 type), as well as the types
362 /// a MVT can represent.
369 EVT() : V((MVT::SimpleValueType)(MVT::INVALID_SIMPLE_VALUE_TYPE)),
371 EVT(MVT::SimpleValueType SVT) : V(SVT), LLVMTy(0) { }
372 EVT(MVT S) : V(S), LLVMTy(0) {}
374 bool operator==(EVT VT) const {
375 return !(*this != VT);
377 bool operator!=(EVT VT) const {
378 if (V.SimpleTy != VT.V.SimpleTy)
380 if (V.SimpleTy == MVT::INVALID_SIMPLE_VALUE_TYPE)
381 return LLVMTy != VT.LLVMTy;
385 /// getFloatingPointVT - Returns the EVT that represents a floating point
386 /// type with the given number of bits. There are two floating point types
387 /// with 128 bits - this returns f128 rather than ppcf128.
388 static EVT getFloatingPointVT(unsigned BitWidth) {
389 return MVT::getFloatingPointVT(BitWidth);
392 /// getIntegerVT - Returns the EVT that represents an integer with the given
394 static EVT getIntegerVT(LLVMContext &Context, unsigned BitWidth) {
395 MVT M = MVT::getIntegerVT(BitWidth);
396 if (M.SimpleTy != MVT::INVALID_SIMPLE_VALUE_TYPE)
398 return getExtendedIntegerVT(Context, BitWidth);
401 /// getVectorVT - Returns the EVT that represents a vector NumElements in
402 /// length, where each element is of type VT.
403 static EVT getVectorVT(LLVMContext &Context, EVT VT, unsigned NumElements) {
404 MVT M = MVT::getVectorVT(VT.V, NumElements);
405 if (M.SimpleTy != MVT::INVALID_SIMPLE_VALUE_TYPE)
407 return getExtendedVectorVT(Context, VT, NumElements);
410 /// getIntVectorWithNumElements - Return any integer vector type that has
411 /// the specified number of elements.
412 static EVT getIntVectorWithNumElements(LLVMContext &C, unsigned NumElts) {
414 default: return getVectorVT(C, MVT::i8, NumElts);
415 case 1: return MVT::v1i64;
416 case 2: return MVT::v2i32;
417 case 4: return MVT::v4i16;
418 case 8: return MVT::v8i8;
419 case 16: return MVT::v16i8;
421 return MVT::INVALID_SIMPLE_VALUE_TYPE;
424 /// isSimple - Test if the given EVT is simple (as opposed to being
426 bool isSimple() const {
427 return V.SimpleTy <= MVT::LastSimpleValueType;
430 /// isExtended - Test if the given EVT is extended (as opposed to
432 bool isExtended() const {
436 /// isFloatingPoint - Return true if this is a FP, or a vector FP type.
437 bool isFloatingPoint() const {
438 return isSimple() ? V.isFloatingPoint() : isExtendedFloatingPoint();
441 /// isInteger - Return true if this is an integer, or a vector integer type.
442 bool isInteger() const {
443 return isSimple() ? V.isInteger() : isExtendedInteger();
446 /// isVector - Return true if this is a vector value type.
447 bool isVector() const {
448 return isSimple() ? V.isVector() : isExtendedVector();
451 /// is64BitVector - Return true if this is a 64-bit vector type.
452 bool is64BitVector() const {
454 return isExtended64BitVector();
456 return (V == MVT::v8i8 || V==MVT::v4i16 || V==MVT::v2i32 ||
457 V == MVT::v1i64 || V==MVT::v2f32);
460 /// is128BitVector - Return true if this is a 128-bit vector type.
461 bool is128BitVector() const {
463 return isExtended128BitVector();
464 return (V==MVT::v16i8 || V==MVT::v8i16 || V==MVT::v4i32 ||
465 V==MVT::v2i64 || V==MVT::v4f32 || V==MVT::v2f64);
468 /// is256BitVector - Return true if this is a 256-bit vector type.
469 inline bool is256BitVector() const {
471 return isExtended256BitVector();
472 return (V == MVT::v8f32 || V == MVT::v4f64 || V == MVT::v32i8 ||
473 V == MVT::v16i16 || V == MVT::v8i32 || V == MVT::v4i64);
476 /// is512BitVector - Return true if this is a 512-bit vector type.
477 inline bool is512BitVector() const {
478 return isSimple() ? (V == MVT::v8i64) : isExtended512BitVector();
481 /// isOverloaded - Return true if this is an overloaded type for TableGen.
482 bool isOverloaded() const {
483 return (V==MVT::iAny || V==MVT::fAny || V==MVT::vAny || V==MVT::iPTRAny);
486 /// isByteSized - Return true if the bit size is a multiple of 8.
487 bool isByteSized() const {
488 return (getSizeInBits() & 7) == 0;
491 /// isRound - Return true if the size is a power-of-two number of bytes.
492 bool isRound() const {
493 unsigned BitSize = getSizeInBits();
494 return BitSize >= 8 && !(BitSize & (BitSize - 1));
497 /// bitsEq - Return true if this has the same number of bits as VT.
498 bool bitsEq(EVT VT) const {
499 if (EVT::operator==(VT)) return true;
500 return getSizeInBits() == VT.getSizeInBits();
503 /// bitsGT - Return true if this has more bits than VT.
504 bool bitsGT(EVT VT) const {
505 if (EVT::operator==(VT)) return false;
506 return getSizeInBits() > VT.getSizeInBits();
509 /// bitsGE - Return true if this has no less bits than VT.
510 bool bitsGE(EVT VT) const {
511 if (EVT::operator==(VT)) return true;
512 return getSizeInBits() >= VT.getSizeInBits();
515 /// bitsLT - Return true if this has less bits than VT.
516 bool bitsLT(EVT VT) const {
517 if (EVT::operator==(VT)) return false;
518 return getSizeInBits() < VT.getSizeInBits();
521 /// bitsLE - Return true if this has no more bits than VT.
522 bool bitsLE(EVT VT) const {
523 if (EVT::operator==(VT)) return true;
524 return getSizeInBits() <= VT.getSizeInBits();
528 /// getSimpleVT - Return the SimpleValueType held in the specified
530 MVT getSimpleVT() const {
531 assert(isSimple() && "Expected a SimpleValueType!");
535 /// getScalarType - If this is a vector type, return the element type,
536 /// otherwise return this.
537 EVT getScalarType() const {
538 return isVector() ? getVectorElementType() : *this;
541 /// getVectorElementType - Given a vector type, return the type of
543 EVT getVectorElementType() const {
544 assert(isVector() && "Invalid vector type!");
546 return V.getVectorElementType();
547 return getExtendedVectorElementType();
550 /// getVectorNumElements - Given a vector type, return the number of
551 /// elements it contains.
552 unsigned getVectorNumElements() const {
553 assert(isVector() && "Invalid vector type!");
555 return V.getVectorNumElements();
556 return getExtendedVectorNumElements();
559 /// getSizeInBits - Return the size of the specified value type in bits.
560 unsigned getSizeInBits() const {
562 return V.getSizeInBits();
563 return getExtendedSizeInBits();
566 /// getStoreSize - Return the number of bytes overwritten by a store
567 /// of the specified value type.
568 unsigned getStoreSize() const {
569 return (getSizeInBits() + 7) / 8;
572 /// getStoreSizeInBits - Return the number of bits overwritten by a store
573 /// of the specified value type.
574 unsigned getStoreSizeInBits() const {
575 return getStoreSize() * 8;
578 /// getRoundIntegerType - Rounds the bit-width of the given integer EVT up
579 /// to the nearest power of two (and at least to eight), and returns the
580 /// integer EVT with that number of bits.
581 EVT getRoundIntegerType(LLVMContext &Context) const {
582 assert(isInteger() && !isVector() && "Invalid integer type!");
583 unsigned BitWidth = getSizeInBits();
586 return getIntegerVT(Context, 1 << Log2_32_Ceil(BitWidth));
589 /// getHalfSizedIntegerVT - Finds the smallest simple value type that is
590 /// greater than or equal to half the width of this EVT. If no simple
591 /// value type can be found, an extended integer value type of half the
592 /// size (rounded up) is returned.
593 EVT getHalfSizedIntegerVT(LLVMContext &Context) const {
594 assert(isInteger() && !isVector() && "Invalid integer type!");
595 unsigned EVTSize = getSizeInBits();
596 for (unsigned IntVT = MVT::FIRST_INTEGER_VALUETYPE;
597 IntVT <= MVT::LAST_INTEGER_VALUETYPE; ++IntVT) {
598 EVT HalfVT = EVT((MVT::SimpleValueType)IntVT);
599 if (HalfVT.getSizeInBits() * 2 >= EVTSize)
602 return getIntegerVT(Context, (EVTSize + 1) / 2);
605 /// isPow2VectorType - Returns true if the given vector is a power of 2.
606 bool isPow2VectorType() const {
607 unsigned NElts = getVectorNumElements();
608 return !(NElts & (NElts - 1));
611 /// getPow2VectorType - Widens the length of the given vector EVT up to
612 /// the nearest power of 2 and returns that type.
613 EVT getPow2VectorType(LLVMContext &Context) const {
614 if (!isPow2VectorType()) {
615 unsigned NElts = getVectorNumElements();
616 unsigned Pow2NElts = 1 << Log2_32_Ceil(NElts);
617 return EVT::getVectorVT(Context, getVectorElementType(), Pow2NElts);
624 /// getEVTString - This function returns value type as a string,
626 std::string getEVTString() const;
628 /// getTypeForEVT - This method returns an LLVM type corresponding to the
629 /// specified EVT. For integer types, this returns an unsigned type. Note
630 /// that this will abort for types that cannot be represented.
631 const Type *getTypeForEVT(LLVMContext &Context) const;
633 /// getEVT - Return the value type corresponding to the specified type.
634 /// This returns all pointers as iPTR. If HandleUnknown is true, unknown
635 /// types are returned as Other, otherwise they are invalid.
636 static EVT getEVT(const Type *Ty, bool HandleUnknown = false);
638 intptr_t getRawBits() {
642 return (intptr_t)(LLVMTy);
645 /// compareRawBits - A meaningless but well-behaved order, useful for
646 /// constructing containers.
647 struct compareRawBits {
648 bool operator()(EVT L, EVT R) const {
649 if (L.V.SimpleTy == R.V.SimpleTy)
650 return L.LLVMTy < R.LLVMTy;
652 return L.V.SimpleTy < R.V.SimpleTy;
657 // Methods for handling the Extended-type case in functions above.
658 // These are all out-of-line to prevent users of this header file
659 // from having a dependency on Type.h.
660 static EVT getExtendedIntegerVT(LLVMContext &C, unsigned BitWidth);
661 static EVT getExtendedVectorVT(LLVMContext &C, EVT VT,
662 unsigned NumElements);
663 bool isExtendedFloatingPoint() const;
664 bool isExtendedInteger() const;
665 bool isExtendedVector() const;
666 bool isExtended64BitVector() const;
667 bool isExtended128BitVector() const;
668 bool isExtended256BitVector() const;
669 bool isExtended512BitVector() const;
670 EVT getExtendedVectorElementType() const;
671 unsigned getExtendedVectorNumElements() const;
672 unsigned getExtendedSizeInBits() const;
675 } // End llvm namespace