Move DataTypes.h to include/llvm/System, update all users. This breaks the last
[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 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       // 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     MVT getVectorElementType() const {
171       switch (SimpleTy) {
172       default:
173         return (MVT::SimpleValueType)(MVT::INVALID_SIMPLE_VALUE_TYPE);
174       case v2i8 :
175       case v4i8 :
176       case v8i8 :
177       case v16i8:
178       case v32i8: return i8;
179       case v2i16:
180       case v4i16:
181       case v8i16:
182       case v16i16: return i16;
183       case v2i32:
184       case v4i32:
185       case v8i32: return i32;
186       case v1i64:
187       case v2i64:
188       case v4i64: return i64;
189       case v2f32:
190       case v4f32:
191       case v8f32: return f32;
192       case v2f64:
193       case v4f64: return f64;
194       }
195     }
196     
197     unsigned getVectorNumElements() const {
198       switch (SimpleTy) {
199       default:
200         return ~0U;
201       case v32i8: return 32;
202       case v16i8:
203       case v16i16: return 16;
204       case v8i8 :
205       case v8i16:
206       case v8i32:
207       case v8f32: return 8;
208       case v4i8:
209       case v4i16:
210       case v4i32:
211       case v4i64:
212       case v4f32:
213       case v4f64: return 4;
214       case v2i8:
215       case v2i16:
216       case v2i32:
217       case v2i64:
218       case v2f32:
219       case v2f64: return 2;
220       case v1i64: return 1;
221       }
222     }
223     
224     unsigned getSizeInBits() const {
225       switch (SimpleTy) {
226       case iPTR:
227         assert(0 && "Value type size is target-dependent. Ask TLI.");
228       case iPTRAny:
229       case iAny:
230       case fAny:
231         assert(0 && "Value type is overloaded.");
232       default:
233         assert(0 && "getSizeInBits called on extended MVT.");
234       case i1  :  return 1;
235       case i8  :  return 8;
236       case i16 :
237       case v2i8:  return 16;
238       case f32 :
239       case i32 :
240       case v4i8:
241       case v2i16: return 32;
242       case f64 :
243       case i64 :
244       case v8i8:
245       case v4i16:
246       case v2i32:
247       case v1i64:
248       case v2f32: return 64;
249       case f80 :  return 80;
250       case f128:
251       case ppcf128:
252       case i128:
253       case v16i8:
254       case v8i16:
255       case v4i32:
256       case v2i64:
257       case v4f32:
258       case v2f64: return 128;
259       case v32i8:
260       case v16i16:
261       case v8i32:
262       case v4i64:
263       case v8f32:
264       case v4f64: return 256;
265       }
266     }
267     
268     static MVT getFloatingPointVT(unsigned BitWidth) {
269       switch (BitWidth) {
270       default:
271         assert(false && "Bad bit width!");
272       case 32:
273         return MVT::f32;
274       case 64:
275         return MVT::f64;
276       case 80:
277         return MVT::f80;
278       case 128:
279         return MVT::f128;
280       }
281     }
282     
283     static MVT getIntegerVT(unsigned BitWidth) {
284       switch (BitWidth) {
285       default:
286         return (MVT::SimpleValueType)(MVT::INVALID_SIMPLE_VALUE_TYPE);
287       case 1:
288         return MVT::i1;
289       case 8:
290         return MVT::i8;
291       case 16:
292         return MVT::i16;
293       case 32:
294         return MVT::i32;
295       case 64:
296         return MVT::i64;
297       case 128:
298         return MVT::i128;
299       }
300     }
301     
302     static MVT getVectorVT(MVT VT, unsigned NumElements) {
303       switch (VT.SimpleTy) {
304       default:
305         break;
306       case MVT::i8:
307         if (NumElements == 2)  return MVT::v2i8;
308         if (NumElements == 4)  return MVT::v4i8;
309         if (NumElements == 8)  return MVT::v8i8;
310         if (NumElements == 16) return MVT::v16i8;
311         if (NumElements == 32) return MVT::v32i8;
312         break;
313       case MVT::i16:
314         if (NumElements == 2)  return MVT::v2i16;
315         if (NumElements == 4)  return MVT::v4i16;
316         if (NumElements == 8)  return MVT::v8i16;
317         if (NumElements == 16) return MVT::v16i16;
318         break;
319       case MVT::i32:
320         if (NumElements == 2)  return MVT::v2i32;
321         if (NumElements == 4)  return MVT::v4i32;
322         if (NumElements == 8)  return MVT::v8i32;
323         break;
324       case MVT::i64:
325         if (NumElements == 1)  return MVT::v1i64;
326         if (NumElements == 2)  return MVT::v2i64;
327         if (NumElements == 4)  return MVT::v4i64;
328         break;
329       case MVT::f32:
330         if (NumElements == 2)  return MVT::v2f32;
331         if (NumElements == 4)  return MVT::v4f32;
332         if (NumElements == 8)  return MVT::v8f32;
333         break;
334       case MVT::f64:
335         if (NumElements == 2)  return MVT::v2f64;
336         if (NumElements == 4)  return MVT::v4f64;
337         break;
338       }
339       return (MVT::SimpleValueType)(MVT::INVALID_SIMPLE_VALUE_TYPE);
340     }
341     
342     static MVT getIntVectorWithNumElements(unsigned NumElts) {
343       switch (NumElts) {
344       default: return (MVT::SimpleValueType)(MVT::INVALID_SIMPLE_VALUE_TYPE);
345       case  1: return MVT::v1i64;
346       case  2: return MVT::v2i32;
347       case  4: return MVT::v4i16;
348       case  8: return MVT::v8i8;
349       case 16: return MVT::v16i8;
350       }
351     }
352   };
353
354   struct EVT { // EVT = Extended Value Type
355   private:
356     MVT V;
357     const Type *LLVMTy;
358
359   public:
360     EVT() : V((MVT::SimpleValueType)(MVT::INVALID_SIMPLE_VALUE_TYPE)),
361             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::INVALID_SIMPLE_VALUE_TYPE)
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::INVALID_SIMPLE_VALUE_TYPE)
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::INVALID_SIMPLE_VALUE_TYPE)
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::INVALID_SIMPLE_VALUE_TYPE)
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::INVALID_SIMPLE_VALUE_TYPE)
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     /// getStoreSize - Return the number of bytes overwritten by a store
556     /// of the specified value type.
557     unsigned getStoreSize() const {
558       return (getSizeInBits() + 7) / 8;
559     }
560
561     /// getStoreSizeInBits - Return the number of bits overwritten by a store
562     /// of the specified value type.
563     unsigned getStoreSizeInBits() const {
564       return getStoreSize() * 8;
565     }
566
567     /// getRoundIntegerType - Rounds the bit-width of the given integer EVT up
568     /// to the nearest power of two (and at least to eight), and returns the
569     /// integer EVT with that number of bits.
570     EVT getRoundIntegerType(LLVMContext &Context) const {
571       assert(isInteger() && !isVector() && "Invalid integer type!");
572       unsigned BitWidth = getSizeInBits();
573       if (BitWidth <= 8)
574         return EVT(MVT::i8);
575       else
576         return getIntegerVT(Context, 1 << Log2_32_Ceil(BitWidth));
577     }
578
579     /// isPow2VectorType - Retuns true if the given vector is a power of 2.
580     bool isPow2VectorType() const {
581       unsigned NElts = getVectorNumElements();
582       return !(NElts & (NElts - 1));
583     }
584
585     /// getPow2VectorType - Widens the length of the given vector EVT up to
586     /// the nearest power of 2 and returns that type.
587     EVT getPow2VectorType(LLVMContext &Context) const {
588       if (!isPow2VectorType()) {
589         unsigned NElts = getVectorNumElements();
590         unsigned Pow2NElts = 1 <<  Log2_32_Ceil(NElts);
591         return EVT::getVectorVT(Context, getVectorElementType(), Pow2NElts);
592       }
593       else {
594         return *this;
595       }
596     }
597
598     /// getEVTString - This function returns value type as a string,
599     /// e.g. "i32".
600     std::string getEVTString() const;
601
602     /// getTypeForEVT - This method returns an LLVM type corresponding to the
603     /// specified EVT.  For integer types, this returns an unsigned type.  Note
604     /// that this will abort for types that cannot be represented.
605     const Type *getTypeForEVT(LLVMContext &Context) const;
606
607     /// getEVT - Return the value type corresponding to the specified type.
608     /// This returns all pointers as iPTR.  If HandleUnknown is true, unknown
609     /// types are returned as Other, otherwise they are invalid.
610     static EVT getEVT(const Type *Ty, bool HandleUnknown = false);
611
612     intptr_t getRawBits() {
613       if (V.SimpleTy <= MVT::LastSimpleValueType)
614         return V.SimpleTy;
615       else
616         return (intptr_t)(LLVMTy);
617     }
618
619     /// compareRawBits - A meaningless but well-behaved order, useful for
620     /// constructing containers.
621     struct compareRawBits {
622       bool operator()(EVT L, EVT R) const {
623         if (L.V.SimpleTy == R.V.SimpleTy)
624           return L.LLVMTy < R.LLVMTy;
625         else
626           return L.V.SimpleTy < R.V.SimpleTy;
627       }
628     };
629
630   private:
631     // Methods for handling the Extended-type case in functions above.
632     // These are all out-of-line to prevent users of this header file
633     // from having a dependency on Type.h.
634     static EVT getExtendedIntegerVT(LLVMContext &C, unsigned BitWidth);
635     static EVT getExtendedVectorVT(LLVMContext &C, EVT VT,
636                                    unsigned NumElements);
637     bool isExtendedFloatingPoint() const;
638     bool isExtendedInteger() const;
639     bool isExtendedVector() const;
640     bool isExtended64BitVector() const;
641     bool isExtended128BitVector() const;
642     bool isExtended256BitVector() const;
643     EVT getExtendedVectorElementType() const;
644     unsigned getExtendedVectorNumElements() const;
645     unsigned getExtendedSizeInBits() const;
646   };
647
648 } // End llvm namespace
649
650 #endif