Update this comment.
[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/System/DataTypes.h"
22 #include "llvm/Support/MathExtras.h"
23
24 namespace llvm {
25   class Type;
26   class LLVMContext;
27   struct EVT;
28
29   class MVT { // MVT = Machine Value Type
30   public:
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
41
42       FIRST_INTEGER_VALUETYPE = i1,
43       LAST_INTEGER_VALUETYPE  = i128,
44
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 glues nodes together during pre-RA sched
51
52       isVoid         =  13,   // This has no value
53
54       v2i8           =  14,   //  2 x i8
55       v4i8           =  15,   //  4 x i8
56       v8i8           =  16,   //  8 x i8
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
69
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
75
76       FIRST_VECTOR_VALUETYPE = v2i8,
77       LAST_VECTOR_VALUETYPE  = v4f64,
78
79       LAST_VALUETYPE =  34,   // This always remains at the end of the list.
80
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,
85
86       // Metadata - This is MDNode or MDString.
87       Metadata       = 250,
88
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.
92       iPTRAny        = 251,
93
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!
97       vAny           = 252,
98
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!
102       fAny           = 253,
103
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!
107       iAny           = 254,
108
109       // iPTR - An int value the size of the pointer of the current
110       // target.  This should only be used internal to tblgen!
111       iPTR           = 255,
112
113       // LastSimpleValueType - The greatest valid SimpleValueType value.
114       LastSimpleValueType = 255,
115
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
119     };
120
121     SimpleValueType SimpleTy;
122
123     MVT() : SimpleTy((SimpleValueType)(INVALID_SIMPLE_VALUE_TYPE)) {}
124     MVT(SimpleValueType SVT) : SimpleTy(SVT) { }
125     
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; }
131     
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));
136     }
137
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));
143     }
144
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);
149     }
150     
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));
155     }
156
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);
164       }
165       else {
166         return *this;
167       }
168     }
169
170     /// getScalarType - If this is a vector type, return the element type,
171     /// otherwise return this.
172     MVT getScalarType() const {
173       return isVector() ? getVectorElementType() : *this;
174     }
175     
176     MVT getVectorElementType() const {
177       switch (SimpleTy) {
178       default:
179         return (MVT::SimpleValueType)(MVT::INVALID_SIMPLE_VALUE_TYPE);
180       case v2i8 :
181       case v4i8 :
182       case v8i8 :
183       case v16i8:
184       case v32i8: return i8;
185       case v2i16:
186       case v4i16:
187       case v8i16:
188       case v16i16: return i16;
189       case v2i32:
190       case v4i32:
191       case v8i32: return i32;
192       case v1i64:
193       case v2i64:
194       case v4i64: return i64;
195       case v2f32:
196       case v4f32:
197       case v8f32: return f32;
198       case v2f64:
199       case v4f64: return f64;
200       }
201     }
202     
203     unsigned getVectorNumElements() const {
204       switch (SimpleTy) {
205       default:
206         return ~0U;
207       case v32i8: return 32;
208       case v16i8:
209       case v16i16: return 16;
210       case v8i8 :
211       case v8i16:
212       case v8i32:
213       case v8f32: return 8;
214       case v4i8:
215       case v4i16:
216       case v4i32:
217       case v4i64:
218       case v4f32:
219       case v4f64: return 4;
220       case v2i8:
221       case v2i16:
222       case v2i32:
223       case v2i64:
224       case v2f32:
225       case v2f64: return 2;
226       case v1i64: return 1;
227       }
228     }
229     
230     unsigned getSizeInBits() const {
231       switch (SimpleTy) {
232       case iPTR:
233         assert(0 && "Value type size is target-dependent. Ask TLI.");
234       case iPTRAny:
235       case iAny:
236       case fAny:
237         assert(0 && "Value type is overloaded.");
238       default:
239         assert(0 && "getSizeInBits called on extended MVT.");
240       case i1  :  return 1;
241       case i8  :  return 8;
242       case i16 :
243       case v2i8:  return 16;
244       case f32 :
245       case i32 :
246       case v4i8:
247       case v2i16: return 32;
248       case f64 :
249       case i64 :
250       case v8i8:
251       case v4i16:
252       case v2i32:
253       case v1i64:
254       case v2f32: return 64;
255       case f80 :  return 80;
256       case f128:
257       case ppcf128:
258       case i128:
259       case v16i8:
260       case v8i16:
261       case v4i32:
262       case v2i64:
263       case v4f32:
264       case v2f64: return 128;
265       case v32i8:
266       case v16i16:
267       case v8i32:
268       case v4i64:
269       case v8f32:
270       case v4f64: return 256;
271       }
272     }
273     
274     static MVT getFloatingPointVT(unsigned BitWidth) {
275       switch (BitWidth) {
276       default:
277         assert(false && "Bad bit width!");
278       case 32:
279         return MVT::f32;
280       case 64:
281         return MVT::f64;
282       case 80:
283         return MVT::f80;
284       case 128:
285         return MVT::f128;
286       }
287     }
288     
289     static MVT getIntegerVT(unsigned BitWidth) {
290       switch (BitWidth) {
291       default:
292         return (MVT::SimpleValueType)(MVT::INVALID_SIMPLE_VALUE_TYPE);
293       case 1:
294         return MVT::i1;
295       case 8:
296         return MVT::i8;
297       case 16:
298         return MVT::i16;
299       case 32:
300         return MVT::i32;
301       case 64:
302         return MVT::i64;
303       case 128:
304         return MVT::i128;
305       }
306     }
307     
308     static MVT getVectorVT(MVT VT, unsigned NumElements) {
309       switch (VT.SimpleTy) {
310       default:
311         break;
312       case MVT::i8:
313         if (NumElements == 2)  return MVT::v2i8;
314         if (NumElements == 4)  return MVT::v4i8;
315         if (NumElements == 8)  return MVT::v8i8;
316         if (NumElements == 16) return MVT::v16i8;
317         if (NumElements == 32) return MVT::v32i8;
318         break;
319       case MVT::i16:
320         if (NumElements == 2)  return MVT::v2i16;
321         if (NumElements == 4)  return MVT::v4i16;
322         if (NumElements == 8)  return MVT::v8i16;
323         if (NumElements == 16) return MVT::v16i16;
324         break;
325       case MVT::i32:
326         if (NumElements == 2)  return MVT::v2i32;
327         if (NumElements == 4)  return MVT::v4i32;
328         if (NumElements == 8)  return MVT::v8i32;
329         break;
330       case MVT::i64:
331         if (NumElements == 1)  return MVT::v1i64;
332         if (NumElements == 2)  return MVT::v2i64;
333         if (NumElements == 4)  return MVT::v4i64;
334         break;
335       case MVT::f32:
336         if (NumElements == 2)  return MVT::v2f32;
337         if (NumElements == 4)  return MVT::v4f32;
338         if (NumElements == 8)  return MVT::v8f32;
339         break;
340       case MVT::f64:
341         if (NumElements == 2)  return MVT::v2f64;
342         if (NumElements == 4)  return MVT::v4f64;
343         break;
344       }
345       return (MVT::SimpleValueType)(MVT::INVALID_SIMPLE_VALUE_TYPE);
346     }
347     
348     static MVT getIntVectorWithNumElements(unsigned NumElts) {
349       switch (NumElts) {
350       default: return (MVT::SimpleValueType)(MVT::INVALID_SIMPLE_VALUE_TYPE);
351       case  1: return MVT::v1i64;
352       case  2: return MVT::v2i32;
353       case  4: return MVT::v4i16;
354       case  8: return MVT::v8i8;
355       case 16: return MVT::v16i8;
356       }
357     }
358   };
359
360   struct EVT { // EVT = Extended Value Type
361   private:
362     MVT V;
363     const Type *LLVMTy;
364
365   public:
366     EVT() : V((MVT::SimpleValueType)(MVT::INVALID_SIMPLE_VALUE_TYPE)),
367             LLVMTy(0) {}
368     EVT(MVT::SimpleValueType SVT) : V(SVT), LLVMTy(0) { }
369     EVT(MVT S) : V(S), LLVMTy(0) {}
370
371     bool operator==(const EVT VT) const {
372       if (V.SimpleTy == VT.V.SimpleTy) {
373         if (V.SimpleTy == MVT::INVALID_SIMPLE_VALUE_TYPE)
374           return LLVMTy == VT.LLVMTy;
375         return true;
376       }
377       return false;
378     }
379     bool operator!=(const EVT VT) const {
380       if (V.SimpleTy == VT.V.SimpleTy) {
381         if (V.SimpleTy == MVT::INVALID_SIMPLE_VALUE_TYPE)
382           return LLVMTy != VT.LLVMTy;
383         return false;
384       }
385       return true;
386     }
387
388     /// getFloatingPointVT - Returns the EVT that represents a floating point
389     /// type with the given number of bits.  There are two floating point types
390     /// with 128 bits - this returns f128 rather than ppcf128.
391     static EVT getFloatingPointVT(unsigned BitWidth) {
392       return MVT::getFloatingPointVT(BitWidth);
393     }
394
395     /// getIntegerVT - Returns the EVT that represents an integer with the given
396     /// number of bits.
397     static EVT getIntegerVT(LLVMContext &Context, unsigned BitWidth) {
398       MVT M = MVT::getIntegerVT(BitWidth);
399       if (M.SimpleTy == MVT::INVALID_SIMPLE_VALUE_TYPE)
400         return getExtendedIntegerVT(Context, BitWidth);
401       else
402         return M;
403     }
404
405     /// getVectorVT - Returns the EVT that represents a vector NumElements in
406     /// length, where each element is of type VT.
407     static EVT getVectorVT(LLVMContext &Context, EVT VT, unsigned NumElements) {
408       MVT M = MVT::getVectorVT(VT.V, NumElements);
409       if (M.SimpleTy == MVT::INVALID_SIMPLE_VALUE_TYPE)
410         return getExtendedVectorVT(Context, VT, NumElements);
411       else
412         return M;
413     }
414
415     /// getIntVectorWithNumElements - Return any integer vector type that has
416     /// the specified number of elements.
417     static EVT getIntVectorWithNumElements(LLVMContext &C, unsigned NumElts) {
418       MVT M = MVT::getIntVectorWithNumElements(NumElts);
419       if (M.SimpleTy == MVT::INVALID_SIMPLE_VALUE_TYPE)
420         return getVectorVT(C, MVT::i8, NumElts);
421       else
422         return M;
423     }
424
425     /// isSimple - Test if the given EVT is simple (as opposed to being
426     /// extended).
427     bool isSimple() const {
428       return V.SimpleTy <= MVT::LastSimpleValueType;
429     }
430
431     /// isExtended - Test if the given EVT is extended (as opposed to
432     /// being simple).
433     bool isExtended() const {
434       return !isSimple();
435     }
436
437     /// isFloatingPoint - Return true if this is a FP, or a vector FP type.
438     bool isFloatingPoint() const {
439       return isSimple() ?
440              ((V >= MVT::f32 && V <= MVT::ppcf128) ||
441               (V >= MVT::v2f32 && V <= MVT::v4f64)) : isExtendedFloatingPoint();
442     }
443
444     /// isInteger - Return true if this is an integer, or a vector integer type.
445     bool isInteger() const {
446       return isSimple() ?
447              ((V >= MVT::FIRST_INTEGER_VALUETYPE &&
448                V <= MVT::LAST_INTEGER_VALUETYPE) ||
449               (V >= MVT::v2i8 && V <= MVT::v4i64)) : isExtendedInteger();
450     }
451
452     /// isVector - Return true if this is a vector value type.
453     bool isVector() const {
454       return isSimple() ?
455              (V >= MVT::FIRST_VECTOR_VALUETYPE && V <= 
456                    MVT::LAST_VECTOR_VALUETYPE) :
457              isExtendedVector();
458     }
459
460     /// is64BitVector - Return true if this is a 64-bit vector type.
461     bool is64BitVector() const {
462       return isSimple() ?
463              (V==MVT::v8i8 || V==MVT::v4i16 || V==MVT::v2i32 ||
464               V==MVT::v1i64 || V==MVT::v2f32) :
465              isExtended64BitVector();
466     }
467
468     /// is128BitVector - Return true if this is a 128-bit vector type.
469     bool is128BitVector() const {
470       return isSimple() ?
471              (V==MVT::v16i8 || V==MVT::v8i16 || V==MVT::v4i32 ||
472               V==MVT::v2i64 || V==MVT::v4f32 || V==MVT::v2f64) :
473              isExtended128BitVector();
474     }
475
476     /// is256BitVector - Return true if this is a 256-bit vector type.
477     inline bool is256BitVector() const {
478       return isSimple() ?
479              (V==MVT::v8f32 || V==MVT::v4f64 || V==MVT::v32i8 ||
480               V==MVT::v16i16 || V==MVT::v8i32 || V==MVT::v4i64) : 
481             isExtended256BitVector();
482     }
483
484     /// isOverloaded - Return true if this is an overloaded type for TableGen.
485     bool isOverloaded() const {
486       return (V==MVT::iAny || V==MVT::fAny || V==MVT::vAny || V==MVT::iPTRAny);
487     }
488
489     /// isByteSized - Return true if the bit size is a multiple of 8.
490     bool isByteSized() const {
491       return (getSizeInBits() & 7) == 0;
492     }
493
494     /// isRound - Return true if the size is a power-of-two number of bytes.
495     bool isRound() const {
496       unsigned BitSize = getSizeInBits();
497       return BitSize >= 8 && !(BitSize & (BitSize - 1));
498     }
499
500     /// bitsEq - Return true if this has the same number of bits as VT.
501     bool bitsEq(EVT VT) const {
502       return getSizeInBits() == VT.getSizeInBits();
503     }
504
505     /// bitsGT - Return true if this has more bits than VT.
506     bool bitsGT(EVT VT) const {
507       return getSizeInBits() > VT.getSizeInBits();
508     }
509
510     /// bitsGE - Return true if this has no less bits than VT.
511     bool bitsGE(EVT VT) const {
512       return getSizeInBits() >= VT.getSizeInBits();
513     }
514
515     /// bitsLT - Return true if this has less bits than VT.
516     bool bitsLT(EVT VT) const {
517       return getSizeInBits() < VT.getSizeInBits();
518     }
519
520     /// bitsLE - Return true if this has no more bits than VT.
521     bool bitsLE(EVT VT) const {
522       return getSizeInBits() <= VT.getSizeInBits();
523     }
524
525
526     /// getSimpleVT - Return the SimpleValueType held in the specified
527     /// simple EVT.
528     MVT getSimpleVT() const {
529       assert(isSimple() && "Expected a SimpleValueType!");
530       return V;
531     }
532
533     /// getScalarType - If this is a vector type, return the element type,
534     /// otherwise return this.
535     EVT getScalarType() const {
536       return isVector() ? getVectorElementType() : *this;
537     }
538     
539     /// getVectorElementType - Given a vector type, return the type of
540     /// each element.
541     EVT getVectorElementType() const {
542       assert(isVector() && "Invalid vector type!");
543       if (isSimple())
544         return V.getVectorElementType();
545       else
546         return getExtendedVectorElementType();
547     }
548
549     /// getVectorNumElements - Given a vector type, return the number of
550     /// elements it contains.
551     unsigned getVectorNumElements() const {
552       assert(isVector() && "Invalid vector type!");
553       if (isSimple())
554         return V.getVectorNumElements();
555       else
556         return getExtendedVectorNumElements();
557     }
558
559     /// getSizeInBits - Return the size of the specified value type in bits.
560     unsigned getSizeInBits() const {
561       if (isSimple())
562         return V.getSizeInBits();
563       else
564         return getExtendedSizeInBits();
565     }
566
567     /// getStoreSize - Return the number of bytes overwritten by a store
568     /// of the specified value type.
569     unsigned getStoreSize() const {
570       return (getSizeInBits() + 7) / 8;
571     }
572
573     /// getStoreSizeInBits - Return the number of bits overwritten by a store
574     /// of the specified value type.
575     unsigned getStoreSizeInBits() const {
576       return getStoreSize() * 8;
577     }
578
579     /// getRoundIntegerType - Rounds the bit-width of the given integer EVT up
580     /// to the nearest power of two (and at least to eight), and returns the
581     /// integer EVT with that number of bits.
582     EVT getRoundIntegerType(LLVMContext &Context) const {
583       assert(isInteger() && !isVector() && "Invalid integer type!");
584       unsigned BitWidth = getSizeInBits();
585       if (BitWidth <= 8)
586         return EVT(MVT::i8);
587       else
588         return getIntegerVT(Context, 1 << Log2_32_Ceil(BitWidth));
589     }
590
591     /// isPow2VectorType - Retuns true if the given vector is a power of 2.
592     bool isPow2VectorType() const {
593       unsigned NElts = getVectorNumElements();
594       return !(NElts & (NElts - 1));
595     }
596
597     /// getPow2VectorType - Widens the length of the given vector EVT up to
598     /// the nearest power of 2 and returns that type.
599     EVT getPow2VectorType(LLVMContext &Context) const {
600       if (!isPow2VectorType()) {
601         unsigned NElts = getVectorNumElements();
602         unsigned Pow2NElts = 1 <<  Log2_32_Ceil(NElts);
603         return EVT::getVectorVT(Context, getVectorElementType(), Pow2NElts);
604       }
605       else {
606         return *this;
607       }
608     }
609
610     /// getEVTString - This function returns value type as a string,
611     /// e.g. "i32".
612     std::string getEVTString() const;
613
614     /// getTypeForEVT - This method returns an LLVM type corresponding to the
615     /// specified EVT.  For integer types, this returns an unsigned type.  Note
616     /// that this will abort for types that cannot be represented.
617     const Type *getTypeForEVT(LLVMContext &Context) const;
618
619     /// getEVT - Return the value type corresponding to the specified type.
620     /// This returns all pointers as iPTR.  If HandleUnknown is true, unknown
621     /// types are returned as Other, otherwise they are invalid.
622     static EVT getEVT(const Type *Ty, bool HandleUnknown = false);
623
624     intptr_t getRawBits() {
625       if (V.SimpleTy <= MVT::LastSimpleValueType)
626         return V.SimpleTy;
627       else
628         return (intptr_t)(LLVMTy);
629     }
630
631     /// compareRawBits - A meaningless but well-behaved order, useful for
632     /// constructing containers.
633     struct compareRawBits {
634       bool operator()(EVT L, EVT R) const {
635         if (L.V.SimpleTy == R.V.SimpleTy)
636           return L.LLVMTy < R.LLVMTy;
637         else
638           return L.V.SimpleTy < R.V.SimpleTy;
639       }
640     };
641
642   private:
643     // Methods for handling the Extended-type case in functions above.
644     // These are all out-of-line to prevent users of this header file
645     // from having a dependency on Type.h.
646     static EVT getExtendedIntegerVT(LLVMContext &C, unsigned BitWidth);
647     static EVT getExtendedVectorVT(LLVMContext &C, EVT VT,
648                                    unsigned NumElements);
649     bool isExtendedFloatingPoint() const;
650     bool isExtendedInteger() const;
651     bool isExtendedVector() const;
652     bool isExtended64BitVector() const;
653     bool isExtended128BitVector() const;
654     bool isExtended256BitVector() const;
655     EVT getExtendedVectorElementType() const;
656     unsigned getExtendedVectorNumElements() const;
657     unsigned getExtendedSizeInBits() const;
658   };
659
660 } // End llvm namespace
661
662 #endif