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 untyped = 36, // This value takes a register, but has
87 // unspecified type. The register class
88 // will be determined by the opcode.
90 LAST_VALUETYPE = 37, // This always remains at the end of the list.
92 // This is the current maximum for LAST_VALUETYPE.
93 // MVT::MAX_ALLOWED_VALUETYPE is used for asserts and to size bit vectors
94 // This value must be a multiple of 32.
95 MAX_ALLOWED_VALUETYPE = 64,
97 // Metadata - This is MDNode or MDString.
100 // iPTRAny - An int value the size of the pointer of the current
101 // target to any address space. This must only be used internal to
102 // tblgen. Other than for overloading, we treat iPTRAny the same as iPTR.
105 // vAny - A vector with any length and element size. This is used
106 // for intrinsics that have overloadings based on vector types.
107 // This is only for tblgen's consumption!
110 // fAny - Any floating-point or vector floating-point value. This is used
111 // for intrinsics that have overloadings based on floating-point types.
112 // This is only for tblgen's consumption!
115 // iAny - An integer or vector integer value of any bit width. This is
116 // used for intrinsics that have overloadings based on integer bit widths.
117 // This is only for tblgen's consumption!
120 // iPTR - An int value the size of the pointer of the current
121 // target. This should only be used internal to tblgen!
124 // LastSimpleValueType - The greatest valid SimpleValueType value.
125 LastSimpleValueType = 255,
127 // INVALID_SIMPLE_VALUE_TYPE - Simple value types greater than or equal
128 // to this are considered extended value types.
129 INVALID_SIMPLE_VALUE_TYPE = LastSimpleValueType + 1
132 SimpleValueType SimpleTy;
134 MVT() : SimpleTy((SimpleValueType)(INVALID_SIMPLE_VALUE_TYPE)) {}
135 MVT(SimpleValueType SVT) : SimpleTy(SVT) { }
137 bool operator>(const MVT& S) const { return SimpleTy > S.SimpleTy; }
138 bool operator<(const MVT& S) const { return SimpleTy < S.SimpleTy; }
139 bool operator==(const MVT& S) const { return SimpleTy == S.SimpleTy; }
140 bool operator!=(const MVT& S) const { return SimpleTy != S.SimpleTy; }
141 bool operator>=(const MVT& S) const { return SimpleTy >= S.SimpleTy; }
142 bool operator<=(const MVT& S) const { return SimpleTy <= S.SimpleTy; }
144 /// isFloatingPoint - Return true if this is a FP, or a vector FP type.
145 bool isFloatingPoint() const {
146 return ((SimpleTy >= MVT::f32 && SimpleTy <= MVT::ppcf128) ||
147 (SimpleTy >= MVT::v2f32 && SimpleTy <= MVT::v4f64));
150 /// isInteger - Return true if this is an integer, or a vector integer type.
151 bool isInteger() const {
152 return ((SimpleTy >= MVT::FIRST_INTEGER_VALUETYPE &&
153 SimpleTy <= MVT::LAST_INTEGER_VALUETYPE) ||
154 (SimpleTy >= MVT::v2i8 && SimpleTy <= MVT::v8i64));
157 /// isVector - Return true if this is a vector value type.
158 bool isVector() const {
159 return (SimpleTy >= MVT::FIRST_VECTOR_VALUETYPE &&
160 SimpleTy <= MVT::LAST_VECTOR_VALUETYPE);
163 /// isPow2VectorType - Returns true if the given vector is a power of 2.
164 bool isPow2VectorType() const {
165 unsigned NElts = getVectorNumElements();
166 return !(NElts & (NElts - 1));
169 /// getPow2VectorType - Widens the length of the given vector MVT up to
170 /// the nearest power of 2 and returns that type.
171 MVT getPow2VectorType() const {
172 if (isPow2VectorType())
175 unsigned NElts = getVectorNumElements();
176 unsigned Pow2NElts = 1 << Log2_32_Ceil(NElts);
177 return MVT::getVectorVT(getVectorElementType(), Pow2NElts);
180 /// getScalarType - If this is a vector type, return the element type,
181 /// otherwise return this.
182 MVT getScalarType() const {
183 return isVector() ? getVectorElementType() : *this;
186 MVT getVectorElementType() const {
189 return (MVT::SimpleValueType)(MVT::INVALID_SIMPLE_VALUE_TYPE);
194 case v32i8: return i8;
198 case v16i16: return i16;
201 case v8i32: return i32;
205 case v8i64: return i64;
208 case v8f32: return f32;
210 case v4f64: return f64;
214 unsigned getVectorNumElements() const {
218 case v32i8: return 32;
220 case v16i16: return 16;
225 case v8f32: return 8;
231 case v4f64: return 4;
237 case v2f64: return 2;
238 case v1i64: return 1;
242 unsigned getSizeInBits() const {
245 assert(0 && "Value type size is target-dependent. Ask TLI.");
249 assert(0 && "Value type is overloaded.");
251 assert(0 && "getSizeInBits called on extended MVT.");
255 case v2i8: return 16;
259 case v2i16: return 32;
267 case v2f32: return 64;
268 case f80 : return 80;
277 case v2f64: return 128;
283 case v4f64: return 256;
284 case v8i64: return 512;
288 /// getStoreSize - Return the number of bytes overwritten by a store
289 /// of the specified value type.
290 unsigned getStoreSize() const {
291 return (getSizeInBits() + 7) / 8;
294 /// getStoreSizeInBits - Return the number of bits overwritten by a store
295 /// of the specified value type.
296 unsigned getStoreSizeInBits() const {
297 return getStoreSize() * 8;
300 static MVT getFloatingPointVT(unsigned BitWidth) {
303 assert(false && "Bad bit width!");
315 static MVT getIntegerVT(unsigned BitWidth) {
318 return (MVT::SimpleValueType)(MVT::INVALID_SIMPLE_VALUE_TYPE);
334 static MVT getVectorVT(MVT VT, unsigned NumElements) {
335 switch (VT.SimpleTy) {
339 if (NumElements == 2) return MVT::v2i8;
340 if (NumElements == 4) return MVT::v4i8;
341 if (NumElements == 8) return MVT::v8i8;
342 if (NumElements == 16) return MVT::v16i8;
343 if (NumElements == 32) return MVT::v32i8;
346 if (NumElements == 2) return MVT::v2i16;
347 if (NumElements == 4) return MVT::v4i16;
348 if (NumElements == 8) return MVT::v8i16;
349 if (NumElements == 16) return MVT::v16i16;
352 if (NumElements == 2) return MVT::v2i32;
353 if (NumElements == 4) return MVT::v4i32;
354 if (NumElements == 8) return MVT::v8i32;
357 if (NumElements == 1) return MVT::v1i64;
358 if (NumElements == 2) return MVT::v2i64;
359 if (NumElements == 4) return MVT::v4i64;
360 if (NumElements == 8) return MVT::v8i64;
363 if (NumElements == 2) return MVT::v2f32;
364 if (NumElements == 4) return MVT::v4f32;
365 if (NumElements == 8) return MVT::v8f32;
368 if (NumElements == 2) return MVT::v2f64;
369 if (NumElements == 4) return MVT::v4f64;
372 return (MVT::SimpleValueType)(MVT::INVALID_SIMPLE_VALUE_TYPE);
377 /// EVT - Extended Value Type. Capable of holding value types which are not
378 /// native for any processor (such as the i12345 type), as well as the types
379 /// a MVT can represent.
386 EVT() : V((MVT::SimpleValueType)(MVT::INVALID_SIMPLE_VALUE_TYPE)),
388 EVT(MVT::SimpleValueType SVT) : V(SVT), LLVMTy(0) { }
389 EVT(MVT S) : V(S), LLVMTy(0) {}
391 bool operator==(EVT VT) const {
392 return !(*this != VT);
394 bool operator!=(EVT VT) const {
395 if (V.SimpleTy != VT.V.SimpleTy)
397 if (V.SimpleTy == MVT::INVALID_SIMPLE_VALUE_TYPE)
398 return LLVMTy != VT.LLVMTy;
402 /// getFloatingPointVT - Returns the EVT that represents a floating point
403 /// type with the given number of bits. There are two floating point types
404 /// with 128 bits - this returns f128 rather than ppcf128.
405 static EVT getFloatingPointVT(unsigned BitWidth) {
406 return MVT::getFloatingPointVT(BitWidth);
409 /// getIntegerVT - Returns the EVT that represents an integer with the given
411 static EVT getIntegerVT(LLVMContext &Context, unsigned BitWidth) {
412 MVT M = MVT::getIntegerVT(BitWidth);
413 if (M.SimpleTy != MVT::INVALID_SIMPLE_VALUE_TYPE)
415 return getExtendedIntegerVT(Context, BitWidth);
418 /// getVectorVT - Returns the EVT that represents a vector NumElements in
419 /// length, where each element is of type VT.
420 static EVT getVectorVT(LLVMContext &Context, EVT VT, unsigned NumElements) {
421 MVT M = MVT::getVectorVT(VT.V, NumElements);
422 if (M.SimpleTy != MVT::INVALID_SIMPLE_VALUE_TYPE)
424 return getExtendedVectorVT(Context, VT, NumElements);
427 /// getIntVectorWithNumElements - Return any integer vector type that has
428 /// the specified number of elements.
429 static EVT getIntVectorWithNumElements(LLVMContext &C, unsigned NumElts) {
431 default: return getVectorVT(C, MVT::i8, NumElts);
432 case 1: return MVT::v1i64;
433 case 2: return MVT::v2i32;
434 case 4: return MVT::v4i16;
435 case 8: return MVT::v8i8;
436 case 16: return MVT::v16i8;
438 return MVT::INVALID_SIMPLE_VALUE_TYPE;
441 /// isSimple - Test if the given EVT is simple (as opposed to being
443 bool isSimple() const {
444 return V.SimpleTy <= MVT::LastSimpleValueType;
447 /// isExtended - Test if the given EVT is extended (as opposed to
449 bool isExtended() const {
453 /// isFloatingPoint - Return true if this is a FP, or a vector FP type.
454 bool isFloatingPoint() const {
455 return isSimple() ? V.isFloatingPoint() : isExtendedFloatingPoint();
458 /// isInteger - Return true if this is an integer, or a vector integer type.
459 bool isInteger() const {
460 return isSimple() ? V.isInteger() : isExtendedInteger();
463 /// isVector - Return true if this is a vector value type.
464 bool isVector() const {
465 return isSimple() ? V.isVector() : isExtendedVector();
468 /// is64BitVector - Return true if this is a 64-bit vector type.
469 bool is64BitVector() const {
471 return isExtended64BitVector();
473 return (V == MVT::v8i8 || V==MVT::v4i16 || V==MVT::v2i32 ||
474 V == MVT::v1i64 || V==MVT::v2f32);
477 /// is128BitVector - Return true if this is a 128-bit vector type.
478 bool is128BitVector() const {
480 return isExtended128BitVector();
481 return (V==MVT::v16i8 || V==MVT::v8i16 || V==MVT::v4i32 ||
482 V==MVT::v2i64 || V==MVT::v4f32 || V==MVT::v2f64);
485 /// is256BitVector - Return true if this is a 256-bit vector type.
486 inline bool is256BitVector() const {
488 return isExtended256BitVector();
489 return (V == MVT::v8f32 || V == MVT::v4f64 || V == MVT::v32i8 ||
490 V == MVT::v16i16 || V == MVT::v8i32 || V == MVT::v4i64);
493 /// is512BitVector - Return true if this is a 512-bit vector type.
494 inline bool is512BitVector() const {
495 return isSimple() ? (V == MVT::v8i64) : isExtended512BitVector();
498 /// isOverloaded - Return true if this is an overloaded type for TableGen.
499 bool isOverloaded() const {
500 return (V==MVT::iAny || V==MVT::fAny || V==MVT::vAny || V==MVT::iPTRAny);
503 /// isByteSized - Return true if the bit size is a multiple of 8.
504 bool isByteSized() const {
505 return (getSizeInBits() & 7) == 0;
508 /// isRound - Return true if the size is a power-of-two number of bytes.
509 bool isRound() const {
510 unsigned BitSize = getSizeInBits();
511 return BitSize >= 8 && !(BitSize & (BitSize - 1));
514 /// bitsEq - Return true if this has the same number of bits as VT.
515 bool bitsEq(EVT VT) const {
516 if (EVT::operator==(VT)) return true;
517 return getSizeInBits() == VT.getSizeInBits();
520 /// bitsGT - Return true if this has more bits than VT.
521 bool bitsGT(EVT VT) const {
522 if (EVT::operator==(VT)) return false;
523 return getSizeInBits() > VT.getSizeInBits();
526 /// bitsGE - Return true if this has no less bits than VT.
527 bool bitsGE(EVT VT) const {
528 if (EVT::operator==(VT)) return true;
529 return getSizeInBits() >= VT.getSizeInBits();
532 /// bitsLT - Return true if this has less bits than VT.
533 bool bitsLT(EVT VT) const {
534 if (EVT::operator==(VT)) return false;
535 return getSizeInBits() < VT.getSizeInBits();
538 /// bitsLE - Return true if this has no more bits than VT.
539 bool bitsLE(EVT VT) const {
540 if (EVT::operator==(VT)) return true;
541 return getSizeInBits() <= VT.getSizeInBits();
545 /// getSimpleVT - Return the SimpleValueType held in the specified
547 MVT getSimpleVT() const {
548 assert(isSimple() && "Expected a SimpleValueType!");
552 /// getScalarType - If this is a vector type, return the element type,
553 /// otherwise return this.
554 EVT getScalarType() const {
555 return isVector() ? getVectorElementType() : *this;
558 /// getVectorElementType - Given a vector type, return the type of
560 EVT getVectorElementType() const {
561 assert(isVector() && "Invalid vector type!");
563 return V.getVectorElementType();
564 return getExtendedVectorElementType();
567 /// getVectorNumElements - Given a vector type, return the number of
568 /// elements it contains.
569 unsigned getVectorNumElements() const {
570 assert(isVector() && "Invalid vector type!");
572 return V.getVectorNumElements();
573 return getExtendedVectorNumElements();
576 /// getSizeInBits - Return the size of the specified value type in bits.
577 unsigned getSizeInBits() const {
579 return V.getSizeInBits();
580 return getExtendedSizeInBits();
583 /// getStoreSize - Return the number of bytes overwritten by a store
584 /// of the specified value type.
585 unsigned getStoreSize() const {
586 return (getSizeInBits() + 7) / 8;
589 /// getStoreSizeInBits - Return the number of bits overwritten by a store
590 /// of the specified value type.
591 unsigned getStoreSizeInBits() const {
592 return getStoreSize() * 8;
595 /// getRoundIntegerType - Rounds the bit-width of the given integer EVT up
596 /// to the nearest power of two (and at least to eight), and returns the
597 /// integer EVT with that number of bits.
598 EVT getRoundIntegerType(LLVMContext &Context) const {
599 assert(isInteger() && !isVector() && "Invalid integer type!");
600 unsigned BitWidth = getSizeInBits();
603 return getIntegerVT(Context, 1 << Log2_32_Ceil(BitWidth));
606 /// getHalfSizedIntegerVT - Finds the smallest simple value type that is
607 /// greater than or equal to half the width of this EVT. If no simple
608 /// value type can be found, an extended integer value type of half the
609 /// size (rounded up) is returned.
610 EVT getHalfSizedIntegerVT(LLVMContext &Context) const {
611 assert(isInteger() && !isVector() && "Invalid integer type!");
612 unsigned EVTSize = getSizeInBits();
613 for (unsigned IntVT = MVT::FIRST_INTEGER_VALUETYPE;
614 IntVT <= MVT::LAST_INTEGER_VALUETYPE; ++IntVT) {
615 EVT HalfVT = EVT((MVT::SimpleValueType)IntVT);
616 if (HalfVT.getSizeInBits() * 2 >= EVTSize)
619 return getIntegerVT(Context, (EVTSize + 1) / 2);
622 /// isPow2VectorType - Returns true if the given vector is a power of 2.
623 bool isPow2VectorType() const {
624 unsigned NElts = getVectorNumElements();
625 return !(NElts & (NElts - 1));
628 /// getPow2VectorType - Widens the length of the given vector EVT up to
629 /// the nearest power of 2 and returns that type.
630 EVT getPow2VectorType(LLVMContext &Context) const {
631 if (!isPow2VectorType()) {
632 unsigned NElts = getVectorNumElements();
633 unsigned Pow2NElts = 1 << Log2_32_Ceil(NElts);
634 return EVT::getVectorVT(Context, getVectorElementType(), Pow2NElts);
641 /// getEVTString - This function returns value type as a string,
643 std::string getEVTString() const;
645 /// getTypeForEVT - This method returns an LLVM type corresponding to the
646 /// specified EVT. For integer types, this returns an unsigned type. Note
647 /// that this will abort for types that cannot be represented.
648 Type *getTypeForEVT(LLVMContext &Context) const;
650 /// getEVT - Return the value type corresponding to the specified type.
651 /// This returns all pointers as iPTR. If HandleUnknown is true, unknown
652 /// types are returned as Other, otherwise they are invalid.
653 static EVT getEVT(Type *Ty, bool HandleUnknown = false);
655 intptr_t getRawBits() {
659 return (intptr_t)(LLVMTy);
662 /// compareRawBits - A meaningless but well-behaved order, useful for
663 /// constructing containers.
664 struct compareRawBits {
665 bool operator()(EVT L, EVT R) const {
666 if (L.V.SimpleTy == R.V.SimpleTy)
667 return L.LLVMTy < R.LLVMTy;
669 return L.V.SimpleTy < R.V.SimpleTy;
674 // Methods for handling the Extended-type case in functions above.
675 // These are all out-of-line to prevent users of this header file
676 // from having a dependency on Type.h.
677 static EVT getExtendedIntegerVT(LLVMContext &C, unsigned BitWidth);
678 static EVT getExtendedVectorVT(LLVMContext &C, EVT VT,
679 unsigned NumElements);
680 bool isExtendedFloatingPoint() const;
681 bool isExtendedInteger() const;
682 bool isExtendedVector() const;
683 bool isExtended64BitVector() const;
684 bool isExtended128BitVector() const;
685 bool isExtended256BitVector() const;
686 bool isExtended512BitVector() const;
687 EVT getExtendedVectorElementType() const;
688 unsigned getExtendedVectorNumElements() const;
689 unsigned getExtendedSizeInBits() const;
692 } // End llvm namespace