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 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
117 SimpleValueType SimpleTy;
119 MVT() : SimpleTy((SimpleValueType)(LastSimpleValueType+1)) {}
120 MVT(SimpleValueType SVT) : SimpleTy(SVT) { }
122 bool operator>(const MVT& S) const { return SimpleTy > S.SimpleTy; }
123 bool operator<(const MVT& S) const { return SimpleTy < S.SimpleTy; }
124 bool operator==(const MVT& S) const { return SimpleTy == S.SimpleTy; }
125 bool operator>=(const MVT& S) const { return SimpleTy >= S.SimpleTy; }
126 bool operator<=(const MVT& S) const { return SimpleTy <= S.SimpleTy; }
128 /// isFloatingPoint - Return true if this is a FP, or a vector FP type.
129 bool isFloatingPoint() const {
130 return ((SimpleTy >= MVT::f32 && SimpleTy <= MVT::ppcf128) ||
131 (SimpleTy >= MVT::v2f32 && SimpleTy <= MVT::v4f64));
134 /// isInteger - Return true if this is an integer, or a vector integer type.
135 bool isInteger() const {
136 return ((SimpleTy >= MVT::FIRST_INTEGER_VALUETYPE &&
137 SimpleTy <= MVT::LAST_INTEGER_VALUETYPE) ||
138 (SimpleTy >= MVT::v2i8 && SimpleTy <= MVT::v4i64));
141 /// isVector - Return true if this is a vector value type.
142 bool isVector() const {
143 return (SimpleTy >= MVT::FIRST_VECTOR_VALUETYPE &&
144 SimpleTy <= MVT::LAST_VECTOR_VALUETYPE);
147 MVT getVectorElementType() const {
150 return (MVT::SimpleValueType)(MVT::LastSimpleValueType+1);
155 case v32i8: return i8;
159 case v16i16: return i16;
162 case v8i32: return i32;
165 case v4i64: return i64;
168 case v8f32: return f32;
170 case v4f64: return f64;
174 unsigned getVectorNumElements() const {
178 case v32i8: return 32;
180 case v16i16: return 16;
184 case v8f32: return 8;
190 case v4f64: return 4;
196 case v2f64: return 2;
197 case v1i64: return 1;
201 unsigned getSizeInBits() const {
204 assert(0 && "Value type size is target-dependent. Ask TLI.");
208 assert(0 && "Value type is overloaded.");
210 assert(0 && "getSizeInBits called on extended MVT.");
214 case v2i8: return 16;
218 case v2i16: return 32;
225 case v2f32: return 64;
226 case f80 : return 80;
235 case v2f64: return 128;
241 case v4f64: return 256;
245 static MVT getFloatingPointVT(unsigned BitWidth) {
248 assert(false && "Bad bit width!");
260 static MVT getIntegerVT(unsigned BitWidth) {
263 return (MVT::SimpleValueType)(MVT::LastSimpleValueType+1);
279 static MVT getVectorVT(MVT VT, unsigned NumElements) {
280 switch (VT.SimpleTy) {
284 if (NumElements == 2) return MVT::v2i8;
285 if (NumElements == 4) return MVT::v4i8;
286 if (NumElements == 8) return MVT::v8i8;
287 if (NumElements == 16) return MVT::v16i8;
288 if (NumElements == 32) return MVT::v32i8;
291 if (NumElements == 2) return MVT::v2i16;
292 if (NumElements == 4) return MVT::v4i16;
293 if (NumElements == 8) return MVT::v8i16;
294 if (NumElements == 16) return MVT::v16i16;
297 if (NumElements == 2) return MVT::v2i32;
298 if (NumElements == 4) return MVT::v4i32;
299 if (NumElements == 8) return MVT::v8i32;
302 if (NumElements == 1) return MVT::v1i64;
303 if (NumElements == 2) return MVT::v2i64;
304 if (NumElements == 4) return MVT::v4i64;
307 if (NumElements == 2) return MVT::v2f32;
308 if (NumElements == 4) return MVT::v4f32;
309 if (NumElements == 8) return MVT::v8f32;
312 if (NumElements == 2) return MVT::v2f64;
313 if (NumElements == 4) return MVT::v4f64;
316 return (MVT::SimpleValueType)(MVT::LastSimpleValueType+1);
319 static MVT getIntVectorWithNumElements(unsigned NumElts) {
321 default: return (MVT::SimpleValueType)(MVT::LastSimpleValueType+1);
322 case 1: return MVT::v1i64;
323 case 2: return MVT::v2i32;
324 case 4: return MVT::v4i16;
325 case 8: return MVT::v8i8;
326 case 16: return MVT::v16i8;
331 struct EVT { // EVT = Extended Value Type
337 EVT() : V((MVT::SimpleValueType)(MVT::LastSimpleValueType+1)) {}
338 EVT(MVT::SimpleValueType SVT) : V(SVT) { }
341 bool operator==(const EVT VT) const {
342 if (V.SimpleTy == VT.V.SimpleTy) {
343 if (V.SimpleTy == MVT::LastSimpleValueType+1)
344 return LLVMTy == VT.LLVMTy;
349 bool operator!=(const EVT VT) const {
350 if (V.SimpleTy == VT.V.SimpleTy) {
351 if (V.SimpleTy == MVT::LastSimpleValueType+1)
352 return LLVMTy != VT.LLVMTy;
358 /// getFloatingPointVT - Returns the EVT that represents a floating point
359 /// type with the given number of bits. There are two floating point types
360 /// with 128 bits - this returns f128 rather than ppcf128.
361 static EVT getFloatingPointVT(unsigned BitWidth) {
362 return MVT::getFloatingPointVT(BitWidth);
365 /// getIntegerVT - Returns the EVT that represents an integer with the given
367 static EVT getIntegerVT(unsigned BitWidth) {
368 MVT M = MVT::getIntegerVT(BitWidth);
369 if (M.SimpleTy == MVT::LastSimpleValueType+1)
370 return getExtendedIntegerVT(BitWidth);
375 /// getVectorVT - Returns the EVT that represents a vector NumElements in
376 /// length, where each element is of type VT.
377 static EVT getVectorVT(EVT VT, unsigned NumElements) {
378 MVT M = MVT::getVectorVT(VT.V, NumElements);
379 if (M.SimpleTy == MVT::LastSimpleValueType+1)
380 return getExtendedVectorVT(VT, NumElements);
385 /// getIntVectorWithNumElements - Return any integer vector type that has
386 /// the specified number of elements.
387 static EVT getIntVectorWithNumElements(unsigned NumElts) {
388 MVT M = MVT::getIntVectorWithNumElements(NumElts);
389 if (M.SimpleTy == MVT::LastSimpleValueType+1)
390 return getVectorVT(EVT(MVT::i8), NumElts);
395 /// isSimple - Test if the given EVT is simple (as opposed to being
397 bool isSimple() const {
398 return V.SimpleTy <= MVT::LastSimpleValueType;
401 /// isExtended - Test if the given EVT is extended (as opposed to
403 bool isExtended() const {
407 /// isFloatingPoint - Return true if this is a FP, or a vector FP type.
408 bool isFloatingPoint() const {
410 ((V >= MVT::f32 && V <= MVT::ppcf128) ||
411 (V >= MVT::v2f32 && V <= MVT::v4f64)) : isExtendedFloatingPoint();
414 /// isInteger - Return true if this is an integer, or a vector integer type.
415 bool isInteger() const {
417 ((V >= MVT::FIRST_INTEGER_VALUETYPE &&
418 V <= MVT::LAST_INTEGER_VALUETYPE) ||
419 (V >= MVT::v2i8 && V <= MVT::v4i64)) : isExtendedInteger();
422 /// isVector - Return true if this is a vector value type.
423 bool isVector() const {
425 (V >= MVT::FIRST_VECTOR_VALUETYPE && V <=
426 MVT::LAST_VECTOR_VALUETYPE) :
430 /// is64BitVector - Return true if this is a 64-bit vector type.
431 bool is64BitVector() const {
433 (V==MVT::v8i8 || V==MVT::v4i16 || V==MVT::v2i32 ||
434 V==MVT::v1i64 || V==MVT::v2f32) :
435 isExtended64BitVector();
438 /// is128BitVector - Return true if this is a 128-bit vector type.
439 bool is128BitVector() const {
441 (V==MVT::v16i8 || V==MVT::v8i16 || V==MVT::v4i32 ||
442 V==MVT::v2i64 || V==MVT::v4f32 || V==MVT::v2f64) :
443 isExtended128BitVector();
446 /// is256BitVector - Return true if this is a 256-bit vector type.
447 inline bool is256BitVector() const {
449 (V==MVT::v8f32 || V==MVT::v4f64 || V==MVT::v32i8 ||
450 V==MVT::v16i16 || V==MVT::v8i32 || V==MVT::v4i64) :
451 isExtended256BitVector();
454 /// isOverloaded - Return true if this is an overloaded type for TableGen.
455 bool isOverloaded() const {
456 return (V==MVT::iAny || V==MVT::fAny || V==MVT::vAny || V==MVT::iPTRAny);
459 /// isByteSized - Return true if the bit size is a multiple of 8.
460 bool isByteSized() const {
461 return (getSizeInBits() & 7) == 0;
464 /// isRound - Return true if the size is a power-of-two number of bytes.
465 bool isRound() const {
466 unsigned BitSize = getSizeInBits();
467 return BitSize >= 8 && !(BitSize & (BitSize - 1));
470 /// bitsEq - Return true if this has the same number of bits as VT.
471 bool bitsEq(EVT VT) const {
472 return getSizeInBits() == VT.getSizeInBits();
475 /// bitsGT - Return true if this has more bits than VT.
476 bool bitsGT(EVT VT) const {
477 return getSizeInBits() > VT.getSizeInBits();
480 /// bitsGE - Return true if this has no less bits than VT.
481 bool bitsGE(EVT VT) const {
482 return getSizeInBits() >= VT.getSizeInBits();
485 /// bitsLT - Return true if this has less bits than VT.
486 bool bitsLT(EVT VT) const {
487 return getSizeInBits() < VT.getSizeInBits();
490 /// bitsLE - Return true if this has no more bits than VT.
491 bool bitsLE(EVT VT) const {
492 return getSizeInBits() <= VT.getSizeInBits();
496 /// getSimpleVT - Return the SimpleValueType held in the specified
498 MVT getSimpleVT() const {
499 assert(isSimple() && "Expected a SimpleValueType!");
503 /// getVectorElementType - Given a vector type, return the type of
505 EVT getVectorElementType() const {
506 assert(isVector() && "Invalid vector type!");
508 return V.getVectorElementType();
510 return getExtendedVectorElementType();
513 /// getVectorNumElements - Given a vector type, return the number of
514 /// elements it contains.
515 unsigned getVectorNumElements() const {
516 assert(isVector() && "Invalid vector type!");
518 return V.getVectorNumElements();
520 return getExtendedVectorNumElements();
523 /// getSizeInBits - Return the size of the specified value type in bits.
524 unsigned getSizeInBits() const {
526 return V.getSizeInBits();
528 return getExtendedSizeInBits();
531 /// getStoreSizeInBits - Return the number of bits overwritten by a store
532 /// of the specified value type.
533 unsigned getStoreSizeInBits() const {
534 return (getSizeInBits() + 7)/8*8;
537 /// getRoundIntegerType - Rounds the bit-width of the given integer EVT up
538 /// to the nearest power of two (and at least to eight), and returns the
539 /// integer EVT with that number of bits.
540 EVT getRoundIntegerType() const {
541 assert(isInteger() && !isVector() && "Invalid integer type!");
542 unsigned BitWidth = getSizeInBits();
546 return getIntegerVT(1 << Log2_32_Ceil(BitWidth));
549 /// isPow2VectorType - Retuns true if the given vector is a power of 2.
550 bool isPow2VectorType() const {
551 unsigned NElts = getVectorNumElements();
552 return !(NElts & (NElts - 1));
555 /// getPow2VectorType - Widens the length of the given vector EVT up to
556 /// the nearest power of 2 and returns that type.
557 EVT getPow2VectorType() const {
558 if (!isPow2VectorType()) {
559 unsigned NElts = getVectorNumElements();
560 unsigned Pow2NElts = 1 << Log2_32_Ceil(NElts);
561 return EVT::getVectorVT(getVectorElementType(), Pow2NElts);
568 /// getEVTString - This function returns value type as a string,
570 std::string getEVTString() const;
572 /// getTypeForEVT - This method returns an LLVM type corresponding to the
573 /// specified EVT. For integer types, this returns an unsigned type. Note
574 /// that this will abort for types that cannot be represented.
575 const Type *getTypeForEVT() const;
577 /// getEVT - Return the value type corresponding to the specified type.
578 /// This returns all pointers as iPTR. If HandleUnknown is true, unknown
579 /// types are returned as Other, otherwise they are invalid.
580 static EVT getEVT(const Type *Ty, bool HandleUnknown = false);
582 intptr_t getRawBits() {
583 if (V.SimpleTy <= MVT::LastSimpleValueType)
586 return (intptr_t)(LLVMTy);
589 /// compareRawBits - A meaningless but well-behaved order, useful for
590 /// constructing containers.
591 struct compareRawBits {
592 bool operator()(EVT L, EVT R) const {
593 if (L.V.SimpleTy == R.V.SimpleTy)
594 return L.LLVMTy < R.LLVMTy;
596 return L.V.SimpleTy < R.V.SimpleTy;
601 // Methods for handling the Extended-type case in functions above.
602 // These are all out-of-line to prevent users of this header file
603 // from having a dependency on Type.h.
604 static EVT getExtendedIntegerVT(unsigned BitWidth);
605 static EVT getExtendedVectorVT(EVT VT, unsigned NumElements);
606 bool isExtendedFloatingPoint() const;
607 bool isExtendedInteger() const;
608 bool isExtendedVector() const;
609 bool isExtended64BitVector() const;
610 bool isExtended128BitVector() const;
611 bool isExtended256BitVector() const;
612 EVT getExtendedVectorElementType() const;
613 unsigned getExtendedVectorNumElements() const;
614 unsigned getExtendedSizeInBits() const;
617 } // End llvm namespace