Remove the SimpleTy enumerated type field from the MVT
[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
27   struct MVT { // MVT = Machine Value Type
28   public:
29     enum SimpleValueType {
30       // If you change this numbering, you must change the values in
31       // ValueTypes.td as well!
32       Other          =   0,   // This is a non-standard value
33       i1             =   1,   // This is a 1 bit integer value
34       i8             =   2,   // This is an 8 bit integer value
35       i16            =   3,   // This is a 16 bit integer value
36       i32            =   4,   // This is a 32 bit integer value
37       i64            =   5,   // This is a 64 bit integer value
38       i128           =   6,   // This is a 128 bit integer value
39
40       FIRST_INTEGER_VALUETYPE = i1,
41       LAST_INTEGER_VALUETYPE  = i128,
42
43       f32            =   7,   // This is a 32 bit floating point value
44       f64            =   8,   // This is a 64 bit floating point value
45       f80            =   9,   // This is a 80 bit floating point value
46       f128           =  10,   // This is a 128 bit floating point value
47       ppcf128        =  11,   // This is a PPC 128-bit floating point value
48       Flag           =  12,   // This is a condition code or machine flag.
49
50       isVoid         =  13,   // This has no value
51
52       v2i8           =  14,   //  2 x i8
53       v4i8           =  15,   //  4 x i8
54       v2i16          =  16,   //  2 x i16
55       v8i8           =  17,   //  8 x i8
56       v4i16          =  18,   //  4 x i16
57       v2i32          =  19,   //  2 x i32
58       v1i64          =  20,   //  1 x i64
59       v16i8          =  21,   // 16 x i8
60       v8i16          =  22,   //  8 x i16
61       v3i32          =  23,   //  3 x i32
62       v4i32          =  24,   //  4 x i32
63       v2i64          =  25,   //  2 x i64
64
65       v2f32          =  26,   //  2 x f32
66       v3f32          =  27,   //  3 x f32
67       v4f32          =  28,   //  4 x f32
68       v2f64          =  29,   //  2 x f64
69
70       FIRST_VECTOR_VALUETYPE = v2i8,
71       LAST_VECTOR_VALUETYPE  = v2f64,
72
73       LAST_VALUETYPE =  30,   // This always remains at the end of the list.
74
75       // iPTRAny - An int value the size of the pointer of the current
76       // target to any address space. This must only be used internal to
77       // tblgen. Other than for overloading, we treat iPTRAny the same as iPTR.
78       iPTRAny        =  252,
79
80       // fAny - Any floating-point or vector floating-point value. This is used
81       // for intrinsics that have overloadings based on floating-point types.
82       // This is only for tblgen's consumption!
83       fAny           =  253,
84
85       // iAny - An integer or vector integer value of any bit width. This is
86       // used for intrinsics that have overloadings based on integer bit widths.
87       // This is only for tblgen's consumption!
88       iAny           =  254,
89
90       // iPTR - An int value the size of the pointer of the current
91       // target.  This should only be used internal to tblgen!
92       iPTR           =  255,
93
94       // LastSimpleValueType - The greatest valid SimpleValueType value.
95       LastSimpleValueType = 255
96     };
97
98   private:
99     /// This union holds low-level value types. Valid values include any of
100     /// the values in the SimpleValueType enum, or any value returned from one
101     /// of the MVT methods.  Any value type equal to one of the SimpleValueType
102     /// enum values is a "simple" value type.  All others are "extended".
103     ///
104     /// Note that simple doesn't necessary mean legal for the target machine.
105     /// All legal value types must be simple, but often there are some simple
106     /// value types that are not legal.
107     ///
108     union {
109       uintptr_t V;
110       const Type *LLVMTy;
111     };
112
113   public:
114     MVT() {}
115     MVT(SimpleValueType S) : V(S) {}
116
117     bool operator==(const MVT VT) const {
118       return getRawBits() == VT.getRawBits();
119     }
120     bool operator!=(const MVT VT) const {
121       return getRawBits() != VT.getRawBits();
122     }
123
124     /// getFloatingPointVT - Returns the MVT that represents a floating point
125     /// type with the given number of bits.  There are two floating point types
126     /// with 128 bits - this returns f128 rather than ppcf128.
127     static MVT getFloatingPointVT(unsigned BitWidth) {
128       switch (BitWidth) {
129       default:
130         assert(false && "Bad bit width!");
131       case 32:
132         return f32;
133       case 64:
134         return f64;
135       case 80:
136         return f80;
137       case 128:
138         return f128;
139       }
140     }
141
142     /// getIntegerVT - Returns the MVT that represents an integer with the given
143     /// number of bits.
144     static MVT getIntegerVT(unsigned BitWidth) {
145       switch (BitWidth) {
146       default:
147         break;
148       case 1:
149         return i1;
150       case 8:
151         return i8;
152       case 16:
153         return i16;
154       case 32:
155         return i32;
156       case 64:
157         return i64;
158       case 128:
159         return i128;
160       }
161       return getExtendedIntegerVT(BitWidth);
162     }
163
164     /// getVectorVT - Returns the MVT that represents a vector NumElements in
165     /// length, where each element is of type VT.
166     static MVT getVectorVT(MVT VT, unsigned NumElements) {
167       switch (VT.V) {
168       default:
169         break;
170       case i8:
171         if (NumElements == 2)  return v2i8;
172         if (NumElements == 4)  return v4i8;
173         if (NumElements == 8)  return v8i8;
174         if (NumElements == 16) return v16i8;
175         break;
176       case i16:
177         if (NumElements == 2)  return v2i16;
178         if (NumElements == 4)  return v4i16;
179         if (NumElements == 8)  return v8i16;
180         break;
181       case i32:
182         if (NumElements == 2)  return v2i32;
183         if (NumElements == 3)  return v3i32;
184         if (NumElements == 4)  return v4i32;
185         break;
186       case i64:
187         if (NumElements == 1)  return v1i64;
188         if (NumElements == 2)  return v2i64;
189         break;
190       case f32:
191         if (NumElements == 2)  return v2f32;
192         if (NumElements == 3)  return v3f32;
193         if (NumElements == 4)  return v4f32;
194         break;
195       case f64:
196         if (NumElements == 2)  return v2f64;
197         break;
198       }
199       return getExtendedVectorVT(VT, NumElements);
200     }
201
202     /// getIntVectorWithNumElements - Return any integer vector type that has
203     /// the specified number of elements.
204     static MVT getIntVectorWithNumElements(unsigned NumElts) {
205       switch (NumElts) {
206       default: return getVectorVT(i8, NumElts);
207       case  1: return v1i64;
208       case  2: return v2i32;
209       case  3: return v3i32;
210       case  4: return v4i16;
211       case  8: return v8i8;
212       case 16: return v16i8;
213       }
214     }
215
216     /// isSimple - Test if the given MVT is simple (as opposed to being
217     /// extended).
218     bool isSimple() const {
219       return V <= LastSimpleValueType;
220     }
221
222     /// isExtended - Test if the given MVT is extended (as opposed to
223     /// being simple).
224     bool isExtended() const {
225       return !isSimple();
226     }
227
228     /// isFloatingPoint - Return true if this is a FP, or a vector FP type.
229     bool isFloatingPoint() const {
230       return isSimple() ?
231              ((V >= f32 && V <= ppcf128) || (V >= v2f32 && V <= v2f64)) :
232              isExtendedFloatingPoint();
233     }
234
235     /// isInteger - Return true if this is an integer, or a vector integer type.
236     bool isInteger() const {
237       return isSimple() ?
238              ((V >= FIRST_INTEGER_VALUETYPE && V <= LAST_INTEGER_VALUETYPE) ||
239               (V >= v2i8 && V <= v2i64)) : isExtendedInteger();
240     }
241
242     /// isVector - Return true if this is a vector value type.
243     bool isVector() const {
244       return isSimple() ?
245              (V >= FIRST_VECTOR_VALUETYPE && V <= LAST_VECTOR_VALUETYPE) :
246              isExtendedVector();
247     }
248
249     /// is64BitVector - Return true if this is a 64-bit vector type.
250     bool is64BitVector() const {
251       return isSimple() ?
252              (V==v8i8 || V==v4i16 || V==v2i32 || V==v1i64 || V==v2f32) :
253              isExtended64BitVector();
254     }
255
256     /// is128BitVector - Return true if this is a 128-bit vector type.
257     bool is128BitVector() const {
258       return isSimple() ?
259              (V==v16i8 || V==v8i16 || V==v4i32 ||
260               V==v2i64 || V==v4f32 || V==v2f64) :
261              isExtended128BitVector();
262     }
263
264     /// isByteSized - Return true if the bit size is a multiple of 8.
265     bool isByteSized() const {
266       return (getSizeInBits() & 7) == 0;
267     }
268
269     /// isRound - Return true if the size is a power-of-two number of bytes.
270     bool isRound() const {
271       unsigned BitSize = getSizeInBits();
272       return BitSize >= 8 && !(BitSize & (BitSize - 1));
273     }
274
275     /// bitsEq - Return true if this has the same number of bits as VT.
276     bool bitsEq(MVT VT) const {
277       return getSizeInBits() == VT.getSizeInBits();
278     }
279
280     /// bitsGT - Return true if this has more bits than VT.
281     bool bitsGT(MVT VT) const {
282       return getSizeInBits() > VT.getSizeInBits();
283     }
284
285     /// bitsGE - Return true if this has no less bits than VT.
286     bool bitsGE(MVT VT) const {
287       return getSizeInBits() >= VT.getSizeInBits();
288     }
289
290     /// bitsLT - Return true if this has less bits than VT.
291     bool bitsLT(MVT VT) const {
292       return getSizeInBits() < VT.getSizeInBits();
293     }
294
295     /// bitsLE - Return true if this has no more bits than VT.
296     bool bitsLE(MVT VT) const {
297       return getSizeInBits() <= VT.getSizeInBits();
298     }
299
300
301     /// getSimpleVT - Return the SimpleValueType held in the specified
302     /// simple MVT.
303     SimpleValueType getSimpleVT() const {
304       assert(isSimple() && "Expected a SimpleValueType!");
305       return SimpleValueType(V);
306     }
307
308     /// getVectorElementType - Given a vector type, return the type of
309     /// each element.
310     MVT getVectorElementType() const {
311       assert(isVector() && "Invalid vector type!");
312       switch (V) {
313       default:
314         return getExtendedVectorElementType();
315       case v2i8 :
316       case v4i8 :
317       case v8i8 :
318       case v16i8: return i8;
319       case v2i16:
320       case v4i16:
321       case v8i16: return i16;
322       case v2i32:
323       case v3i32:
324       case v4i32: return i32;
325       case v1i64:
326       case v2i64: return i64;
327       case v2f32:
328       case v3f32:
329       case v4f32: return f32;
330       case v2f64: return f64;
331       }
332     }
333
334     /// getVectorNumElements - Given a vector type, return the number of
335     /// elements it contains.
336     unsigned getVectorNumElements() const {
337       assert(isVector() && "Invalid vector type!");
338       switch (V) {
339       default:
340         return getExtendedVectorNumElements();
341       case v16i8: return 16;
342       case v8i8 :
343       case v8i16: return 8;
344       case v4i8:
345       case v4i16:
346       case v4i32:
347       case v4f32: return 4;
348       case v3i32:
349       case v3f32: return 3;
350       case v2i8:
351       case v2i16:
352       case v2i32:
353       case v2i64:
354       case v2f32:
355       case v2f64: return 2;
356       case v1i64: return 1;
357       }
358     }
359
360     /// getSizeInBits - Return the size of the specified value type in bits.
361     unsigned getSizeInBits() const {
362       switch (V) {
363       case iPTR:
364         assert(0 && "Value type size is target-dependent. Ask TLI.");
365       case iPTRAny:
366       case iAny:
367       case fAny:
368         assert(0 && "Value type is overloaded.");
369       default:
370         return getExtendedSizeInBits();
371       case i1  :  return 1;
372       case i8  :  return 8;
373       case i16 :
374       case v2i8:  return 16;
375       case f32 :
376       case i32 :
377       case v4i8:
378       case v2i16: return 32;
379       case f64 :
380       case i64 :
381       case v8i8:
382       case v4i16:
383       case v2i32:
384       case v1i64:
385       case v2f32: return 64;
386       case f80 :  return 80;
387       case v3i32:
388       case v3f32: return 96;
389       case f128:
390       case ppcf128:
391       case i128:
392       case v16i8:
393       case v8i16:
394       case v4i32:
395       case v2i64:
396       case v4f32:
397       case v2f64: return 128;
398       }
399     }
400
401     /// getStoreSizeInBits - Return the number of bits overwritten by a store
402     /// of the specified value type.
403     unsigned getStoreSizeInBits() const {
404       return (getSizeInBits() + 7)/8*8;
405     }
406
407     /// getRoundIntegerType - Rounds the bit-width of the given integer MVT up
408     /// to the nearest power of two (and at least to eight), and returns the
409     /// integer MVT with that number of bits.
410     MVT getRoundIntegerType() const {
411       assert(isInteger() && !isVector() && "Invalid integer type!");
412       unsigned BitWidth = getSizeInBits();
413       if (BitWidth <= 8)
414         return i8;
415       else
416         return getIntegerVT(1 << Log2_32_Ceil(BitWidth));
417     }
418
419     /// isPow2VectorType - Retuns true if the given vector is a power of 2.
420     bool isPow2VectorType() const {
421       unsigned NElts = getVectorNumElements();
422       return !(NElts & (NElts - 1));
423     }
424
425     /// getPow2VectorType - Widens the length of the given vector MVT up to
426     /// the nearest power of 2 and returns that type.
427     MVT getPow2VectorType() const {
428       if (!isPow2VectorType()) {
429         unsigned NElts = getVectorNumElements();
430         unsigned Pow2NElts = 1 <<  Log2_32_Ceil(NElts);
431         return MVT::getVectorVT(getVectorElementType(), Pow2NElts);
432       }
433       else {
434         return *this;
435       }
436     }
437
438     /// getMVTString - This function returns value type as a string,
439     /// e.g. "i32".
440     std::string getMVTString() const;
441
442     /// getTypeForMVT - This method returns an LLVM type corresponding to the
443     /// specified MVT.  For integer types, this returns an unsigned type.  Note
444     /// that this will abort for types that cannot be represented.
445     const Type *getTypeForMVT() const;
446
447     /// getMVT - Return the value type corresponding to the specified type.
448     /// This returns all pointers as iPTR.  If HandleUnknown is true, unknown
449     /// types are returned as Other, otherwise they are invalid.
450     static MVT getMVT(const Type *Ty, bool HandleUnknown = false);
451
452     /// getRawBits - Represent the type as a bunch of bits.
453     uintptr_t getRawBits() const { return V; }
454
455     /// compareRawBits - A meaningless but well-behaved order, useful for
456     /// constructing containers.
457     struct compareRawBits {
458       bool operator()(MVT L, MVT R) const {
459         return L.getRawBits() < R.getRawBits();
460       }
461     };
462
463   private:
464     // Methods for handling the Extended-type case in functions above.
465     // These are all out-of-line to prevent users of this header file
466     // from having a dependency on Type.h.
467     static MVT getExtendedIntegerVT(unsigned BitWidth);
468     static MVT getExtendedVectorVT(MVT VT, unsigned NumElements);
469     bool isExtendedFloatingPoint() const;
470     bool isExtendedInteger() const;
471     bool isExtendedVector() const;
472     bool isExtended64BitVector() const;
473     bool isExtended128BitVector() const;
474     MVT getExtendedVectorElementType() const;
475     unsigned getExtendedVectorNumElements() const;
476     unsigned getExtendedSizeInBits() const;
477   };
478
479 } // End llvm namespace
480
481 #endif