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 x86mmx = 33, // This is an X86 MMX value
79 Flag = 34, // This glues nodes together during pre-RA sched
81 isVoid = 35, // This has no value
83 LAST_VALUETYPE = 36, // This always remains at the end of the list.
85 // This is the current maximum for LAST_VALUETYPE.
86 // EVT::MAX_ALLOWED_VALUETYPE is used for asserts and to size bit vectors
87 // This value must be a multiple of 32.
88 MAX_ALLOWED_VALUETYPE = 64,
90 // Metadata - This is MDNode or MDString.
93 // iPTRAny - An int value the size of the pointer of the current
94 // target to any address space. This must only be used internal to
95 // tblgen. Other than for overloading, we treat iPTRAny the same as iPTR.
98 // vAny - A vector with any length and element size. This is used
99 // for intrinsics that have overloadings based on vector types.
100 // This is only for tblgen's consumption!
103 // fAny - Any floating-point or vector floating-point value. This is used
104 // for intrinsics that have overloadings based on floating-point types.
105 // This is only for tblgen's consumption!
108 // iAny - An integer or vector integer value of any bit width. This is
109 // used for intrinsics that have overloadings based on integer bit widths.
110 // This is only for tblgen's consumption!
113 // iPTR - An int value the size of the pointer of the current
114 // target. This should only be used internal to tblgen!
117 // LastSimpleValueType - The greatest valid SimpleValueType value.
118 LastSimpleValueType = 255,
120 // INVALID_SIMPLE_VALUE_TYPE - Simple value types greater than or equal
121 // to this are considered extended value types.
122 INVALID_SIMPLE_VALUE_TYPE = LastSimpleValueType + 1
125 SimpleValueType SimpleTy;
127 MVT() : SimpleTy((SimpleValueType)(INVALID_SIMPLE_VALUE_TYPE)) {}
128 MVT(SimpleValueType SVT) : SimpleTy(SVT) { }
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; }
133 bool operator>=(const MVT& S) const { return SimpleTy >= S.SimpleTy; }
134 bool operator<=(const MVT& S) const { return SimpleTy <= S.SimpleTy; }
136 /// isFloatingPoint - Return true if this is a FP, or a vector FP type.
137 bool isFloatingPoint() const {
138 return ((SimpleTy >= MVT::f32 && SimpleTy <= MVT::ppcf128) ||
139 (SimpleTy >= MVT::v2f32 && SimpleTy <= MVT::v4f64));
142 /// isInteger - Return true if this is an integer, or a vector integer type.
143 bool isInteger() const {
144 return ((SimpleTy >= MVT::FIRST_INTEGER_VALUETYPE &&
145 SimpleTy <= MVT::LAST_INTEGER_VALUETYPE) ||
146 (SimpleTy >= MVT::v2i8 && SimpleTy <= MVT::v8i64));
149 /// isVector - Return true if this is a vector value type.
150 bool isVector() const {
151 return (SimpleTy >= MVT::FIRST_VECTOR_VALUETYPE &&
152 SimpleTy <= MVT::LAST_VECTOR_VALUETYPE);
155 /// isPow2VectorType - Returns true if the given vector is a power of 2.
156 bool isPow2VectorType() const {
157 unsigned NElts = getVectorNumElements();
158 return !(NElts & (NElts - 1));
161 /// getPow2VectorType - Widens the length of the given vector EVT up to
162 /// the nearest power of 2 and returns that type.
163 MVT getPow2VectorType() const {
164 if (isPow2VectorType())
167 unsigned NElts = getVectorNumElements();
168 unsigned Pow2NElts = 1 << Log2_32_Ceil(NElts);
169 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;
259 case v2f32: return 64;
260 case f80 : return 80;
269 case v2f64: return 128;
275 case v4f64: return 256;
276 case v8i64: return 512;
280 static MVT getFloatingPointVT(unsigned BitWidth) {
283 assert(false && "Bad bit width!");
295 static MVT getIntegerVT(unsigned BitWidth) {
298 return (MVT::SimpleValueType)(MVT::INVALID_SIMPLE_VALUE_TYPE);
314 static MVT getVectorVT(MVT VT, unsigned NumElements) {
315 switch (VT.SimpleTy) {
319 if (NumElements == 2) return MVT::v2i8;
320 if (NumElements == 4) return MVT::v4i8;
321 if (NumElements == 8) return MVT::v8i8;
322 if (NumElements == 16) return MVT::v16i8;
323 if (NumElements == 32) return MVT::v32i8;
326 if (NumElements == 2) return MVT::v2i16;
327 if (NumElements == 4) return MVT::v4i16;
328 if (NumElements == 8) return MVT::v8i16;
329 if (NumElements == 16) return MVT::v16i16;
332 if (NumElements == 2) return MVT::v2i32;
333 if (NumElements == 4) return MVT::v4i32;
334 if (NumElements == 8) return MVT::v8i32;
337 if (NumElements == 1) return MVT::v1i64;
338 if (NumElements == 2) return MVT::v2i64;
339 if (NumElements == 4) return MVT::v4i64;
340 if (NumElements == 8) return MVT::v8i64;
343 if (NumElements == 2) return MVT::v2f32;
344 if (NumElements == 4) return MVT::v4f32;
345 if (NumElements == 8) return MVT::v8f32;
348 if (NumElements == 2) return MVT::v2f64;
349 if (NumElements == 4) return MVT::v4f64;
352 return (MVT::SimpleValueType)(MVT::INVALID_SIMPLE_VALUE_TYPE);
356 struct EVT { // EVT = Extended Value Type
362 EVT() : V((MVT::SimpleValueType)(MVT::INVALID_SIMPLE_VALUE_TYPE)),
364 EVT(MVT::SimpleValueType SVT) : V(SVT), LLVMTy(0) { }
365 EVT(MVT S) : V(S), LLVMTy(0) {}
367 bool operator==(EVT VT) const {
368 return !(*this != VT);
370 bool operator!=(EVT VT) const {
371 if (V.SimpleTy != VT.V.SimpleTy)
373 if (V.SimpleTy == MVT::INVALID_SIMPLE_VALUE_TYPE)
374 return LLVMTy != VT.LLVMTy;
378 /// getFloatingPointVT - Returns the EVT that represents a floating point
379 /// type with the given number of bits. There are two floating point types
380 /// with 128 bits - this returns f128 rather than ppcf128.
381 static EVT getFloatingPointVT(unsigned BitWidth) {
382 return MVT::getFloatingPointVT(BitWidth);
385 /// getIntegerVT - Returns the EVT that represents an integer with the given
387 static EVT getIntegerVT(LLVMContext &Context, unsigned BitWidth) {
388 MVT M = MVT::getIntegerVT(BitWidth);
389 if (M.SimpleTy != MVT::INVALID_SIMPLE_VALUE_TYPE)
391 return getExtendedIntegerVT(Context, BitWidth);
394 /// getVectorVT - Returns the EVT that represents a vector NumElements in
395 /// length, where each element is of type VT.
396 static EVT getVectorVT(LLVMContext &Context, EVT VT, unsigned NumElements) {
397 MVT M = MVT::getVectorVT(VT.V, NumElements);
398 if (M.SimpleTy != MVT::INVALID_SIMPLE_VALUE_TYPE)
400 return getExtendedVectorVT(Context, VT, NumElements);
403 /// getIntVectorWithNumElements - Return any integer vector type that has
404 /// the specified number of elements.
405 static EVT getIntVectorWithNumElements(LLVMContext &C, unsigned NumElts) {
407 default: return getVectorVT(C, MVT::i8, NumElts);
408 case 1: return MVT::v1i64;
409 case 2: return MVT::v2i32;
410 case 4: return MVT::v4i16;
411 case 8: return MVT::v8i8;
412 case 16: return MVT::v16i8;
414 return MVT::INVALID_SIMPLE_VALUE_TYPE;
417 /// isSimple - Test if the given EVT is simple (as opposed to being
419 bool isSimple() const {
420 return V.SimpleTy <= MVT::LastSimpleValueType;
423 /// isExtended - Test if the given EVT is extended (as opposed to
425 bool isExtended() const {
429 /// isFloatingPoint - Return true if this is a FP, or a vector FP type.
430 bool isFloatingPoint() const {
431 return isSimple() ? V.isFloatingPoint() : isExtendedFloatingPoint();
434 /// isInteger - Return true if this is an integer, or a vector integer type.
435 bool isInteger() const {
436 return isSimple() ? V.isInteger() : isExtendedInteger();
439 /// isVector - Return true if this is a vector value type.
440 bool isVector() const {
441 return isSimple() ? V.isVector() : isExtendedVector();
444 /// is64BitVector - Return true if this is a 64-bit vector type.
445 bool is64BitVector() const {
447 return isExtended64BitVector();
449 return (V == MVT::v8i8 || V==MVT::v4i16 || V==MVT::v2i32 ||
450 V == MVT::v1i64 || V==MVT::v2f32);
453 /// is128BitVector - Return true if this is a 128-bit vector type.
454 bool is128BitVector() const {
456 return isExtended128BitVector();
457 return (V==MVT::v16i8 || V==MVT::v8i16 || V==MVT::v4i32 ||
458 V==MVT::v2i64 || V==MVT::v4f32 || V==MVT::v2f64);
461 /// is256BitVector - Return true if this is a 256-bit vector type.
462 inline bool is256BitVector() const {
464 return isExtended256BitVector();
465 return (V == MVT::v8f32 || V == MVT::v4f64 || V == MVT::v32i8 ||
466 V == MVT::v16i16 || V == MVT::v8i32 || V == MVT::v4i64);
469 /// is512BitVector - Return true if this is a 512-bit vector type.
470 inline bool is512BitVector() const {
471 return isSimple() ? (V == MVT::v8i64) : isExtended512BitVector();
474 /// isOverloaded - Return true if this is an overloaded type for TableGen.
475 bool isOverloaded() const {
476 return (V==MVT::iAny || V==MVT::fAny || V==MVT::vAny || V==MVT::iPTRAny);
479 /// isByteSized - Return true if the bit size is a multiple of 8.
480 bool isByteSized() const {
481 return (getSizeInBits() & 7) == 0;
484 /// isRound - Return true if the size is a power-of-two number of bytes.
485 bool isRound() const {
486 unsigned BitSize = getSizeInBits();
487 return BitSize >= 8 && !(BitSize & (BitSize - 1));
490 /// bitsEq - Return true if this has the same number of bits as VT.
491 bool bitsEq(EVT VT) const {
492 if (EVT::operator==(VT)) return true;
493 return getSizeInBits() == VT.getSizeInBits();
496 /// bitsGT - Return true if this has more bits than VT.
497 bool bitsGT(EVT VT) const {
498 if (EVT::operator==(VT)) return false;
499 return getSizeInBits() > VT.getSizeInBits();
502 /// bitsGE - Return true if this has no less bits than VT.
503 bool bitsGE(EVT VT) const {
504 if (EVT::operator==(VT)) return true;
505 return getSizeInBits() >= VT.getSizeInBits();
508 /// bitsLT - Return true if this has less bits than VT.
509 bool bitsLT(EVT VT) const {
510 if (EVT::operator==(VT)) return false;
511 return getSizeInBits() < VT.getSizeInBits();
514 /// bitsLE - Return true if this has no more bits than VT.
515 bool bitsLE(EVT VT) const {
516 if (EVT::operator==(VT)) return true;
517 return getSizeInBits() <= VT.getSizeInBits();
521 /// getSimpleVT - Return the SimpleValueType held in the specified
523 MVT getSimpleVT() const {
524 assert(isSimple() && "Expected a SimpleValueType!");
528 /// getScalarType - If this is a vector type, return the element type,
529 /// otherwise return this.
530 EVT getScalarType() const {
531 return isVector() ? getVectorElementType() : *this;
534 /// getVectorElementType - Given a vector type, return the type of
536 EVT getVectorElementType() const {
537 assert(isVector() && "Invalid vector type!");
539 return V.getVectorElementType();
540 return getExtendedVectorElementType();
543 /// getVectorNumElements - Given a vector type, return the number of
544 /// elements it contains.
545 unsigned getVectorNumElements() const {
546 assert(isVector() && "Invalid vector type!");
548 return V.getVectorNumElements();
549 return getExtendedVectorNumElements();
552 /// getSizeInBits - Return the size of the specified value type in bits.
553 unsigned getSizeInBits() const {
555 return V.getSizeInBits();
556 return getExtendedSizeInBits();
559 /// getStoreSize - Return the number of bytes overwritten by a store
560 /// of the specified value type.
561 unsigned getStoreSize() const {
562 return (getSizeInBits() + 7) / 8;
565 /// getStoreSizeInBits - Return the number of bits overwritten by a store
566 /// of the specified value type.
567 unsigned getStoreSizeInBits() const {
568 return getStoreSize() * 8;
571 /// getRoundIntegerType - Rounds the bit-width of the given integer EVT up
572 /// to the nearest power of two (and at least to eight), and returns the
573 /// integer EVT with that number of bits.
574 EVT getRoundIntegerType(LLVMContext &Context) const {
575 assert(isInteger() && !isVector() && "Invalid integer type!");
576 unsigned BitWidth = getSizeInBits();
579 return getIntegerVT(Context, 1 << Log2_32_Ceil(BitWidth));
582 /// getHalfSizedIntegerVT - Finds the smallest simple value type that is
583 /// greater than or equal to half the width of this EVT. If no simple
584 /// value type can be found, an extended integer value type of half the
585 /// size (rounded up) is returned.
586 EVT getHalfSizedIntegerVT(LLVMContext &Context) const {
587 assert(isInteger() && !isVector() && "Invalid integer type!");
588 unsigned EVTSize = getSizeInBits();
589 for (unsigned IntVT = MVT::FIRST_INTEGER_VALUETYPE;
590 IntVT <= MVT::LAST_INTEGER_VALUETYPE; ++IntVT) {
591 EVT HalfVT = EVT((MVT::SimpleValueType)IntVT);
592 if (HalfVT.getSizeInBits() * 2 >= EVTSize)
595 return getIntegerVT(Context, (EVTSize + 1) / 2);
598 /// isPow2VectorType - Returns true if the given vector is a power of 2.
599 bool isPow2VectorType() const {
600 unsigned NElts = getVectorNumElements();
601 return !(NElts & (NElts - 1));
604 /// getPow2VectorType - Widens the length of the given vector EVT up to
605 /// the nearest power of 2 and returns that type.
606 EVT getPow2VectorType(LLVMContext &Context) const {
607 if (!isPow2VectorType()) {
608 unsigned NElts = getVectorNumElements();
609 unsigned Pow2NElts = 1 << Log2_32_Ceil(NElts);
610 return EVT::getVectorVT(Context, getVectorElementType(), Pow2NElts);
617 /// getEVTString - This function returns value type as a string,
619 std::string getEVTString() const;
621 /// getTypeForEVT - This method returns an LLVM type corresponding to the
622 /// specified EVT. For integer types, this returns an unsigned type. Note
623 /// that this will abort for types that cannot be represented.
624 const Type *getTypeForEVT(LLVMContext &Context) const;
626 /// getEVT - Return the value type corresponding to the specified type.
627 /// This returns all pointers as iPTR. If HandleUnknown is true, unknown
628 /// types are returned as Other, otherwise they are invalid.
629 static EVT getEVT(const Type *Ty, bool HandleUnknown = false);
631 intptr_t getRawBits() {
635 return (intptr_t)(LLVMTy);
638 /// compareRawBits - A meaningless but well-behaved order, useful for
639 /// constructing containers.
640 struct compareRawBits {
641 bool operator()(EVT L, EVT R) const {
642 if (L.V.SimpleTy == R.V.SimpleTy)
643 return L.LLVMTy < R.LLVMTy;
645 return L.V.SimpleTy < R.V.SimpleTy;
650 // Methods for handling the Extended-type case in functions above.
651 // These are all out-of-line to prevent users of this header file
652 // from having a dependency on Type.h.
653 static EVT getExtendedIntegerVT(LLVMContext &C, unsigned BitWidth);
654 static EVT getExtendedVectorVT(LLVMContext &C, EVT VT,
655 unsigned NumElements);
656 bool isExtendedFloatingPoint() const;
657 bool isExtendedInteger() const;
658 bool isExtendedVector() const;
659 bool isExtended64BitVector() const;
660 bool isExtended128BitVector() const;
661 bool isExtended256BitVector() const;
662 bool isExtended512BitVector() const;
663 EVT getExtendedVectorElementType() const;
664 unsigned getExtendedVectorNumElements() const;
665 unsigned getExtendedSizeInBits() const;
668 } // End llvm namespace