Fix gcc-4.4/fedora 11 by adding a sentinel value to SimpleValueType.
[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   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 is a condition code or machine flag.
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       // FirstExtendedValueType - This sentinel is needed so that gcc 4.4 won't
117       // optimize away checks of a SimpleValueType compared to
118       // LastSimpleValueType+1.
119       FirstExtendedValueType = 256
120     };
121
122     SimpleValueType SimpleTy;
123
124     MVT() : SimpleTy((SimpleValueType)(LastSimpleValueType+1)) {}
125     MVT(SimpleValueType SVT) : SimpleTy(SVT) { }
126     
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     bool operator<=(const MVT& S) const { return SimpleTy <= S.SimpleTy; }
132     
133     /// isFloatingPoint - Return true if this is a FP, or a vector FP type.
134     bool isFloatingPoint() const {
135       return ((SimpleTy >= MVT::f32 && SimpleTy <= MVT::ppcf128) ||
136         (SimpleTy >= MVT::v2f32 && SimpleTy <= MVT::v4f64));
137     }
138
139     /// isInteger - Return true if this is an integer, or a vector integer type.
140     bool isInteger() const {
141       return ((SimpleTy >= MVT::FIRST_INTEGER_VALUETYPE &&
142                SimpleTy <= MVT::LAST_INTEGER_VALUETYPE) ||
143                (SimpleTy >= MVT::v2i8 && SimpleTy <= MVT::v4i64));
144     }
145
146     /// isVector - Return true if this is a vector value type.
147     bool isVector() const {
148       return (SimpleTy >= MVT::FIRST_VECTOR_VALUETYPE &&
149               SimpleTy <= MVT::LAST_VECTOR_VALUETYPE);
150     }
151     
152     /// isPow2VectorType - Retuns true if the given vector is a power of 2.
153     bool isPow2VectorType() const {
154       unsigned NElts = getVectorNumElements();
155       return !(NElts & (NElts - 1));
156     }
157
158     /// getPow2VectorType - Widens the length of the given vector EVT up to
159     /// the nearest power of 2 and returns that type.
160     MVT getPow2VectorType() const {
161       if (!isPow2VectorType()) {
162         unsigned NElts = getVectorNumElements();
163         unsigned Pow2NElts = 1 <<  Log2_32_Ceil(NElts);
164         return MVT::getVectorVT(getVectorElementType(), Pow2NElts);
165       }
166       else {
167         return *this;
168       }
169     }
170     
171     MVT getVectorElementType() const {
172       switch (SimpleTy) {
173       default:
174         return (MVT::SimpleValueType)(MVT::LastSimpleValueType+1);
175       case v2i8 :
176       case v4i8 :
177       case v8i8 :
178       case v16i8:
179       case v32i8: return i8;
180       case v2i16:
181       case v4i16:
182       case v8i16:
183       case v16i16: return i16;
184       case v2i32:
185       case v4i32:
186       case v8i32: return i32;
187       case v1i64:
188       case v2i64:
189       case v4i64: return i64;
190       case v2f32:
191       case v4f32:
192       case v8f32: return f32;
193       case v2f64:
194       case v4f64: return f64;
195       }
196     }
197     
198     unsigned getVectorNumElements() const {
199       switch (SimpleTy) {
200       default:
201         return ~0U;
202       case v32i8: return 32;
203       case v16i8:
204       case v16i16: return 16;
205       case v8i8 :
206       case v8i16:
207       case v8i32:
208       case v8f32: return 8;
209       case v4i8:
210       case v4i16:
211       case v4i32:
212       case v4i64:
213       case v4f32:
214       case v4f64: return 4;
215       case v2i8:
216       case v2i16:
217       case v2i32:
218       case v2i64:
219       case v2f32:
220       case v2f64: return 2;
221       case v1i64: return 1;
222       }
223     }
224     
225     unsigned getSizeInBits() const {
226       switch (SimpleTy) {
227       case iPTR:
228         assert(0 && "Value type size is target-dependent. Ask TLI.");
229       case iPTRAny:
230       case iAny:
231       case fAny:
232         assert(0 && "Value type is overloaded.");
233       default:
234         assert(0 && "getSizeInBits called on extended MVT.");
235       case i1  :  return 1;
236       case i8  :  return 8;
237       case i16 :
238       case v2i8:  return 16;
239       case f32 :
240       case i32 :
241       case v4i8:
242       case v2i16: return 32;
243       case f64 :
244       case i64 :
245       case v8i8:
246       case v4i16:
247       case v2i32:
248       case v1i64:
249       case v2f32: return 64;
250       case f80 :  return 80;
251       case f128:
252       case ppcf128:
253       case i128:
254       case v16i8:
255       case v8i16:
256       case v4i32:
257       case v2i64:
258       case v4f32:
259       case v2f64: return 128;
260       case v32i8:
261       case v16i16:
262       case v8i32:
263       case v4i64:
264       case v8f32:
265       case v4f64: return 256;
266       }
267     }
268     
269     static MVT getFloatingPointVT(unsigned BitWidth) {
270       switch (BitWidth) {
271       default:
272         assert(false && "Bad bit width!");
273       case 32:
274         return MVT::f32;
275       case 64:
276         return MVT::f64;
277       case 80:
278         return MVT::f80;
279       case 128:
280         return MVT::f128;
281       }
282     }
283     
284     static MVT getIntegerVT(unsigned BitWidth) {
285       switch (BitWidth) {
286       default:
287         return (MVT::SimpleValueType)(MVT::LastSimpleValueType+1);
288       case 1:
289         return MVT::i1;
290       case 8:
291         return MVT::i8;
292       case 16:
293         return MVT::i16;
294       case 32:
295         return MVT::i32;
296       case 64:
297         return MVT::i64;
298       case 128:
299         return MVT::i128;
300       }
301     }
302     
303     static MVT getVectorVT(MVT VT, unsigned NumElements) {
304       switch (VT.SimpleTy) {
305       default:
306         break;
307       case MVT::i8:
308         if (NumElements == 2)  return MVT::v2i8;
309         if (NumElements == 4)  return MVT::v4i8;
310         if (NumElements == 8)  return MVT::v8i8;
311         if (NumElements == 16) return MVT::v16i8;
312         if (NumElements == 32) return MVT::v32i8;
313         break;
314       case MVT::i16:
315         if (NumElements == 2)  return MVT::v2i16;
316         if (NumElements == 4)  return MVT::v4i16;
317         if (NumElements == 8)  return MVT::v8i16;
318         if (NumElements == 16) return MVT::v16i16;
319         break;
320       case MVT::i32:
321         if (NumElements == 2)  return MVT::v2i32;
322         if (NumElements == 4)  return MVT::v4i32;
323         if (NumElements == 8)  return MVT::v8i32;
324         break;
325       case MVT::i64:
326         if (NumElements == 1)  return MVT::v1i64;
327         if (NumElements == 2)  return MVT::v2i64;
328         if (NumElements == 4)  return MVT::v4i64;
329         break;
330       case MVT::f32:
331         if (NumElements == 2)  return MVT::v2f32;
332         if (NumElements == 4)  return MVT::v4f32;
333         if (NumElements == 8)  return MVT::v8f32;
334         break;
335       case MVT::f64:
336         if (NumElements == 2)  return MVT::v2f64;
337         if (NumElements == 4)  return MVT::v4f64;
338         break;
339       }
340       return (MVT::SimpleValueType)(MVT::LastSimpleValueType+1);
341     }
342     
343     static MVT getIntVectorWithNumElements(unsigned NumElts) {
344       switch (NumElts) {
345       default: return (MVT::SimpleValueType)(MVT::LastSimpleValueType+1);
346       case  1: return MVT::v1i64;
347       case  2: return MVT::v2i32;
348       case  4: return MVT::v4i16;
349       case  8: return MVT::v8i8;
350       case 16: return MVT::v16i8;
351       }
352     }
353   };
354
355   struct EVT { // EVT = Extended Value Type
356   private:
357     MVT V;
358     const Type *LLVMTy;
359
360   public:
361     EVT() : V((MVT::SimpleValueType)(MVT::LastSimpleValueType+1)), LLVMTy(0) {}
362     EVT(MVT::SimpleValueType SVT) : V(SVT), LLVMTy(0) { }
363     EVT(MVT S) : V(S), LLVMTy(0) {}
364
365     bool operator==(const EVT VT) const {
366       if (V.SimpleTy == VT.V.SimpleTy) {
367         if (V.SimpleTy == MVT::LastSimpleValueType+1)
368           return LLVMTy == VT.LLVMTy;
369         return true;
370       }
371       return false;
372     }
373     bool operator!=(const EVT VT) const {
374       if (V.SimpleTy == VT.V.SimpleTy) {
375         if (V.SimpleTy == MVT::LastSimpleValueType+1)
376           return LLVMTy != VT.LLVMTy;
377         return false;
378       }
379       return true;
380     }
381
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);
387     }
388
389     /// getIntegerVT - Returns the EVT that represents an integer with the given
390     /// number of bits.
391     static EVT getIntegerVT(LLVMContext &Context, unsigned BitWidth) {
392       MVT M = MVT::getIntegerVT(BitWidth);
393       if (M.SimpleTy == MVT::LastSimpleValueType+1)
394         return getExtendedIntegerVT(Context, BitWidth);
395       else
396         return M;
397     }
398
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::LastSimpleValueType+1)
404         return getExtendedVectorVT(Context, VT, NumElements);
405       else
406         return M;
407     }
408
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::LastSimpleValueType+1)
414         return getVectorVT(C, MVT::i8, NumElts);
415       else
416         return M;
417     }
418
419     /// isSimple - Test if the given EVT is simple (as opposed to being
420     /// extended).
421     bool isSimple() const {
422       return V.SimpleTy <= MVT::LastSimpleValueType;
423     }
424
425     /// isExtended - Test if the given EVT is extended (as opposed to
426     /// being simple).
427     bool isExtended() const {
428       return !isSimple();
429     }
430
431     /// isFloatingPoint - Return true if this is a FP, or a vector FP type.
432     bool isFloatingPoint() const {
433       return isSimple() ?
434              ((V >= MVT::f32 && V <= MVT::ppcf128) ||
435               (V >= MVT::v2f32 && V <= MVT::v4f64)) : isExtendedFloatingPoint();
436     }
437
438     /// isInteger - Return true if this is an integer, or a vector integer type.
439     bool isInteger() const {
440       return isSimple() ?
441              ((V >= MVT::FIRST_INTEGER_VALUETYPE &&
442                V <= MVT::LAST_INTEGER_VALUETYPE) ||
443               (V >= MVT::v2i8 && V <= MVT::v4i64)) : isExtendedInteger();
444     }
445
446     /// isVector - Return true if this is a vector value type.
447     bool isVector() const {
448       return isSimple() ?
449              (V >= MVT::FIRST_VECTOR_VALUETYPE && V <= 
450                    MVT::LAST_VECTOR_VALUETYPE) :
451              isExtendedVector();
452     }
453
454     /// is64BitVector - Return true if this is a 64-bit vector type.
455     bool is64BitVector() const {
456       return isSimple() ?
457              (V==MVT::v8i8 || V==MVT::v4i16 || V==MVT::v2i32 ||
458               V==MVT::v1i64 || V==MVT::v2f32) :
459              isExtended64BitVector();
460     }
461
462     /// is128BitVector - Return true if this is a 128-bit vector type.
463     bool is128BitVector() const {
464       return isSimple() ?
465              (V==MVT::v16i8 || V==MVT::v8i16 || V==MVT::v4i32 ||
466               V==MVT::v2i64 || V==MVT::v4f32 || V==MVT::v2f64) :
467              isExtended128BitVector();
468     }
469
470     /// is256BitVector - Return true if this is a 256-bit vector type.
471     inline bool is256BitVector() const {
472       return isSimple() ?
473              (V==MVT::v8f32 || V==MVT::v4f64 || V==MVT::v32i8 ||
474               V==MVT::v16i16 || V==MVT::v8i32 || V==MVT::v4i64) : 
475             isExtended256BitVector();
476     }
477
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);
481     }
482
483     /// isByteSized - Return true if the bit size is a multiple of 8.
484     bool isByteSized() const {
485       return (getSizeInBits() & 7) == 0;
486     }
487
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));
492     }
493
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();
497     }
498
499     /// bitsGT - Return true if this has more bits than VT.
500     bool bitsGT(EVT VT) const {
501       return getSizeInBits() > VT.getSizeInBits();
502     }
503
504     /// bitsGE - Return true if this has no less bits than VT.
505     bool bitsGE(EVT VT) const {
506       return getSizeInBits() >= VT.getSizeInBits();
507     }
508
509     /// bitsLT - Return true if this has less bits than VT.
510     bool bitsLT(EVT VT) const {
511       return getSizeInBits() < VT.getSizeInBits();
512     }
513
514     /// bitsLE - Return true if this has no more bits than VT.
515     bool bitsLE(EVT VT) const {
516       return getSizeInBits() <= VT.getSizeInBits();
517     }
518
519
520     /// getSimpleVT - Return the SimpleValueType held in the specified
521     /// simple EVT.
522     MVT getSimpleVT() const {
523       assert(isSimple() && "Expected a SimpleValueType!");
524       return V;
525     }
526
527     /// getVectorElementType - Given a vector type, return the type of
528     /// each element.
529     EVT getVectorElementType() const {
530       assert(isVector() && "Invalid vector type!");
531       if (isSimple())
532         return V.getVectorElementType();
533       else
534         return getExtendedVectorElementType();
535     }
536
537     /// getVectorNumElements - Given a vector type, return the number of
538     /// elements it contains.
539     unsigned getVectorNumElements() const {
540       assert(isVector() && "Invalid vector type!");
541       if (isSimple())
542         return V.getVectorNumElements();
543       else
544         return getExtendedVectorNumElements();
545     }
546
547     /// getSizeInBits - Return the size of the specified value type in bits.
548     unsigned getSizeInBits() const {
549       if (isSimple())
550         return V.getSizeInBits();
551       else
552         return getExtendedSizeInBits();
553     }
554
555     /// getStoreSizeInBits - Return the number of bits overwritten by a store
556     /// of the specified value type.
557     unsigned getStoreSizeInBits() const {
558       return (getSizeInBits() + 7)/8*8;
559     }
560
561     /// getRoundIntegerType - Rounds the bit-width of the given integer EVT up
562     /// to the nearest power of two (and at least to eight), and returns the
563     /// integer EVT with that number of bits.
564     EVT getRoundIntegerType(LLVMContext &Context) const {
565       assert(isInteger() && !isVector() && "Invalid integer type!");
566       unsigned BitWidth = getSizeInBits();
567       if (BitWidth <= 8)
568         return EVT(MVT::i8);
569       else
570         return getIntegerVT(Context, 1 << Log2_32_Ceil(BitWidth));
571     }
572
573     /// isPow2VectorType - Retuns true if the given vector is a power of 2.
574     bool isPow2VectorType() const {
575       unsigned NElts = getVectorNumElements();
576       return !(NElts & (NElts - 1));
577     }
578
579     /// getPow2VectorType - Widens the length of the given vector EVT up to
580     /// the nearest power of 2 and returns that type.
581     EVT getPow2VectorType(LLVMContext &Context) const {
582       if (!isPow2VectorType()) {
583         unsigned NElts = getVectorNumElements();
584         unsigned Pow2NElts = 1 <<  Log2_32_Ceil(NElts);
585         return EVT::getVectorVT(Context, getVectorElementType(), Pow2NElts);
586       }
587       else {
588         return *this;
589       }
590     }
591
592     /// getEVTString - This function returns value type as a string,
593     /// e.g. "i32".
594     std::string getEVTString() const;
595
596     /// getTypeForEVT - This method returns an LLVM type corresponding to the
597     /// specified EVT.  For integer types, this returns an unsigned type.  Note
598     /// that this will abort for types that cannot be represented.
599     const Type *getTypeForEVT(LLVMContext &Context) const;
600
601     /// getEVT - Return the value type corresponding to the specified type.
602     /// This returns all pointers as iPTR.  If HandleUnknown is true, unknown
603     /// types are returned as Other, otherwise they are invalid.
604     static EVT getEVT(const Type *Ty, bool HandleUnknown = false);
605
606     intptr_t getRawBits() {
607       if (V.SimpleTy <= MVT::LastSimpleValueType)
608         return V.SimpleTy;
609       else
610         return (intptr_t)(LLVMTy);
611     }
612
613     /// compareRawBits - A meaningless but well-behaved order, useful for
614     /// constructing containers.
615     struct compareRawBits {
616       bool operator()(EVT L, EVT R) const {
617         if (L.V.SimpleTy == R.V.SimpleTy)
618           return L.LLVMTy < R.LLVMTy;
619         else
620           return L.V.SimpleTy < R.V.SimpleTy;
621       }
622     };
623
624   private:
625     // Methods for handling the Extended-type case in functions above.
626     // These are all out-of-line to prevent users of this header file
627     // from having a dependency on Type.h.
628     static EVT getExtendedIntegerVT(LLVMContext &C, unsigned BitWidth);
629     static EVT getExtendedVectorVT(LLVMContext &C, EVT VT,
630                                    unsigned NumElements);
631     bool isExtendedFloatingPoint() const;
632     bool isExtendedInteger() const;
633     bool isExtendedVector() const;
634     bool isExtended64BitVector() const;
635     bool isExtended128BitVector() const;
636     bool isExtended256BitVector() const;
637     EVT getExtendedVectorElementType() const;
638     unsigned getExtendedVectorNumElements() const;
639     unsigned getExtendedSizeInBits() const;
640   };
641
642 } // End llvm namespace
643
644 #endif