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 struct MVT { // MVT = Machine Value Type
30 enum SimpleValueType {
31 // If you change this numbering, you must change the values in
32 // ValueTypes.td as well!
33 Other = 0, // This is a non-standard value
34 i1 = 1, // This is a 1 bit integer value
35 i8 = 2, // This is an 8 bit integer value
36 i16 = 3, // This is a 16 bit integer value
37 i32 = 4, // This is a 32 bit integer value
38 i64 = 5, // This is a 64 bit integer value
39 i128 = 6, // This is a 128 bit integer value
41 FIRST_INTEGER_VALUETYPE = i1,
42 LAST_INTEGER_VALUETYPE = i128,
44 f32 = 7, // This is a 32 bit floating point value
45 f64 = 8, // This is a 64 bit floating point value
46 f80 = 9, // This is a 80 bit floating point value
47 f128 = 10, // This is a 128 bit floating point value
48 ppcf128 = 11, // This is a PPC 128-bit floating point value
49 Flag = 12, // This is a condition code or machine flag.
51 isVoid = 13, // This has no value
56 v16i8 = 17, // 16 x i8
57 v32i8 = 18, // 32 x i8
58 v2i16 = 19, // 2 x i16
59 v4i16 = 20, // 4 x i16
60 v8i16 = 21, // 8 x i16
61 v16i16 = 22, // 16 x i16
62 v2i32 = 23, // 2 x i32
63 v4i32 = 24, // 4 x i32
64 v8i32 = 25, // 8 x i32
65 v1i64 = 26, // 1 x i64
66 v2i64 = 27, // 2 x i64
67 v4i64 = 28, // 4 x i64
69 v2f32 = 29, // 2 x f32
70 v4f32 = 30, // 4 x f32
71 v8f32 = 31, // 8 x f32
72 v2f64 = 32, // 2 x f64
73 v4f64 = 33, // 4 x f64
75 FIRST_VECTOR_VALUETYPE = v2i8,
76 LAST_VECTOR_VALUETYPE = v4f64,
78 LAST_VALUETYPE = 34, // This always remains at the end of the list.
80 // This is the current maximum for LAST_VALUETYPE.
81 // EVT::MAX_ALLOWED_VALUETYPE is used for asserts and to size bit vectors
82 // This value must be a multiple of 32.
83 MAX_ALLOWED_VALUETYPE = 64,
85 // Metadata - This is MDNode or MDString.
88 // iPTRAny - An int value the size of the pointer of the current
89 // target to any address space. This must only be used internal to
90 // tblgen. Other than for overloading, we treat iPTRAny the same as iPTR.
93 // vAny - A vector with any length and element size. This is used
94 // for intrinsics that have overloadings based on vector types.
95 // This is only for tblgen's consumption!
98 // fAny - Any floating-point or vector floating-point value. This is used
99 // for intrinsics that have overloadings based on floating-point types.
100 // This is only for tblgen's consumption!
103 // iAny - An integer or vector integer value of any bit width. This is
104 // used for intrinsics that have overloadings based on integer bit widths.
105 // This is only for tblgen's consumption!
108 // iPTR - An int value the size of the pointer of the current
109 // target. This should only be used internal to tblgen!
112 // LastSimpleValueType - The greatest valid SimpleValueType value.
113 LastSimpleValueType = 255
116 SimpleValueType SimpleTy;
118 MVT() : SimpleTy((SimpleValueType)(LastSimpleValueType+1)) {}
119 MVT(SimpleValueType SVT) : SimpleTy(SVT) { }
121 bool operator>(const MVT& S) const { return SimpleTy > S.SimpleTy; }
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; }
127 /// isFloatingPoint - Return true if this is a FP, or a vector FP type.
128 bool isFloatingPoint() const {
129 return ((SimpleTy >= MVT::f32 && SimpleTy <= MVT::ppcf128) ||
130 (SimpleTy >= MVT::v2f32 && SimpleTy <= MVT::v4f64));
133 /// isInteger - Return true if this is an integer, or a vector integer type.
134 bool isInteger() const {
135 return ((SimpleTy >= MVT::FIRST_INTEGER_VALUETYPE &&
136 SimpleTy <= MVT::LAST_INTEGER_VALUETYPE) ||
137 (SimpleTy >= MVT::v2i8 && SimpleTy <= MVT::v4i64));
140 /// isVector - Return true if this is a vector value type.
141 bool isVector() const {
142 return (SimpleTy >= MVT::FIRST_VECTOR_VALUETYPE &&
143 SimpleTy <= MVT::LAST_VECTOR_VALUETYPE);
146 MVT getVectorElementType() const {
149 return (MVT::SimpleValueType)(MVT::LastSimpleValueType+1);
154 case v32i8: return i8;
158 case v16i16: return i16;
161 case v8i32: return i32;
164 case v4i64: return i64;
167 case v8f32: return f32;
169 case v4f64: return f64;
173 unsigned getVectorNumElements() const {
177 case v32i8: return 32;
179 case v16i16: return 16;
183 case v8f32: return 8;
189 case v4f64: return 4;
195 case v2f64: return 2;
196 case v1i64: return 1;
200 unsigned getSizeInBits() const {
203 assert(0 && "Value type size is target-dependent. Ask TLI.");
207 assert(0 && "Value type is overloaded.");
209 assert(0 && "getSizeInBits called on extended MVT.");
213 case v2i8: return 16;
217 case v2i16: return 32;
224 case v2f32: return 64;
225 case f80 : return 80;
234 case v2f64: return 128;
240 case v4f64: return 256;
244 static MVT getFloatingPointVT(unsigned BitWidth) {
247 assert(false && "Bad bit width!");
259 static MVT getIntegerVT(unsigned BitWidth) {
262 return (MVT::SimpleValueType)(MVT::LastSimpleValueType+1);
278 static MVT getVectorVT(MVT VT, unsigned NumElements) {
279 switch (VT.SimpleTy) {
283 if (NumElements == 2) return MVT::v2i8;
284 if (NumElements == 4) return MVT::v4i8;
285 if (NumElements == 8) return MVT::v8i8;
286 if (NumElements == 16) return MVT::v16i8;
287 if (NumElements == 32) return MVT::v32i8;
290 if (NumElements == 2) return MVT::v2i16;
291 if (NumElements == 4) return MVT::v4i16;
292 if (NumElements == 8) return MVT::v8i16;
293 if (NumElements == 16) return MVT::v16i16;
296 if (NumElements == 2) return MVT::v2i32;
297 if (NumElements == 4) return MVT::v4i32;
298 if (NumElements == 8) return MVT::v8i32;
301 if (NumElements == 1) return MVT::v1i64;
302 if (NumElements == 2) return MVT::v2i64;
303 if (NumElements == 4) return MVT::v4i64;
306 if (NumElements == 2) return MVT::v2f32;
307 if (NumElements == 4) return MVT::v4f32;
308 if (NumElements == 8) return MVT::v8f32;
311 if (NumElements == 2) return MVT::v2f64;
312 if (NumElements == 4) return MVT::v4f64;
315 return (MVT::SimpleValueType)(MVT::LastSimpleValueType+1);
318 static MVT getIntVectorWithNumElements(unsigned NumElts) {
320 default: return (MVT::SimpleValueType)(MVT::LastSimpleValueType+1);
321 case 1: return MVT::v1i64;
322 case 2: return MVT::v2i32;
323 case 4: return MVT::v4i16;
324 case 8: return MVT::v8i8;
325 case 16: return MVT::v16i8;
330 struct EVT { // EVT = Extended Value Type
336 EVT() : V((MVT::SimpleValueType)(MVT::LastSimpleValueType+1)) {}
337 EVT(MVT::SimpleValueType SVT) : V(SVT) { }
340 bool operator==(const EVT VT) const {
341 if (V.SimpleTy == VT.V.SimpleTy) {
342 if (V.SimpleTy == MVT::LastSimpleValueType+1)
343 return LLVMTy == VT.LLVMTy;
348 bool operator!=(const EVT VT) const {
349 if (V.SimpleTy == VT.V.SimpleTy) {
350 if (V.SimpleTy == MVT::LastSimpleValueType+1)
351 return LLVMTy != VT.LLVMTy;
357 /// getFloatingPointVT - Returns the EVT that represents a floating point
358 /// type with the given number of bits. There are two floating point types
359 /// with 128 bits - this returns f128 rather than ppcf128.
360 static EVT getFloatingPointVT(unsigned BitWidth) {
361 return MVT::getFloatingPointVT(BitWidth);
364 /// getIntegerVT - Returns the EVT that represents an integer with the given
366 static EVT getIntegerVT(unsigned BitWidth) {
367 MVT M = MVT::getIntegerVT(BitWidth);
368 if (M.SimpleTy == MVT::LastSimpleValueType+1)
369 return getExtendedIntegerVT(BitWidth);
374 /// getVectorVT - Returns the EVT that represents a vector NumElements in
375 /// length, where each element is of type VT.
376 static EVT getVectorVT(EVT VT, unsigned NumElements) {
377 MVT M = MVT::getVectorVT(VT.V, NumElements);
378 if (M.SimpleTy == MVT::LastSimpleValueType+1)
379 return getExtendedVectorVT(VT, NumElements);
384 /// getIntVectorWithNumElements - Return any integer vector type that has
385 /// the specified number of elements.
386 static EVT getIntVectorWithNumElements(unsigned NumElts) {
387 MVT M = MVT::getIntVectorWithNumElements(NumElts);
388 if (M.SimpleTy == MVT::LastSimpleValueType+1)
389 return getVectorVT(EVT(MVT::i8), NumElts);
394 /// isSimple - Test if the given EVT is simple (as opposed to being
396 bool isSimple() const {
397 return V.SimpleTy <= MVT::LastSimpleValueType;
400 /// isExtended - Test if the given EVT is extended (as opposed to
402 bool isExtended() const {
406 /// isFloatingPoint - Return true if this is a FP, or a vector FP type.
407 bool isFloatingPoint() const {
409 ((V >= MVT::f32 && V <= MVT::ppcf128) ||
410 (V >= MVT::v2f32 && V <= MVT::v4f64)) : isExtendedFloatingPoint();
413 /// isInteger - Return true if this is an integer, or a vector integer type.
414 bool isInteger() const {
416 ((V >= MVT::FIRST_INTEGER_VALUETYPE &&
417 V <= MVT::LAST_INTEGER_VALUETYPE) ||
418 (V >= MVT::v2i8 && V <= MVT::v4i64)) : isExtendedInteger();
421 /// isVector - Return true if this is a vector value type.
422 bool isVector() const {
424 (V >= MVT::FIRST_VECTOR_VALUETYPE && V <=
425 MVT::LAST_VECTOR_VALUETYPE) :
429 /// is64BitVector - Return true if this is a 64-bit vector type.
430 bool is64BitVector() const {
432 (V==MVT::v8i8 || V==MVT::v4i16 || V==MVT::v2i32 ||
433 V==MVT::v1i64 || V==MVT::v2f32) :
434 isExtended64BitVector();
437 /// is128BitVector - Return true if this is a 128-bit vector type.
438 bool is128BitVector() const {
440 (V==MVT::v16i8 || V==MVT::v8i16 || V==MVT::v4i32 ||
441 V==MVT::v2i64 || V==MVT::v4f32 || V==MVT::v2f64) :
442 isExtended128BitVector();
445 /// is256BitVector - Return true if this is a 256-bit vector type.
446 inline bool is256BitVector() const {
448 (V==MVT::v8f32 || V==MVT::v4f64 || V==MVT::v32i8 ||
449 V==MVT::v16i16 || V==MVT::v8i32 || V==MVT::v4i64) :
450 isExtended256BitVector();
453 /// isOverloaded - Return true if this is an overloaded type for TableGen.
454 bool isOverloaded() const {
455 return (V==MVT::iAny || V==MVT::fAny || V==MVT::vAny || V==MVT::iPTRAny);
458 /// isByteSized - Return true if the bit size is a multiple of 8.
459 bool isByteSized() const {
460 return (getSizeInBits() & 7) == 0;
463 /// isRound - Return true if the size is a power-of-two number of bytes.
464 bool isRound() const {
465 unsigned BitSize = getSizeInBits();
466 return BitSize >= 8 && !(BitSize & (BitSize - 1));
469 /// bitsEq - Return true if this has the same number of bits as VT.
470 bool bitsEq(EVT VT) const {
471 return getSizeInBits() == VT.getSizeInBits();
474 /// bitsGT - Return true if this has more bits than VT.
475 bool bitsGT(EVT VT) const {
476 return getSizeInBits() > VT.getSizeInBits();
479 /// bitsGE - Return true if this has no less bits than VT.
480 bool bitsGE(EVT VT) const {
481 return getSizeInBits() >= VT.getSizeInBits();
484 /// bitsLT - Return true if this has less bits than VT.
485 bool bitsLT(EVT VT) const {
486 return getSizeInBits() < VT.getSizeInBits();
489 /// bitsLE - Return true if this has no more bits than VT.
490 bool bitsLE(EVT VT) const {
491 return getSizeInBits() <= VT.getSizeInBits();
495 /// getSimpleVT - Return the SimpleValueType held in the specified
497 MVT getSimpleVT() const {
498 assert(isSimple() && "Expected a SimpleValueType!");
502 /// getVectorElementType - Given a vector type, return the type of
504 EVT getVectorElementType() const {
505 assert(isVector() && "Invalid vector type!");
507 return V.getVectorElementType();
509 return getExtendedVectorElementType();
512 /// getVectorNumElements - Given a vector type, return the number of
513 /// elements it contains.
514 unsigned getVectorNumElements() const {
515 assert(isVector() && "Invalid vector type!");
517 return V.getVectorNumElements();
519 return getExtendedVectorNumElements();
522 /// getSizeInBits - Return the size of the specified value type in bits.
523 unsigned getSizeInBits() const {
525 return V.getSizeInBits();
527 return getExtendedSizeInBits();
530 /// getStoreSizeInBits - Return the number of bits overwritten by a store
531 /// of the specified value type.
532 unsigned getStoreSizeInBits() const {
533 return (getSizeInBits() + 7)/8*8;
536 /// getRoundIntegerType - Rounds the bit-width of the given integer EVT up
537 /// to the nearest power of two (and at least to eight), and returns the
538 /// integer EVT with that number of bits.
539 EVT getRoundIntegerType() const {
540 assert(isInteger() && !isVector() && "Invalid integer type!");
541 unsigned BitWidth = getSizeInBits();
545 return getIntegerVT(1 << Log2_32_Ceil(BitWidth));
548 /// isPow2VectorType - Retuns true if the given vector is a power of 2.
549 bool isPow2VectorType() const {
550 unsigned NElts = getVectorNumElements();
551 return !(NElts & (NElts - 1));
554 /// getPow2VectorType - Widens the length of the given vector EVT up to
555 /// the nearest power of 2 and returns that type.
556 EVT getPow2VectorType() const {
557 if (!isPow2VectorType()) {
558 unsigned NElts = getVectorNumElements();
559 unsigned Pow2NElts = 1 << Log2_32_Ceil(NElts);
560 return EVT::getVectorVT(getVectorElementType(), Pow2NElts);
567 /// getEVTString - This function returns value type as a string,
569 std::string getEVTString() const;
571 /// getTypeForEVT - This method returns an LLVM type corresponding to the
572 /// specified EVT. For integer types, this returns an unsigned type. Note
573 /// that this will abort for types that cannot be represented.
574 const Type *getTypeForEVT() const;
576 /// getEVT - Return the value type corresponding to the specified type.
577 /// This returns all pointers as iPTR. If HandleUnknown is true, unknown
578 /// types are returned as Other, otherwise they are invalid.
579 static EVT getEVT(const Type *Ty, bool HandleUnknown = false);
581 intptr_t getRawBits() {
582 if (V.SimpleTy <= MVT::LastSimpleValueType)
585 return (intptr_t)(LLVMTy);
588 /// compareRawBits - A meaningless but well-behaved order, useful for
589 /// constructing containers.
590 struct compareRawBits {
591 bool operator()(EVT L, EVT R) const {
592 if (L.V.SimpleTy == R.V.SimpleTy)
593 return L.LLVMTy < R.LLVMTy;
595 return L.V.SimpleTy < R.V.SimpleTy;
600 // Methods for handling the Extended-type case in functions above.
601 // These are all out-of-line to prevent users of this header file
602 // from having a dependency on Type.h.
603 static EVT getExtendedIntegerVT(unsigned BitWidth);
604 static EVT getExtendedVectorVT(EVT VT, unsigned NumElements);
605 bool isExtendedFloatingPoint() const;
606 bool isExtendedInteger() const;
607 bool isExtendedVector() const;
608 bool isExtended64BitVector() const;
609 bool isExtended128BitVector() const;
610 bool isExtended256BitVector() const;
611 EVT getExtendedVectorElementType() const;
612 unsigned getExtendedVectorNumElements() const;
613 unsigned getExtendedSizeInBits() const;
616 } // End llvm namespace