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,
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 MVT getVectorElementType() const {
173 return (MVT::SimpleValueType)(MVT::INVALID_SIMPLE_VALUE_TYPE);
178 case v32i8: return i8;
182 case v16i16: return i16;
185 case v8i32: return i32;
188 case v4i64: return i64;
191 case v8f32: return f32;
193 case v4f64: return f64;
197 unsigned getVectorNumElements() const {
201 case v32i8: return 32;
203 case v16i16: return 16;
207 case v8f32: return 8;
213 case v4f64: return 4;
219 case v2f64: return 2;
220 case v1i64: return 1;
224 unsigned getSizeInBits() const {
227 assert(0 && "Value type size is target-dependent. Ask TLI.");
231 assert(0 && "Value type is overloaded.");
233 assert(0 && "getSizeInBits called on extended MVT.");
237 case v2i8: return 16;
241 case v2i16: return 32;
248 case v2f32: return 64;
249 case f80 : return 80;
258 case v2f64: return 128;
264 case v4f64: return 256;
268 static MVT getFloatingPointVT(unsigned BitWidth) {
271 assert(false && "Bad bit width!");
283 static MVT getIntegerVT(unsigned BitWidth) {
286 return (MVT::SimpleValueType)(MVT::INVALID_SIMPLE_VALUE_TYPE);
302 static MVT getVectorVT(MVT VT, unsigned NumElements) {
303 switch (VT.SimpleTy) {
307 if (NumElements == 2) return MVT::v2i8;
308 if (NumElements == 4) return MVT::v4i8;
309 if (NumElements == 8) return MVT::v8i8;
310 if (NumElements == 16) return MVT::v16i8;
311 if (NumElements == 32) return MVT::v32i8;
314 if (NumElements == 2) return MVT::v2i16;
315 if (NumElements == 4) return MVT::v4i16;
316 if (NumElements == 8) return MVT::v8i16;
317 if (NumElements == 16) return MVT::v16i16;
320 if (NumElements == 2) return MVT::v2i32;
321 if (NumElements == 4) return MVT::v4i32;
322 if (NumElements == 8) return MVT::v8i32;
325 if (NumElements == 1) return MVT::v1i64;
326 if (NumElements == 2) return MVT::v2i64;
327 if (NumElements == 4) return MVT::v4i64;
330 if (NumElements == 2) return MVT::v2f32;
331 if (NumElements == 4) return MVT::v4f32;
332 if (NumElements == 8) return MVT::v8f32;
335 if (NumElements == 2) return MVT::v2f64;
336 if (NumElements == 4) return MVT::v4f64;
339 return (MVT::SimpleValueType)(MVT::INVALID_SIMPLE_VALUE_TYPE);
342 static MVT getIntVectorWithNumElements(unsigned NumElts) {
344 default: return (MVT::SimpleValueType)(MVT::INVALID_SIMPLE_VALUE_TYPE);
345 case 1: return MVT::v1i64;
346 case 2: return MVT::v2i32;
347 case 4: return MVT::v4i16;
348 case 8: return MVT::v8i8;
349 case 16: return MVT::v16i8;
354 struct EVT { // EVT = Extended Value Type
360 EVT() : V((MVT::SimpleValueType)(MVT::INVALID_SIMPLE_VALUE_TYPE)),
362 EVT(MVT::SimpleValueType SVT) : V(SVT), LLVMTy(0) { }
363 EVT(MVT S) : V(S), LLVMTy(0) {}
365 bool operator==(const EVT VT) const {
366 if (V.SimpleTy == VT.V.SimpleTy) {
367 if (V.SimpleTy == MVT::INVALID_SIMPLE_VALUE_TYPE)
368 return LLVMTy == VT.LLVMTy;
373 bool operator!=(const EVT VT) const {
374 if (V.SimpleTy == VT.V.SimpleTy) {
375 if (V.SimpleTy == MVT::INVALID_SIMPLE_VALUE_TYPE)
376 return LLVMTy != VT.LLVMTy;
382 /// getFloatingPointVT - Returns the EVT that represents a floating point
383 /// type with the given number of bits. There are two floating point types
384 /// with 128 bits - this returns f128 rather than ppcf128.
385 static EVT getFloatingPointVT(unsigned BitWidth) {
386 return MVT::getFloatingPointVT(BitWidth);
389 /// getIntegerVT - Returns the EVT that represents an integer with the given
391 static EVT getIntegerVT(LLVMContext &Context, unsigned BitWidth) {
392 MVT M = MVT::getIntegerVT(BitWidth);
393 if (M.SimpleTy == MVT::INVALID_SIMPLE_VALUE_TYPE)
394 return getExtendedIntegerVT(Context, BitWidth);
399 /// getVectorVT - Returns the EVT that represents a vector NumElements in
400 /// length, where each element is of type VT.
401 static EVT getVectorVT(LLVMContext &Context, EVT VT, unsigned NumElements) {
402 MVT M = MVT::getVectorVT(VT.V, NumElements);
403 if (M.SimpleTy == MVT::INVALID_SIMPLE_VALUE_TYPE)
404 return getExtendedVectorVT(Context, VT, NumElements);
409 /// getIntVectorWithNumElements - Return any integer vector type that has
410 /// the specified number of elements.
411 static EVT getIntVectorWithNumElements(LLVMContext &C, unsigned NumElts) {
412 MVT M = MVT::getIntVectorWithNumElements(NumElts);
413 if (M.SimpleTy == MVT::INVALID_SIMPLE_VALUE_TYPE)
414 return getVectorVT(C, MVT::i8, NumElts);
419 /// isSimple - Test if the given EVT is simple (as opposed to being
421 bool isSimple() const {
422 return V.SimpleTy <= MVT::LastSimpleValueType;
425 /// isExtended - Test if the given EVT is extended (as opposed to
427 bool isExtended() const {
431 /// isFloatingPoint - Return true if this is a FP, or a vector FP type.
432 bool isFloatingPoint() const {
434 ((V >= MVT::f32 && V <= MVT::ppcf128) ||
435 (V >= MVT::v2f32 && V <= MVT::v4f64)) : isExtendedFloatingPoint();
438 /// isInteger - Return true if this is an integer, or a vector integer type.
439 bool isInteger() const {
441 ((V >= MVT::FIRST_INTEGER_VALUETYPE &&
442 V <= MVT::LAST_INTEGER_VALUETYPE) ||
443 (V >= MVT::v2i8 && V <= MVT::v4i64)) : isExtendedInteger();
446 /// isVector - Return true if this is a vector value type.
447 bool isVector() const {
449 (V >= MVT::FIRST_VECTOR_VALUETYPE && V <=
450 MVT::LAST_VECTOR_VALUETYPE) :
454 /// is64BitVector - Return true if this is a 64-bit vector type.
455 bool is64BitVector() const {
457 (V==MVT::v8i8 || V==MVT::v4i16 || V==MVT::v2i32 ||
458 V==MVT::v1i64 || V==MVT::v2f32) :
459 isExtended64BitVector();
462 /// is128BitVector - Return true if this is a 128-bit vector type.
463 bool is128BitVector() const {
465 (V==MVT::v16i8 || V==MVT::v8i16 || V==MVT::v4i32 ||
466 V==MVT::v2i64 || V==MVT::v4f32 || V==MVT::v2f64) :
467 isExtended128BitVector();
470 /// is256BitVector - Return true if this is a 256-bit vector type.
471 inline bool is256BitVector() const {
473 (V==MVT::v8f32 || V==MVT::v4f64 || V==MVT::v32i8 ||
474 V==MVT::v16i16 || V==MVT::v8i32 || V==MVT::v4i64) :
475 isExtended256BitVector();
478 /// isOverloaded - Return true if this is an overloaded type for TableGen.
479 bool isOverloaded() const {
480 return (V==MVT::iAny || V==MVT::fAny || V==MVT::vAny || V==MVT::iPTRAny);
483 /// isByteSized - Return true if the bit size is a multiple of 8.
484 bool isByteSized() const {
485 return (getSizeInBits() & 7) == 0;
488 /// isRound - Return true if the size is a power-of-two number of bytes.
489 bool isRound() const {
490 unsigned BitSize = getSizeInBits();
491 return BitSize >= 8 && !(BitSize & (BitSize - 1));
494 /// bitsEq - Return true if this has the same number of bits as VT.
495 bool bitsEq(EVT VT) const {
496 return getSizeInBits() == VT.getSizeInBits();
499 /// bitsGT - Return true if this has more bits than VT.
500 bool bitsGT(EVT VT) const {
501 return getSizeInBits() > VT.getSizeInBits();
504 /// bitsGE - Return true if this has no less bits than VT.
505 bool bitsGE(EVT VT) const {
506 return getSizeInBits() >= VT.getSizeInBits();
509 /// bitsLT - Return true if this has less bits than VT.
510 bool bitsLT(EVT VT) const {
511 return getSizeInBits() < VT.getSizeInBits();
514 /// bitsLE - Return true if this has no more bits than VT.
515 bool bitsLE(EVT VT) const {
516 return getSizeInBits() <= VT.getSizeInBits();
520 /// getSimpleVT - Return the SimpleValueType held in the specified
522 MVT getSimpleVT() const {
523 assert(isSimple() && "Expected a SimpleValueType!");
527 /// getVectorElementType - Given a vector type, return the type of
529 EVT getVectorElementType() const {
530 assert(isVector() && "Invalid vector type!");
532 return V.getVectorElementType();
534 return getExtendedVectorElementType();
537 /// getVectorNumElements - Given a vector type, return the number of
538 /// elements it contains.
539 unsigned getVectorNumElements() const {
540 assert(isVector() && "Invalid vector type!");
542 return V.getVectorNumElements();
544 return getExtendedVectorNumElements();
547 /// getSizeInBits - Return the size of the specified value type in bits.
548 unsigned getSizeInBits() const {
550 return V.getSizeInBits();
552 return getExtendedSizeInBits();
555 /// getStoreSize - Return the number of bytes overwritten by a store
556 /// of the specified value type.
557 unsigned getStoreSize() const {
558 return (getSizeInBits() + 7) / 8;
561 /// getStoreSizeInBits - Return the number of bits overwritten by a store
562 /// of the specified value type.
563 unsigned getStoreSizeInBits() const {
564 return getStoreSize() * 8;
567 /// getRoundIntegerType - Rounds the bit-width of the given integer EVT up
568 /// to the nearest power of two (and at least to eight), and returns the
569 /// integer EVT with that number of bits.
570 EVT getRoundIntegerType(LLVMContext &Context) const {
571 assert(isInteger() && !isVector() && "Invalid integer type!");
572 unsigned BitWidth = getSizeInBits();
576 return getIntegerVT(Context, 1 << Log2_32_Ceil(BitWidth));
579 /// isPow2VectorType - Retuns true if the given vector is a power of 2.
580 bool isPow2VectorType() const {
581 unsigned NElts = getVectorNumElements();
582 return !(NElts & (NElts - 1));
585 /// getPow2VectorType - Widens the length of the given vector EVT up to
586 /// the nearest power of 2 and returns that type.
587 EVT getPow2VectorType(LLVMContext &Context) const {
588 if (!isPow2VectorType()) {
589 unsigned NElts = getVectorNumElements();
590 unsigned Pow2NElts = 1 << Log2_32_Ceil(NElts);
591 return EVT::getVectorVT(Context, getVectorElementType(), Pow2NElts);
598 /// getEVTString - This function returns value type as a string,
600 std::string getEVTString() const;
602 /// getTypeForEVT - This method returns an LLVM type corresponding to the
603 /// specified EVT. For integer types, this returns an unsigned type. Note
604 /// that this will abort for types that cannot be represented.
605 const Type *getTypeForEVT(LLVMContext &Context) const;
607 /// getEVT - Return the value type corresponding to the specified type.
608 /// This returns all pointers as iPTR. If HandleUnknown is true, unknown
609 /// types are returned as Other, otherwise they are invalid.
610 static EVT getEVT(const Type *Ty, bool HandleUnknown = false);
612 intptr_t getRawBits() {
613 if (V.SimpleTy <= MVT::LastSimpleValueType)
616 return (intptr_t)(LLVMTy);
619 /// compareRawBits - A meaningless but well-behaved order, useful for
620 /// constructing containers.
621 struct compareRawBits {
622 bool operator()(EVT L, EVT R) const {
623 if (L.V.SimpleTy == R.V.SimpleTy)
624 return L.LLVMTy < R.LLVMTy;
626 return L.V.SimpleTy < R.V.SimpleTy;
631 // Methods for handling the Extended-type case in functions above.
632 // These are all out-of-line to prevent users of this header file
633 // from having a dependency on Type.h.
634 static EVT getExtendedIntegerVT(LLVMContext &C, unsigned BitWidth);
635 static EVT getExtendedVectorVT(LLVMContext &C, EVT VT,
636 unsigned NumElements);
637 bool isExtendedFloatingPoint() const;
638 bool isExtendedInteger() const;
639 bool isExtendedVector() const;
640 bool isExtended64BitVector() const;
641 bool isExtended128BitVector() const;
642 bool isExtended256BitVector() const;
643 EVT getExtendedVectorElementType() const;
644 unsigned getExtendedVectorNumElements() const;
645 unsigned getExtendedSizeInBits() const;
648 } // End llvm namespace