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
50 Flag = 12, // This is a condition code or machine flag.
52 isVoid = 13, // This has no value
57 v16i8 = 17, // 16 x i8
58 v32i8 = 18, // 32 x i8
59 v2i16 = 19, // 2 x i16
60 v4i16 = 20, // 4 x i16
61 v8i16 = 21, // 8 x i16
62 v16i16 = 22, // 16 x i16
63 v2i32 = 23, // 2 x i32
64 v4i32 = 24, // 4 x i32
65 v8i32 = 25, // 8 x i32
66 v1i64 = 26, // 1 x i64
67 v2i64 = 27, // 2 x i64
68 v4i64 = 28, // 4 x i64
70 v2f32 = 29, // 2 x f32
71 v4f32 = 30, // 4 x f32
72 v8f32 = 31, // 8 x f32
73 v2f64 = 32, // 2 x f64
74 v4f64 = 33, // 4 x f64
76 FIRST_VECTOR_VALUETYPE = v2i8,
77 LAST_VECTOR_VALUETYPE = v4f64,
79 LAST_VALUETYPE = 34, // This always remains at the end of the list.
81 // This is the current maximum for LAST_VALUETYPE.
82 // EVT::MAX_ALLOWED_VALUETYPE is used for asserts and to size bit vectors
83 // This value must be a multiple of 32.
84 MAX_ALLOWED_VALUETYPE = 64,
86 // Metadata - This is MDNode or MDString.
89 // iPTRAny - An int value the size of the pointer of the current
90 // target to any address space. This must only be used internal to
91 // tblgen. Other than for overloading, we treat iPTRAny the same as iPTR.
94 // vAny - A vector with any length and element size. This is used
95 // for intrinsics that have overloadings based on vector types.
96 // This is only for tblgen's consumption!
99 // fAny - Any floating-point or vector floating-point value. This is used
100 // for intrinsics that have overloadings based on floating-point types.
101 // This is only for tblgen's consumption!
104 // iAny - An integer or vector integer value of any bit width. This is
105 // used for intrinsics that have overloadings based on integer bit widths.
106 // This is only for tblgen's consumption!
109 // iPTR - An int value the size of the pointer of the current
110 // target. This should only be used internal to tblgen!
113 // LastSimpleValueType - The greatest valid SimpleValueType value.
114 LastSimpleValueType = 255,
116 // INVALID_SIMPLE_VALUE_TYPE - Simple value types greater than or equal
117 // to this are considered extended value types.
118 INVALID_SIMPLE_VALUE_TYPE = LastSimpleValueType + 1
121 SimpleValueType SimpleTy;
123 MVT() : SimpleTy((SimpleValueType)(INVALID_SIMPLE_VALUE_TYPE)) {}
124 MVT(SimpleValueType SVT) : SimpleTy(SVT) { }
126 bool operator>(const MVT& S) const { return SimpleTy > S.SimpleTy; }
127 bool operator<(const MVT& S) const { return SimpleTy < S.SimpleTy; }
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; }
132 /// isFloatingPoint - Return true if this is a FP, or a vector FP type.
133 bool isFloatingPoint() const {
134 return ((SimpleTy >= MVT::f32 && SimpleTy <= MVT::ppcf128) ||
135 (SimpleTy >= MVT::v2f32 && SimpleTy <= MVT::v4f64));
138 /// isInteger - Return true if this is an integer, or a vector integer type.
139 bool isInteger() const {
140 return ((SimpleTy >= MVT::FIRST_INTEGER_VALUETYPE &&
141 SimpleTy <= MVT::LAST_INTEGER_VALUETYPE) ||
142 (SimpleTy >= MVT::v2i8 && SimpleTy <= MVT::v4i64));
145 /// isVector - Return true if this is a vector value type.
146 bool isVector() const {
147 return (SimpleTy >= MVT::FIRST_VECTOR_VALUETYPE &&
148 SimpleTy <= MVT::LAST_VECTOR_VALUETYPE);
151 /// isPow2VectorType - Retuns true if the given vector is a power of 2.
152 bool isPow2VectorType() const {
153 unsigned NElts = getVectorNumElements();
154 return !(NElts & (NElts - 1));
157 /// getPow2VectorType - Widens the length of the given vector EVT up to
158 /// the nearest power of 2 and returns that type.
159 MVT getPow2VectorType() const {
160 if (!isPow2VectorType()) {
161 unsigned NElts = getVectorNumElements();
162 unsigned Pow2NElts = 1 << Log2_32_Ceil(NElts);
163 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;
194 case v4i64: return i64;
197 case v8f32: return f32;
199 case v4f64: return f64;
203 unsigned getVectorNumElements() const {
207 case v32i8: return 32;
209 case v16i16: return 16;
213 case v8f32: return 8;
219 case v4f64: return 4;
225 case v2f64: return 2;
226 case v1i64: return 1;
230 unsigned getSizeInBits() const {
233 assert(0 && "Value type size is target-dependent. Ask TLI.");
237 assert(0 && "Value type is overloaded.");
239 assert(0 && "getSizeInBits called on extended MVT.");
243 case v2i8: return 16;
247 case v2i16: return 32;
254 case v2f32: return 64;
255 case f80 : return 80;
264 case v2f64: return 128;
270 case v4f64: return 256;
274 static MVT getFloatingPointVT(unsigned BitWidth) {
277 assert(false && "Bad bit width!");
289 static MVT getIntegerVT(unsigned BitWidth) {
292 return (MVT::SimpleValueType)(MVT::INVALID_SIMPLE_VALUE_TYPE);
308 static MVT getVectorVT(MVT VT, unsigned NumElements) {
309 switch (VT.SimpleTy) {
313 if (NumElements == 2) return MVT::v2i8;
314 if (NumElements == 4) return MVT::v4i8;
315 if (NumElements == 8) return MVT::v8i8;
316 if (NumElements == 16) return MVT::v16i8;
317 if (NumElements == 32) return MVT::v32i8;
320 if (NumElements == 2) return MVT::v2i16;
321 if (NumElements == 4) return MVT::v4i16;
322 if (NumElements == 8) return MVT::v8i16;
323 if (NumElements == 16) return MVT::v16i16;
326 if (NumElements == 2) return MVT::v2i32;
327 if (NumElements == 4) return MVT::v4i32;
328 if (NumElements == 8) return MVT::v8i32;
331 if (NumElements == 1) return MVT::v1i64;
332 if (NumElements == 2) return MVT::v2i64;
333 if (NumElements == 4) return MVT::v4i64;
336 if (NumElements == 2) return MVT::v2f32;
337 if (NumElements == 4) return MVT::v4f32;
338 if (NumElements == 8) return MVT::v8f32;
341 if (NumElements == 2) return MVT::v2f64;
342 if (NumElements == 4) return MVT::v4f64;
345 return (MVT::SimpleValueType)(MVT::INVALID_SIMPLE_VALUE_TYPE);
348 static MVT getIntVectorWithNumElements(unsigned NumElts) {
350 default: return (MVT::SimpleValueType)(MVT::INVALID_SIMPLE_VALUE_TYPE);
351 case 1: return MVT::v1i64;
352 case 2: return MVT::v2i32;
353 case 4: return MVT::v4i16;
354 case 8: return MVT::v8i8;
355 case 16: return MVT::v16i8;
360 struct EVT { // EVT = Extended Value Type
366 EVT() : V((MVT::SimpleValueType)(MVT::INVALID_SIMPLE_VALUE_TYPE)),
368 EVT(MVT::SimpleValueType SVT) : V(SVT), LLVMTy(0) { }
369 EVT(MVT S) : V(S), LLVMTy(0) {}
371 bool operator==(const EVT VT) const {
372 if (V.SimpleTy == VT.V.SimpleTy) {
373 if (V.SimpleTy == MVT::INVALID_SIMPLE_VALUE_TYPE)
374 return LLVMTy == VT.LLVMTy;
379 bool operator!=(const EVT VT) const {
380 if (V.SimpleTy == VT.V.SimpleTy) {
381 if (V.SimpleTy == MVT::INVALID_SIMPLE_VALUE_TYPE)
382 return LLVMTy != VT.LLVMTy;
388 /// getFloatingPointVT - Returns the EVT that represents a floating point
389 /// type with the given number of bits. There are two floating point types
390 /// with 128 bits - this returns f128 rather than ppcf128.
391 static EVT getFloatingPointVT(unsigned BitWidth) {
392 return MVT::getFloatingPointVT(BitWidth);
395 /// getIntegerVT - Returns the EVT that represents an integer with the given
397 static EVT getIntegerVT(LLVMContext &Context, unsigned BitWidth) {
398 MVT M = MVT::getIntegerVT(BitWidth);
399 if (M.SimpleTy == MVT::INVALID_SIMPLE_VALUE_TYPE)
400 return getExtendedIntegerVT(Context, BitWidth);
405 /// getVectorVT - Returns the EVT that represents a vector NumElements in
406 /// length, where each element is of type VT.
407 static EVT getVectorVT(LLVMContext &Context, EVT VT, unsigned NumElements) {
408 MVT M = MVT::getVectorVT(VT.V, NumElements);
409 if (M.SimpleTy == MVT::INVALID_SIMPLE_VALUE_TYPE)
410 return getExtendedVectorVT(Context, VT, NumElements);
415 /// getIntVectorWithNumElements - Return any integer vector type that has
416 /// the specified number of elements.
417 static EVT getIntVectorWithNumElements(LLVMContext &C, unsigned NumElts) {
418 MVT M = MVT::getIntVectorWithNumElements(NumElts);
419 if (M.SimpleTy == MVT::INVALID_SIMPLE_VALUE_TYPE)
420 return getVectorVT(C, MVT::i8, NumElts);
425 /// isSimple - Test if the given EVT is simple (as opposed to being
427 bool isSimple() const {
428 return V.SimpleTy <= MVT::LastSimpleValueType;
431 /// isExtended - Test if the given EVT is extended (as opposed to
433 bool isExtended() const {
437 /// isFloatingPoint - Return true if this is a FP, or a vector FP type.
438 bool isFloatingPoint() const {
440 ((V >= MVT::f32 && V <= MVT::ppcf128) ||
441 (V >= MVT::v2f32 && V <= MVT::v4f64)) : isExtendedFloatingPoint();
444 /// isInteger - Return true if this is an integer, or a vector integer type.
445 bool isInteger() const {
447 ((V >= MVT::FIRST_INTEGER_VALUETYPE &&
448 V <= MVT::LAST_INTEGER_VALUETYPE) ||
449 (V >= MVT::v2i8 && V <= MVT::v4i64)) : isExtendedInteger();
452 /// isVector - Return true if this is a vector value type.
453 bool isVector() const {
455 (V >= MVT::FIRST_VECTOR_VALUETYPE && V <=
456 MVT::LAST_VECTOR_VALUETYPE) :
460 /// is64BitVector - Return true if this is a 64-bit vector type.
461 bool is64BitVector() const {
463 (V==MVT::v8i8 || V==MVT::v4i16 || V==MVT::v2i32 ||
464 V==MVT::v1i64 || V==MVT::v2f32) :
465 isExtended64BitVector();
468 /// is128BitVector - Return true if this is a 128-bit vector type.
469 bool is128BitVector() const {
471 (V==MVT::v16i8 || V==MVT::v8i16 || V==MVT::v4i32 ||
472 V==MVT::v2i64 || V==MVT::v4f32 || V==MVT::v2f64) :
473 isExtended128BitVector();
476 /// is256BitVector - Return true if this is a 256-bit vector type.
477 inline bool is256BitVector() const {
479 (V==MVT::v8f32 || V==MVT::v4f64 || V==MVT::v32i8 ||
480 V==MVT::v16i16 || V==MVT::v8i32 || V==MVT::v4i64) :
481 isExtended256BitVector();
484 /// isOverloaded - Return true if this is an overloaded type for TableGen.
485 bool isOverloaded() const {
486 return (V==MVT::iAny || V==MVT::fAny || V==MVT::vAny || V==MVT::iPTRAny);
489 /// isByteSized - Return true if the bit size is a multiple of 8.
490 bool isByteSized() const {
491 return (getSizeInBits() & 7) == 0;
494 /// isRound - Return true if the size is a power-of-two number of bytes.
495 bool isRound() const {
496 unsigned BitSize = getSizeInBits();
497 return BitSize >= 8 && !(BitSize & (BitSize - 1));
500 /// bitsEq - Return true if this has the same number of bits as VT.
501 bool bitsEq(EVT VT) const {
502 return getSizeInBits() == VT.getSizeInBits();
505 /// bitsGT - Return true if this has more bits than VT.
506 bool bitsGT(EVT VT) const {
507 return getSizeInBits() > VT.getSizeInBits();
510 /// bitsGE - Return true if this has no less bits than VT.
511 bool bitsGE(EVT VT) const {
512 return getSizeInBits() >= VT.getSizeInBits();
515 /// bitsLT - Return true if this has less bits than VT.
516 bool bitsLT(EVT VT) const {
517 return getSizeInBits() < VT.getSizeInBits();
520 /// bitsLE - Return true if this has no more bits than VT.
521 bool bitsLE(EVT VT) const {
522 return getSizeInBits() <= VT.getSizeInBits();
526 /// getSimpleVT - Return the SimpleValueType held in the specified
528 MVT getSimpleVT() const {
529 assert(isSimple() && "Expected a SimpleValueType!");
533 /// getScalarType - If this is a vector type, return the element type,
534 /// otherwise return this.
535 EVT getScalarType() const {
536 return isVector() ? getVectorElementType() : *this;
539 /// getVectorElementType - Given a vector type, return the type of
541 EVT getVectorElementType() const {
542 assert(isVector() && "Invalid vector type!");
544 return V.getVectorElementType();
546 return getExtendedVectorElementType();
549 /// getVectorNumElements - Given a vector type, return the number of
550 /// elements it contains.
551 unsigned getVectorNumElements() const {
552 assert(isVector() && "Invalid vector type!");
554 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();
564 return getExtendedSizeInBits();
567 /// getStoreSize - Return the number of bytes overwritten by a store
568 /// of the specified value type.
569 unsigned getStoreSize() const {
570 return (getSizeInBits() + 7) / 8;
573 /// getStoreSizeInBits - Return the number of bits overwritten by a store
574 /// of the specified value type.
575 unsigned getStoreSizeInBits() const {
576 return getStoreSize() * 8;
579 /// getRoundIntegerType - Rounds the bit-width of the given integer EVT up
580 /// to the nearest power of two (and at least to eight), and returns the
581 /// integer EVT with that number of bits.
582 EVT getRoundIntegerType(LLVMContext &Context) const {
583 assert(isInteger() && !isVector() && "Invalid integer type!");
584 unsigned BitWidth = getSizeInBits();
588 return getIntegerVT(Context, 1 << Log2_32_Ceil(BitWidth));
591 /// isPow2VectorType - Retuns true if the given vector is a power of 2.
592 bool isPow2VectorType() const {
593 unsigned NElts = getVectorNumElements();
594 return !(NElts & (NElts - 1));
597 /// getPow2VectorType - Widens the length of the given vector EVT up to
598 /// the nearest power of 2 and returns that type.
599 EVT getPow2VectorType(LLVMContext &Context) const {
600 if (!isPow2VectorType()) {
601 unsigned NElts = getVectorNumElements();
602 unsigned Pow2NElts = 1 << Log2_32_Ceil(NElts);
603 return EVT::getVectorVT(Context, getVectorElementType(), Pow2NElts);
610 /// getEVTString - This function returns value type as a string,
612 std::string getEVTString() const;
614 /// getTypeForEVT - This method returns an LLVM type corresponding to the
615 /// specified EVT. For integer types, this returns an unsigned type. Note
616 /// that this will abort for types that cannot be represented.
617 const Type *getTypeForEVT(LLVMContext &Context) const;
619 /// getEVT - Return the value type corresponding to the specified type.
620 /// This returns all pointers as iPTR. If HandleUnknown is true, unknown
621 /// types are returned as Other, otherwise they are invalid.
622 static EVT getEVT(const Type *Ty, bool HandleUnknown = false);
624 intptr_t getRawBits() {
625 if (V.SimpleTy <= MVT::LastSimpleValueType)
628 return (intptr_t)(LLVMTy);
631 /// compareRawBits - A meaningless but well-behaved order, useful for
632 /// constructing containers.
633 struct compareRawBits {
634 bool operator()(EVT L, EVT R) const {
635 if (L.V.SimpleTy == R.V.SimpleTy)
636 return L.LLVMTy < R.LLVMTy;
638 return L.V.SimpleTy < R.V.SimpleTy;
643 // Methods for handling the Extended-type case in functions above.
644 // These are all out-of-line to prevent users of this header file
645 // from having a dependency on Type.h.
646 static EVT getExtendedIntegerVT(LLVMContext &C, unsigned BitWidth);
647 static EVT getExtendedVectorVT(LLVMContext &C, EVT VT,
648 unsigned NumElements);
649 bool isExtendedFloatingPoint() const;
650 bool isExtendedInteger() const;
651 bool isExtendedVector() const;
652 bool isExtended64BitVector() const;
653 bool isExtended128BitVector() const;
654 bool isExtended256BitVector() const;
655 EVT getExtendedVectorElementType() const;
656 unsigned getExtendedVectorNumElements() const;
657 unsigned getExtendedSizeInBits() const;
660 } // End llvm namespace