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