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 = 35, // 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())
165 unsigned NElts = getVectorNumElements();
166 unsigned Pow2NElts = 1 << Log2_32_Ceil(NElts);
167 return MVT::getVectorVT(getVectorElementType(), Pow2NElts);
170 /// getScalarType - If this is a vector type, return the element type,
171 /// otherwise return this.
172 MVT getScalarType() const {
173 return isVector() ? getVectorElementType() : *this;
176 MVT getVectorElementType() const {
179 return (MVT::SimpleValueType)(MVT::INVALID_SIMPLE_VALUE_TYPE);
184 case v32i8: return i8;
188 case v16i16: return i16;
191 case v8i32: return i32;
195 case v8i64: return i64;
198 case v8f32: return f32;
200 case v4f64: return f64;
204 unsigned getVectorNumElements() const {
208 case v32i8: return 32;
210 case v16i16: return 16;
215 case v8f32: return 8;
221 case v4f64: return 4;
227 case v2f64: return 2;
228 case v1i64: return 1;
232 unsigned getSizeInBits() const {
235 assert(0 && "Value type size is target-dependent. Ask TLI.");
239 assert(0 && "Value type is overloaded.");
241 assert(0 && "getSizeInBits called on extended MVT.");
245 case v2i8: return 16;
249 case v2i16: return 32;
256 case v2f32: return 64;
257 case f80 : return 80;
266 case v2f64: return 128;
272 case v4f64: return 256;
273 case v8i64: return 512;
277 static MVT getFloatingPointVT(unsigned BitWidth) {
280 assert(false && "Bad bit width!");
292 static MVT getIntegerVT(unsigned BitWidth) {
295 return (MVT::SimpleValueType)(MVT::INVALID_SIMPLE_VALUE_TYPE);
311 static MVT getVectorVT(MVT VT, unsigned NumElements) {
312 switch (VT.SimpleTy) {
316 if (NumElements == 2) return MVT::v2i8;
317 if (NumElements == 4) return MVT::v4i8;
318 if (NumElements == 8) return MVT::v8i8;
319 if (NumElements == 16) return MVT::v16i8;
320 if (NumElements == 32) return MVT::v32i8;
323 if (NumElements == 2) return MVT::v2i16;
324 if (NumElements == 4) return MVT::v4i16;
325 if (NumElements == 8) return MVT::v8i16;
326 if (NumElements == 16) return MVT::v16i16;
329 if (NumElements == 2) return MVT::v2i32;
330 if (NumElements == 4) return MVT::v4i32;
331 if (NumElements == 8) return MVT::v8i32;
334 if (NumElements == 1) return MVT::v1i64;
335 if (NumElements == 2) return MVT::v2i64;
336 if (NumElements == 4) return MVT::v4i64;
337 if (NumElements == 8) return MVT::v8i64;
340 if (NumElements == 2) return MVT::v2f32;
341 if (NumElements == 4) return MVT::v4f32;
342 if (NumElements == 8) return MVT::v8f32;
345 if (NumElements == 2) return MVT::v2f64;
346 if (NumElements == 4) return MVT::v4f64;
349 return (MVT::SimpleValueType)(MVT::INVALID_SIMPLE_VALUE_TYPE);
353 struct EVT { // EVT = Extended Value Type
359 EVT() : V((MVT::SimpleValueType)(MVT::INVALID_SIMPLE_VALUE_TYPE)),
361 EVT(MVT::SimpleValueType SVT) : V(SVT), LLVMTy(0) { }
362 EVT(MVT S) : V(S), LLVMTy(0) {}
364 bool operator==(EVT VT) const {
365 return !(*this != VT);
367 bool operator!=(EVT VT) const {
368 if (V.SimpleTy != VT.V.SimpleTy)
370 if (V.SimpleTy == MVT::INVALID_SIMPLE_VALUE_TYPE)
371 return LLVMTy != VT.LLVMTy;
375 /// getFloatingPointVT - Returns the EVT that represents a floating point
376 /// type with the given number of bits. There are two floating point types
377 /// with 128 bits - this returns f128 rather than ppcf128.
378 static EVT getFloatingPointVT(unsigned BitWidth) {
379 return MVT::getFloatingPointVT(BitWidth);
382 /// getIntegerVT - Returns the EVT that represents an integer with the given
384 static EVT getIntegerVT(LLVMContext &Context, unsigned BitWidth) {
385 MVT M = MVT::getIntegerVT(BitWidth);
386 if (M.SimpleTy != MVT::INVALID_SIMPLE_VALUE_TYPE)
388 return getExtendedIntegerVT(Context, BitWidth);
391 /// getVectorVT - Returns the EVT that represents a vector NumElements in
392 /// length, where each element is of type VT.
393 static EVT getVectorVT(LLVMContext &Context, EVT VT, unsigned NumElements) {
394 MVT M = MVT::getVectorVT(VT.V, NumElements);
395 if (M.SimpleTy != MVT::INVALID_SIMPLE_VALUE_TYPE)
397 return getExtendedVectorVT(Context, VT, NumElements);
400 /// getIntVectorWithNumElements - Return any integer vector type that has
401 /// the specified number of elements.
402 static EVT getIntVectorWithNumElements(LLVMContext &C, unsigned NumElts) {
404 default: return getVectorVT(C, MVT::i8, NumElts);
405 case 1: return MVT::v1i64;
406 case 2: return MVT::v2i32;
407 case 4: return MVT::v4i16;
408 case 8: return MVT::v8i8;
409 case 16: return MVT::v16i8;
411 return MVT::INVALID_SIMPLE_VALUE_TYPE;
414 /// isSimple - Test if the given EVT is simple (as opposed to being
416 bool isSimple() const {
417 return V.SimpleTy <= MVT::LastSimpleValueType;
420 /// isExtended - Test if the given EVT is extended (as opposed to
422 bool isExtended() const {
426 /// isFloatingPoint - Return true if this is a FP, or a vector FP type.
427 bool isFloatingPoint() const {
428 return isSimple() ? V.isFloatingPoint() : isExtendedFloatingPoint();
431 /// isInteger - Return true if this is an integer, or a vector integer type.
432 bool isInteger() const {
433 return isSimple() ? V.isInteger() : isExtendedInteger();
436 /// isVector - Return true if this is a vector value type.
437 bool isVector() const {
438 return isSimple() ? V.isVector() : isExtendedVector();
441 /// is64BitVector - Return true if this is a 64-bit vector type.
442 bool is64BitVector() const {
444 return isExtended64BitVector();
446 return (V == MVT::v8i8 || V==MVT::v4i16 || V==MVT::v2i32 ||
447 V == MVT::v1i64 || V==MVT::v2f32);
450 /// is128BitVector - Return true if this is a 128-bit vector type.
451 bool is128BitVector() const {
453 return isExtended128BitVector();
454 return (V==MVT::v16i8 || V==MVT::v8i16 || V==MVT::v4i32 ||
455 V==MVT::v2i64 || V==MVT::v4f32 || V==MVT::v2f64);
458 /// is256BitVector - Return true if this is a 256-bit vector type.
459 inline bool is256BitVector() const {
461 return isExtended256BitVector();
462 return (V == MVT::v8f32 || V == MVT::v4f64 || V == MVT::v32i8 ||
463 V == MVT::v16i16 || V == MVT::v8i32 || V == MVT::v4i64);
466 /// is512BitVector - Return true if this is a 512-bit vector type.
467 inline bool is512BitVector() const {
468 return isSimple() ? (V == MVT::v8i64) : isExtended512BitVector();
471 /// isOverloaded - Return true if this is an overloaded type for TableGen.
472 bool isOverloaded() const {
473 return (V==MVT::iAny || V==MVT::fAny || V==MVT::vAny || V==MVT::iPTRAny);
476 /// isByteSized - Return true if the bit size is a multiple of 8.
477 bool isByteSized() const {
478 return (getSizeInBits() & 7) == 0;
481 /// isRound - Return true if the size is a power-of-two number of bytes.
482 bool isRound() const {
483 unsigned BitSize = getSizeInBits();
484 return BitSize >= 8 && !(BitSize & (BitSize - 1));
487 /// bitsEq - Return true if this has the same number of bits as VT.
488 bool bitsEq(EVT VT) const {
489 if (EVT::operator==(VT)) return true;
490 return getSizeInBits() == VT.getSizeInBits();
493 /// bitsGT - Return true if this has more bits than VT.
494 bool bitsGT(EVT VT) const {
495 if (EVT::operator==(VT)) return false;
496 return getSizeInBits() > VT.getSizeInBits();
499 /// bitsGE - Return true if this has no less bits than VT.
500 bool bitsGE(EVT VT) const {
501 if (EVT::operator==(VT)) return true;
502 return getSizeInBits() >= VT.getSizeInBits();
505 /// bitsLT - Return true if this has less bits than VT.
506 bool bitsLT(EVT VT) const {
507 if (EVT::operator==(VT)) return false;
508 return getSizeInBits() < VT.getSizeInBits();
511 /// bitsLE - Return true if this has no more bits than VT.
512 bool bitsLE(EVT VT) const {
513 if (EVT::operator==(VT)) return true;
514 return getSizeInBits() <= VT.getSizeInBits();
518 /// getSimpleVT - Return the SimpleValueType held in the specified
520 MVT getSimpleVT() const {
521 assert(isSimple() && "Expected a SimpleValueType!");
525 /// getScalarType - If this is a vector type, return the element type,
526 /// otherwise return this.
527 EVT getScalarType() const {
528 return isVector() ? getVectorElementType() : *this;
531 /// getVectorElementType - Given a vector type, return the type of
533 EVT getVectorElementType() const {
534 assert(isVector() && "Invalid vector type!");
536 return V.getVectorElementType();
537 return getExtendedVectorElementType();
540 /// getVectorNumElements - Given a vector type, return the number of
541 /// elements it contains.
542 unsigned getVectorNumElements() const {
543 assert(isVector() && "Invalid vector type!");
545 return V.getVectorNumElements();
546 return getExtendedVectorNumElements();
549 /// getSizeInBits - Return the size of the specified value type in bits.
550 unsigned getSizeInBits() const {
552 return V.getSizeInBits();
553 return getExtendedSizeInBits();
556 /// getStoreSize - Return the number of bytes overwritten by a store
557 /// of the specified value type.
558 unsigned getStoreSize() const {
559 return (getSizeInBits() + 7) / 8;
562 /// getStoreSizeInBits - Return the number of bits overwritten by a store
563 /// of the specified value type.
564 unsigned getStoreSizeInBits() const {
565 return getStoreSize() * 8;
568 /// getRoundIntegerType - Rounds the bit-width of the given integer EVT up
569 /// to the nearest power of two (and at least to eight), and returns the
570 /// integer EVT with that number of bits.
571 EVT getRoundIntegerType(LLVMContext &Context) const {
572 assert(isInteger() && !isVector() && "Invalid integer type!");
573 unsigned BitWidth = getSizeInBits();
576 return getIntegerVT(Context, 1 << Log2_32_Ceil(BitWidth));
579 /// getHalfSizedIntegerVT - Finds the smallest simple value type that is
580 /// greater than or equal to half the width of this EVT. If no simple
581 /// value type can be found, an extended integer value type of half the
582 /// size (rounded up) is returned.
583 EVT getHalfSizedIntegerVT(LLVMContext &Context) const {
584 assert(isInteger() && !isVector() && "Invalid integer type!");
585 unsigned EVTSize = getSizeInBits();
586 for (unsigned IntVT = MVT::FIRST_INTEGER_VALUETYPE;
587 IntVT <= MVT::LAST_INTEGER_VALUETYPE; ++IntVT) {
588 EVT HalfVT = EVT((MVT::SimpleValueType)IntVT);
589 if (HalfVT.getSizeInBits() * 2 >= EVTSize)
592 return getIntegerVT(Context, (EVTSize + 1) / 2);
595 /// isPow2VectorType - Returns true if the given vector is a power of 2.
596 bool isPow2VectorType() const {
597 unsigned NElts = getVectorNumElements();
598 return !(NElts & (NElts - 1));
601 /// getPow2VectorType - Widens the length of the given vector EVT up to
602 /// the nearest power of 2 and returns that type.
603 EVT getPow2VectorType(LLVMContext &Context) const {
604 if (!isPow2VectorType()) {
605 unsigned NElts = getVectorNumElements();
606 unsigned Pow2NElts = 1 << Log2_32_Ceil(NElts);
607 return EVT::getVectorVT(Context, getVectorElementType(), Pow2NElts);
614 /// getEVTString - This function returns value type as a string,
616 std::string getEVTString() const;
618 /// getTypeForEVT - This method returns an LLVM type corresponding to the
619 /// specified EVT. For integer types, this returns an unsigned type. Note
620 /// that this will abort for types that cannot be represented.
621 const Type *getTypeForEVT(LLVMContext &Context) const;
623 /// getEVT - Return the value type corresponding to the specified type.
624 /// This returns all pointers as iPTR. If HandleUnknown is true, unknown
625 /// types are returned as Other, otherwise they are invalid.
626 static EVT getEVT(const Type *Ty, bool HandleUnknown = false);
628 intptr_t getRawBits() {
632 return (intptr_t)(LLVMTy);
635 /// compareRawBits - A meaningless but well-behaved order, useful for
636 /// constructing containers.
637 struct compareRawBits {
638 bool operator()(EVT L, EVT R) const {
639 if (L.V.SimpleTy == R.V.SimpleTy)
640 return L.LLVMTy < R.LLVMTy;
642 return L.V.SimpleTy < R.V.SimpleTy;
647 // Methods for handling the Extended-type case in functions above.
648 // These are all out-of-line to prevent users of this header file
649 // from having a dependency on Type.h.
650 static EVT getExtendedIntegerVT(LLVMContext &C, unsigned BitWidth);
651 static EVT getExtendedVectorVT(LLVMContext &C, EVT VT,
652 unsigned NumElements);
653 bool isExtendedFloatingPoint() const;
654 bool isExtendedInteger() const;
655 bool isExtendedVector() const;
656 bool isExtended64BitVector() const;
657 bool isExtended128BitVector() const;
658 bool isExtended256BitVector() const;
659 bool isExtended512BitVector() const;
660 EVT getExtendedVectorElementType() const;
661 unsigned getExtendedVectorNumElements() const;
662 unsigned getExtendedSizeInBits() const;
665 } // End llvm namespace