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