Split EVT into MVT and EVT, the former representing _just_ a primitive type, while
[oota-llvm.git] / include / llvm / CodeGen / ValueTypes.h
1 //===- CodeGen/ValueTypes.h - Low-Level Target independ. types --*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
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.
13 //
14 //===----------------------------------------------------------------------===//
15
16 #ifndef LLVM_CODEGEN_VALUETYPES_H
17 #define LLVM_CODEGEN_VALUETYPES_H
18
19 #include <cassert>
20 #include <string>
21 #include "llvm/Support/DataTypes.h"
22 #include "llvm/Support/MathExtras.h"
23
24 namespace llvm {
25   class Type;
26   class LLVMContext;
27   struct EVT;
28
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
40
41       FIRST_INTEGER_VALUETYPE = i1,
42       LAST_INTEGER_VALUETYPE  = i128,
43
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.
50
51       isVoid         =  13,   // This has no value
52
53       v2i8           =  14,   //  2 x i8
54       v4i8           =  15,   //  4 x i8
55       v8i8           =  16,   //  8 x i8
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
68
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
74
75       FIRST_VECTOR_VALUETYPE = v2i8,
76       LAST_VECTOR_VALUETYPE  = v4f64,
77
78       LAST_VALUETYPE =  34,   // This always remains at the end of the list.
79
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,
84
85       // Metadata - This is MDNode or MDString.
86       Metadata       = 250,
87
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.
91       iPTRAny        = 251,
92
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!
96       vAny           = 252,
97
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!
101       fAny           = 253,
102
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!
106       iAny           = 254,
107
108       // iPTR - An int value the size of the pointer of the current
109       // target.  This should only be used internal to tblgen!
110       iPTR           = 255,
111
112       // LastSimpleValueType - The greatest valid SimpleValueType value.
113       LastSimpleValueType = 255
114     };
115
116     SimpleValueType SimpleTy;
117
118     MVT() : SimpleTy((SimpleValueType)(LastSimpleValueType+1)) {}
119     MVT(SimpleValueType SVT) : SimpleTy(SVT) { }
120     
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; }
126     
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));
131     }
132
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));
138     }
139
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);
144     }
145     
146     MVT getVectorElementType() const {
147       switch (SimpleTy) {
148       default:
149         return (MVT::SimpleValueType)(MVT::LastSimpleValueType+1);
150       case v2i8 :
151       case v4i8 :
152       case v8i8 :
153       case v16i8:
154       case v32i8: return i8;
155       case v2i16:
156       case v4i16:
157       case v8i16:
158       case v16i16: return i16;
159       case v2i32:
160       case v4i32:
161       case v8i32: return i32;
162       case v1i64:
163       case v2i64:
164       case v4i64: return i64;
165       case v2f32:
166       case v4f32:
167       case v8f32: return f32;
168       case v2f64:
169       case v4f64: return f64;
170       }
171     }
172     
173     unsigned getVectorNumElements() const {
174       switch (SimpleTy) {
175       default:
176         return ~0U;
177       case v32i8: return 32;
178       case v16i8:
179       case v16i16: return 16;
180       case v8i8 :
181       case v8i16:
182       case v8i32:
183       case v8f32: return 8;
184       case v4i8:
185       case v4i16:
186       case v4i32:
187       case v4i64:
188       case v4f32:
189       case v4f64: return 4;
190       case v2i8:
191       case v2i16:
192       case v2i32:
193       case v2i64:
194       case v2f32:
195       case v2f64: return 2;
196       case v1i64: return 1;
197       }
198     }
199     
200     unsigned getSizeInBits() const {
201       switch (SimpleTy) {
202       case iPTR:
203         assert(0 && "Value type size is target-dependent. Ask TLI.");
204       case iPTRAny:
205       case iAny:
206       case fAny:
207         assert(0 && "Value type is overloaded.");
208       default:
209         assert(0 && "getSizeInBits called on extended MVT.");
210       case i1  :  return 1;
211       case i8  :  return 8;
212       case i16 :
213       case v2i8:  return 16;
214       case f32 :
215       case i32 :
216       case v4i8:
217       case v2i16: return 32;
218       case f64 :
219       case i64 :
220       case v8i8:
221       case v4i16:
222       case v2i32:
223       case v1i64:
224       case v2f32: return 64;
225       case f80 :  return 80;
226       case f128:
227       case ppcf128:
228       case i128:
229       case v16i8:
230       case v8i16:
231       case v4i32:
232       case v2i64:
233       case v4f32:
234       case v2f64: return 128;
235       case v32i8:
236       case v16i16:
237       case v8i32:
238       case v4i64:
239       case v8f32:
240       case v4f64: return 256;
241       }
242     }
243     
244     static MVT getFloatingPointVT(unsigned BitWidth) {
245       switch (BitWidth) {
246       default:
247         assert(false && "Bad bit width!");
248       case 32:
249         return MVT::f32;
250       case 64:
251         return MVT::f64;
252       case 80:
253         return MVT::f80;
254       case 128:
255         return MVT::f128;
256       }
257     }
258     
259     static MVT getIntegerVT(unsigned BitWidth) {
260       switch (BitWidth) {
261       default:
262         return (MVT::SimpleValueType)(MVT::LastSimpleValueType+1);
263       case 1:
264         return MVT::i1;
265       case 8:
266         return MVT::i8;
267       case 16:
268         return MVT::i16;
269       case 32:
270         return MVT::i32;
271       case 64:
272         return MVT::i64;
273       case 128:
274         return MVT::i128;
275       }
276     }
277     
278     static MVT getVectorVT(MVT VT, unsigned NumElements) {
279       switch (VT.SimpleTy) {
280       default:
281         break;
282       case MVT::i8:
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;
288         break;
289       case MVT::i16:
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;
294         break;
295       case MVT::i32:
296         if (NumElements == 2)  return MVT::v2i32;
297         if (NumElements == 4)  return MVT::v4i32;
298         if (NumElements == 8)  return MVT::v8i32;
299         break;
300       case MVT::i64:
301         if (NumElements == 1)  return MVT::v1i64;
302         if (NumElements == 2)  return MVT::v2i64;
303         if (NumElements == 4)  return MVT::v4i64;
304         break;
305       case MVT::f32:
306         if (NumElements == 2)  return MVT::v2f32;
307         if (NumElements == 4)  return MVT::v4f32;
308         if (NumElements == 8)  return MVT::v8f32;
309         break;
310       case MVT::f64:
311         if (NumElements == 2)  return MVT::v2f64;
312         if (NumElements == 4)  return MVT::v4f64;
313         break;
314       }
315       return (MVT::SimpleValueType)(MVT::LastSimpleValueType+1);
316     }
317     
318     static MVT getIntVectorWithNumElements(unsigned NumElts) {
319       switch (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;
326       }
327     }
328   };
329
330   struct EVT { // EVT = Extended Value Type
331   private:
332     MVT V;
333     const Type *LLVMTy;
334
335   public:
336     EVT() : V((MVT::SimpleValueType)(MVT::LastSimpleValueType+1)) {}
337     EVT(MVT::SimpleValueType SVT) : V(SVT) { }
338     EVT(MVT S) : V(S) {}
339
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;
344         return true;
345       }
346       return false;
347     }
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;
352         return false;
353       }
354       return true;
355     }
356
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);
362     }
363
364     /// getIntegerVT - Returns the EVT that represents an integer with the given
365     /// number of bits.
366     static EVT getIntegerVT(unsigned BitWidth) {
367       MVT M = MVT::getIntegerVT(BitWidth);
368       if (M.SimpleTy == MVT::LastSimpleValueType+1)
369         return getExtendedIntegerVT(BitWidth);
370       else
371         return M;
372     }
373
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);
380       else
381         return M;
382     }
383
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);
390       else
391         return M;
392     }
393
394     /// isSimple - Test if the given EVT is simple (as opposed to being
395     /// extended).
396     bool isSimple() const {
397       return V.SimpleTy <= MVT::LastSimpleValueType;
398     }
399
400     /// isExtended - Test if the given EVT is extended (as opposed to
401     /// being simple).
402     bool isExtended() const {
403       return !isSimple();
404     }
405
406     /// isFloatingPoint - Return true if this is a FP, or a vector FP type.
407     bool isFloatingPoint() const {
408       return isSimple() ?
409              ((V >= MVT::f32 && V <= MVT::ppcf128) ||
410               (V >= MVT::v2f32 && V <= MVT::v4f64)) : isExtendedFloatingPoint();
411     }
412
413     /// isInteger - Return true if this is an integer, or a vector integer type.
414     bool isInteger() const {
415       return isSimple() ?
416              ((V >= MVT::FIRST_INTEGER_VALUETYPE &&
417                V <= MVT::LAST_INTEGER_VALUETYPE) ||
418               (V >= MVT::v2i8 && V <= MVT::v4i64)) : isExtendedInteger();
419     }
420
421     /// isVector - Return true if this is a vector value type.
422     bool isVector() const {
423       return isSimple() ?
424              (V >= MVT::FIRST_VECTOR_VALUETYPE && V <= 
425                    MVT::LAST_VECTOR_VALUETYPE) :
426              isExtendedVector();
427     }
428
429     /// is64BitVector - Return true if this is a 64-bit vector type.
430     bool is64BitVector() const {
431       return isSimple() ?
432              (V==MVT::v8i8 || V==MVT::v4i16 || V==MVT::v2i32 ||
433               V==MVT::v1i64 || V==MVT::v2f32) :
434              isExtended64BitVector();
435     }
436
437     /// is128BitVector - Return true if this is a 128-bit vector type.
438     bool is128BitVector() const {
439       return isSimple() ?
440              (V==MVT::v16i8 || V==MVT::v8i16 || V==MVT::v4i32 ||
441               V==MVT::v2i64 || V==MVT::v4f32 || V==MVT::v2f64) :
442              isExtended128BitVector();
443     }
444
445     /// is256BitVector - Return true if this is a 256-bit vector type.
446     inline bool is256BitVector() const {
447       return isSimple() ?
448              (V==MVT::v8f32 || V==MVT::v4f64 || V==MVT::v32i8 ||
449               V==MVT::v16i16 || V==MVT::v8i32 || V==MVT::v4i64) : 
450             isExtended256BitVector();
451     }
452
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);
456     }
457
458     /// isByteSized - Return true if the bit size is a multiple of 8.
459     bool isByteSized() const {
460       return (getSizeInBits() & 7) == 0;
461     }
462
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));
467     }
468
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();
472     }
473
474     /// bitsGT - Return true if this has more bits than VT.
475     bool bitsGT(EVT VT) const {
476       return getSizeInBits() > VT.getSizeInBits();
477     }
478
479     /// bitsGE - Return true if this has no less bits than VT.
480     bool bitsGE(EVT VT) const {
481       return getSizeInBits() >= VT.getSizeInBits();
482     }
483
484     /// bitsLT - Return true if this has less bits than VT.
485     bool bitsLT(EVT VT) const {
486       return getSizeInBits() < VT.getSizeInBits();
487     }
488
489     /// bitsLE - Return true if this has no more bits than VT.
490     bool bitsLE(EVT VT) const {
491       return getSizeInBits() <= VT.getSizeInBits();
492     }
493
494
495     /// getSimpleVT - Return the SimpleValueType held in the specified
496     /// simple EVT.
497     MVT getSimpleVT() const {
498       assert(isSimple() && "Expected a SimpleValueType!");
499       return V;
500     }
501
502     /// getVectorElementType - Given a vector type, return the type of
503     /// each element.
504     EVT getVectorElementType() const {
505       assert(isVector() && "Invalid vector type!");
506       if (isSimple())
507         return V.getVectorElementType();
508       else
509         return getExtendedVectorElementType();
510     }
511
512     /// getVectorNumElements - Given a vector type, return the number of
513     /// elements it contains.
514     unsigned getVectorNumElements() const {
515       assert(isVector() && "Invalid vector type!");
516       if (isSimple())
517         return V.getVectorNumElements();
518       else
519         return getExtendedVectorNumElements();
520     }
521
522     /// getSizeInBits - Return the size of the specified value type in bits.
523     unsigned getSizeInBits() const {
524       if (isSimple())
525         return V.getSizeInBits();
526       else
527         return getExtendedSizeInBits();
528     }
529
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;
534     }
535
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();
542       if (BitWidth <= 8)
543         return EVT(MVT::i8);
544       else
545         return getIntegerVT(1 << Log2_32_Ceil(BitWidth));
546     }
547
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));
552     }
553
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);
561       }
562       else {
563         return *this;
564       }
565     }
566
567     /// getEVTString - This function returns value type as a string,
568     /// e.g. "i32".
569     std::string getEVTString() const;
570
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;
575
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);
580
581     intptr_t getRawBits() {
582       if (V.SimpleTy <= MVT::LastSimpleValueType)
583         return V.SimpleTy;
584       else
585         return (intptr_t)(LLVMTy);
586     }
587
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;
594         else
595           return L.V.SimpleTy < R.V.SimpleTy;
596       }
597     };
598
599   private:
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;
614   };
615
616 } // End llvm namespace
617
618 #endif