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 /// isPow2VectorType - Retuns true if the given vector is a power of 2.
148 bool isPow2VectorType() const {
149 unsigned NElts = getVectorNumElements();
150 return !(NElts & (NElts - 1));
153 /// getPow2VectorType - Widens the length of the given vector EVT up to
154 /// the nearest power of 2 and returns that type.
155 MVT getPow2VectorType() const {
156 if (!isPow2VectorType()) {
157 unsigned NElts = getVectorNumElements();
158 unsigned Pow2NElts = 1 << Log2_32_Ceil(NElts);
159 return MVT::getVectorVT(getVectorElementType(), Pow2NElts);
166 MVT getVectorElementType() const {
169 return (MVT::SimpleValueType)(MVT::LastSimpleValueType+1);
174 case v32i8: return i8;
178 case v16i16: return i16;
181 case v8i32: return i32;
184 case v4i64: return i64;
187 case v8f32: return f32;
189 case v4f64: return f64;
193 unsigned getVectorNumElements() const {
197 case v32i8: return 32;
199 case v16i16: return 16;
203 case v8f32: return 8;
209 case v4f64: return 4;
215 case v2f64: return 2;
216 case v1i64: return 1;
220 unsigned getSizeInBits() const {
223 assert(0 && "Value type size is target-dependent. Ask TLI.");
227 assert(0 && "Value type is overloaded.");
229 assert(0 && "getSizeInBits called on extended MVT.");
233 case v2i8: return 16;
237 case v2i16: return 32;
244 case v2f32: return 64;
245 case f80 : return 80;
254 case v2f64: return 128;
260 case v4f64: return 256;
264 static MVT getFloatingPointVT(unsigned BitWidth) {
267 assert(false && "Bad bit width!");
279 static MVT getIntegerVT(unsigned BitWidth) {
282 return (MVT::SimpleValueType)(MVT::LastSimpleValueType+1);
298 static MVT getVectorVT(MVT VT, unsigned NumElements) {
299 switch (VT.SimpleTy) {
303 if (NumElements == 2) return MVT::v2i8;
304 if (NumElements == 4) return MVT::v4i8;
305 if (NumElements == 8) return MVT::v8i8;
306 if (NumElements == 16) return MVT::v16i8;
307 if (NumElements == 32) return MVT::v32i8;
310 if (NumElements == 2) return MVT::v2i16;
311 if (NumElements == 4) return MVT::v4i16;
312 if (NumElements == 8) return MVT::v8i16;
313 if (NumElements == 16) return MVT::v16i16;
316 if (NumElements == 2) return MVT::v2i32;
317 if (NumElements == 4) return MVT::v4i32;
318 if (NumElements == 8) return MVT::v8i32;
321 if (NumElements == 1) return MVT::v1i64;
322 if (NumElements == 2) return MVT::v2i64;
323 if (NumElements == 4) return MVT::v4i64;
326 if (NumElements == 2) return MVT::v2f32;
327 if (NumElements == 4) return MVT::v4f32;
328 if (NumElements == 8) return MVT::v8f32;
331 if (NumElements == 2) return MVT::v2f64;
332 if (NumElements == 4) return MVT::v4f64;
335 return (MVT::SimpleValueType)(MVT::LastSimpleValueType+1);
338 static MVT getIntVectorWithNumElements(unsigned NumElts) {
340 default: return (MVT::SimpleValueType)(MVT::LastSimpleValueType+1);
341 case 1: return MVT::v1i64;
342 case 2: return MVT::v2i32;
343 case 4: return MVT::v4i16;
344 case 8: return MVT::v8i8;
345 case 16: return MVT::v16i8;
350 struct EVT { // EVT = Extended Value Type
356 EVT() : V((MVT::SimpleValueType)(MVT::LastSimpleValueType+1)), LLVMTy(0) {}
357 EVT(MVT::SimpleValueType SVT) : V(SVT), LLVMTy(0) { }
358 EVT(MVT S) : V(S), LLVMTy(0) {}
360 bool operator==(const EVT VT) const {
361 if (V.SimpleTy == VT.V.SimpleTy) {
362 if (V.SimpleTy == MVT::LastSimpleValueType+1)
363 return LLVMTy == VT.LLVMTy;
368 bool operator!=(const EVT VT) const {
369 if (V.SimpleTy == VT.V.SimpleTy) {
370 if (V.SimpleTy == MVT::LastSimpleValueType+1)
371 return LLVMTy != VT.LLVMTy;
377 /// getFloatingPointVT - Returns the EVT that represents a floating point
378 /// type with the given number of bits. There are two floating point types
379 /// with 128 bits - this returns f128 rather than ppcf128.
380 static EVT getFloatingPointVT(unsigned BitWidth) {
381 return MVT::getFloatingPointVT(BitWidth);
384 /// getIntegerVT - Returns the EVT that represents an integer with the given
386 static EVT getIntegerVT(LLVMContext &Context, unsigned BitWidth) {
387 MVT M = MVT::getIntegerVT(BitWidth);
388 if (M.SimpleTy == MVT::LastSimpleValueType+1)
389 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::LastSimpleValueType+1)
399 return getExtendedVectorVT(Context, VT, NumElements);
404 /// getIntVectorWithNumElements - Return any integer vector type that has
405 /// the specified number of elements.
406 static EVT getIntVectorWithNumElements(LLVMContext &C, unsigned NumElts) {
407 MVT M = MVT::getIntVectorWithNumElements(NumElts);
408 if (M.SimpleTy == MVT::LastSimpleValueType+1)
409 return getVectorVT(C, MVT::i8, NumElts);
414 /// isSimple - Test if the given EVT is simple (as opposed to being
416 bool isSimple() const {
417 return V.SimpleTy <= MVT::LastSimpleValueType;
420 /// isExtended - Test if the given EVT is extended (as opposed to
422 bool isExtended() const {
426 /// isFloatingPoint - Return true if this is a FP, or a vector FP type.
427 bool isFloatingPoint() const {
429 ((V >= MVT::f32 && V <= MVT::ppcf128) ||
430 (V >= MVT::v2f32 && V <= MVT::v4f64)) : isExtendedFloatingPoint();
433 /// isInteger - Return true if this is an integer, or a vector integer type.
434 bool isInteger() const {
436 ((V >= MVT::FIRST_INTEGER_VALUETYPE &&
437 V <= MVT::LAST_INTEGER_VALUETYPE) ||
438 (V >= MVT::v2i8 && V <= MVT::v4i64)) : isExtendedInteger();
441 /// isVector - Return true if this is a vector value type.
442 bool isVector() const {
444 (V >= MVT::FIRST_VECTOR_VALUETYPE && V <=
445 MVT::LAST_VECTOR_VALUETYPE) :
449 /// is64BitVector - Return true if this is a 64-bit vector type.
450 bool is64BitVector() const {
452 (V==MVT::v8i8 || V==MVT::v4i16 || V==MVT::v2i32 ||
453 V==MVT::v1i64 || V==MVT::v2f32) :
454 isExtended64BitVector();
457 /// is128BitVector - Return true if this is a 128-bit vector type.
458 bool is128BitVector() const {
460 (V==MVT::v16i8 || V==MVT::v8i16 || V==MVT::v4i32 ||
461 V==MVT::v2i64 || V==MVT::v4f32 || V==MVT::v2f64) :
462 isExtended128BitVector();
465 /// is256BitVector - Return true if this is a 256-bit vector type.
466 inline bool is256BitVector() const {
468 (V==MVT::v8f32 || V==MVT::v4f64 || V==MVT::v32i8 ||
469 V==MVT::v16i16 || V==MVT::v8i32 || V==MVT::v4i64) :
470 isExtended256BitVector();
473 /// isOverloaded - Return true if this is an overloaded type for TableGen.
474 bool isOverloaded() const {
475 return (V==MVT::iAny || V==MVT::fAny || V==MVT::vAny || V==MVT::iPTRAny);
478 /// isByteSized - Return true if the bit size is a multiple of 8.
479 bool isByteSized() const {
480 return (getSizeInBits() & 7) == 0;
483 /// isRound - Return true if the size is a power-of-two number of bytes.
484 bool isRound() const {
485 unsigned BitSize = getSizeInBits();
486 return BitSize >= 8 && !(BitSize & (BitSize - 1));
489 /// bitsEq - Return true if this has the same number of bits as VT.
490 bool bitsEq(EVT VT) const {
491 return getSizeInBits() == VT.getSizeInBits();
494 /// bitsGT - Return true if this has more bits than VT.
495 bool bitsGT(EVT VT) const {
496 return getSizeInBits() > VT.getSizeInBits();
499 /// bitsGE - Return true if this has no less bits than VT.
500 bool bitsGE(EVT VT) const {
501 return getSizeInBits() >= VT.getSizeInBits();
504 /// bitsLT - Return true if this has less bits than VT.
505 bool bitsLT(EVT VT) const {
506 return getSizeInBits() < VT.getSizeInBits();
509 /// bitsLE - Return true if this has no more bits than VT.
510 bool bitsLE(EVT VT) const {
511 return getSizeInBits() <= VT.getSizeInBits();
515 /// getSimpleVT - Return the SimpleValueType held in the specified
517 MVT getSimpleVT() const {
518 assert(isSimple() && "Expected a SimpleValueType!");
522 /// getVectorElementType - Given a vector type, return the type of
524 EVT getVectorElementType() const {
525 assert(isVector() && "Invalid vector type!");
527 return V.getVectorElementType();
529 return getExtendedVectorElementType();
532 /// getVectorNumElements - Given a vector type, return the number of
533 /// elements it contains.
534 unsigned getVectorNumElements() const {
535 assert(isVector() && "Invalid vector type!");
537 return V.getVectorNumElements();
539 return getExtendedVectorNumElements();
542 /// getSizeInBits - Return the size of the specified value type in bits.
543 unsigned getSizeInBits() const {
545 return V.getSizeInBits();
547 return getExtendedSizeInBits();
550 /// getStoreSizeInBits - Return the number of bits overwritten by a store
551 /// of the specified value type.
552 unsigned getStoreSizeInBits() const {
553 return (getSizeInBits() + 7)/8*8;
556 /// getRoundIntegerType - Rounds the bit-width of the given integer EVT up
557 /// to the nearest power of two (and at least to eight), and returns the
558 /// integer EVT with that number of bits.
559 EVT getRoundIntegerType(LLVMContext &Context) const {
560 assert(isInteger() && !isVector() && "Invalid integer type!");
561 unsigned BitWidth = getSizeInBits();
565 return getIntegerVT(Context, 1 << Log2_32_Ceil(BitWidth));
568 /// isPow2VectorType - Retuns true if the given vector is a power of 2.
569 bool isPow2VectorType() const {
570 unsigned NElts = getVectorNumElements();
571 return !(NElts & (NElts - 1));
574 /// getPow2VectorType - Widens the length of the given vector EVT up to
575 /// the nearest power of 2 and returns that type.
576 EVT getPow2VectorType(LLVMContext &Context) const {
577 if (!isPow2VectorType()) {
578 unsigned NElts = getVectorNumElements();
579 unsigned Pow2NElts = 1 << Log2_32_Ceil(NElts);
580 return EVT::getVectorVT(Context, getVectorElementType(), Pow2NElts);
587 /// getEVTString - This function returns value type as a string,
589 std::string getEVTString() const;
591 /// getTypeForEVT - This method returns an LLVM type corresponding to the
592 /// specified EVT. For integer types, this returns an unsigned type. Note
593 /// that this will abort for types that cannot be represented.
594 const Type *getTypeForEVT(LLVMContext &Context) const;
596 /// getEVT - Return the value type corresponding to the specified type.
597 /// This returns all pointers as iPTR. If HandleUnknown is true, unknown
598 /// types are returned as Other, otherwise they are invalid.
599 static EVT getEVT(const Type *Ty, bool HandleUnknown = false);
601 intptr_t getRawBits() {
602 if (V.SimpleTy <= MVT::LastSimpleValueType)
605 return (intptr_t)(LLVMTy);
608 /// compareRawBits - A meaningless but well-behaved order, useful for
609 /// constructing containers.
610 struct compareRawBits {
611 bool operator()(EVT L, EVT R) const {
612 if (L.V.SimpleTy == R.V.SimpleTy)
613 return L.LLVMTy < R.LLVMTy;
615 return L.V.SimpleTy < R.V.SimpleTy;
620 // Methods for handling the Extended-type case in functions above.
621 // These are all out-of-line to prevent users of this header file
622 // from having a dependency on Type.h.
623 static EVT getExtendedIntegerVT(LLVMContext &C, unsigned BitWidth);
624 static EVT getExtendedVectorVT(LLVMContext &C, EVT VT,
625 unsigned NumElements);
626 bool isExtendedFloatingPoint() const;
627 bool isExtendedInteger() const;
628 bool isExtendedVector() const;
629 bool isExtended64BitVector() const;
630 bool isExtended128BitVector() const;
631 bool isExtended256BitVector() const;
632 EVT getExtendedVectorElementType() const;
633 unsigned getExtendedVectorNumElements() const;
634 unsigned getExtendedSizeInBits() const;
637 } // End llvm namespace