Wrap MVT::ValueType in a struct to get type safety
[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
29     enum SimpleValueType {
30       // If you change this numbering, you must change the values in
31       // ValueTypes.td 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       v8i8           =  14,   //  8 x i8
53       v4i16          =  15,   //  4 x i16
54       v2i32          =  16,   //  2 x i32
55       v1i64          =  17,   //  1 x i64
56       v16i8          =  18,   // 16 x i8
57       v8i16          =  19,   //  8 x i16
58       v3i32          =  20,   //  3 x i32
59       v4i32          =  21,   //  4 x i32
60       v2i64          =  22,   //  2 x i64
61
62       v2f32          =  23,   //  2 x f32
63       v3f32          =  24,   //  3 x f32
64       v4f32          =  25,   //  4 x f32
65       v2f64          =  26,   //  2 x f64
66
67       FIRST_VECTOR_VALUETYPE = v8i8,
68       LAST_VECTOR_VALUETYPE  = v2f64,
69
70       LAST_VALUETYPE =  27,   // This always remains at the end of the list.
71
72       // fAny - Any floating-point or vector floating-point value. This is used
73       // for intrinsics that have overloadings based on floating-point types.
74       // This is only for tblgen's consumption!
75       fAny           =  253,
76
77       // iAny - An integer or vector integer value of any bit width. This is
78       // used for intrinsics that have overloadings based on integer bit widths.
79       // This is only for tblgen's consumption!
80       iAny           =  254,
81
82       // iPTR - An int value the size of the pointer of the current
83       // target.  This should only be used internal to tblgen!
84       iPTR           =  255
85     };
86
87     /// MVT - This type holds low-level value types. Valid values include any of
88     /// the values in the SimpleValueType enum, or any value returned from one
89     /// of the MVT methods.  Any value type equal to one of the SimpleValueType
90     /// enum values is a "simple" value type.  All others are "extended".
91     ///
92     /// Note that simple doesn't necessary mean legal for the target machine.
93     /// All legal value types must be simple, but often there are some simple
94     /// value types that are not legal.
95     ///
96     /// @internal
97     /// Extended types are either vector types or arbitrary precision integers.
98     /// Arbitrary precision integers have iAny in the first SimpleTypeBits bits,
99     /// and the bit-width in the next PrecisionBits bits, offset by minus one.
100     /// Vector types are encoded by having the first SimpleTypeBits+PrecisionBits
101     /// bits encode the vector element type (which must be a scalar type, possibly
102     /// an arbitrary precision integer) and the remaining VectorBits upper bits
103     /// encode the vector length, offset by one.
104     ///
105     /// 31--------------16-----------8-------------0
106     ///  | Vector length | Precision | Simple type |
107     ///  |               |      Vector element     |
108     ///
109
110     static const int SimpleTypeBits = 8;
111     static const int PrecisionBits  = 8;
112     static const int VectorBits     = 32 - SimpleTypeBits - PrecisionBits;
113
114     static const uint32_t SimpleTypeMask =
115       (~uint32_t(0) << (32 - SimpleTypeBits)) >> (32 - SimpleTypeBits);
116
117     static const uint32_t PrecisionMask =
118       ((~uint32_t(0) << VectorBits) >> (32 - PrecisionBits)) << SimpleTypeBits;
119
120     static const uint32_t VectorMask =
121       (~uint32_t(0) >> (32 - VectorBits)) << (32 - VectorBits);
122
123     static const uint32_t ElementMask =
124       (~uint32_t(0) << VectorBits) >> VectorBits;
125
126     uint32_t V;
127
128     MVT() {}
129     MVT(SimpleValueType S) { V = S; }
130     inline bool operator== (const MVT VT) const { return V == VT.V; }
131     inline bool operator!= (const MVT VT) const { return V != VT.V; }
132
133     /// FIXME: The following comparison methods are bogus - they are only here
134     /// to ease the transition to a struct type.
135     inline bool operator<  (const MVT VT) const { return V <  VT.V; }
136     inline bool operator<= (const MVT VT) const { return V <= VT.V; }
137     inline bool operator>  (const MVT VT) const { return V >  VT.V; }
138     inline bool operator>= (const MVT VT) const { return V >= VT.V; }
139
140     /// getIntegerVT - Returns the MVT that represents an integer with the given
141     /// number of bits.
142     static inline MVT getIntegerVT(unsigned BitWidth) {
143       switch (BitWidth) {
144       default:
145         break;
146       case 1:
147         return i1;
148       case 8:
149         return i8;
150       case 16:
151         return i16;
152       case 32:
153         return i32;
154       case 64:
155         return i64;
156       case 128:
157         return i128;
158       }
159       MVT VT;
160       VT.V = iAny | (((BitWidth - 1) << SimpleTypeBits) & PrecisionMask);
161       assert(VT.getSizeInBits() == BitWidth && "Bad bit width!");
162       return VT;
163     }
164
165     /// getVectorVT - Returns the MVT that represents a vector NumElements in
166     /// length, where each element is of type VT.
167     static inline MVT getVectorVT(MVT VT, unsigned NumElements) {
168       switch (VT.V) {
169       default:
170         break;
171       case i8:
172         if (NumElements == 8)  return v8i8;
173         if (NumElements == 16) return v16i8;
174         break;
175       case i16:
176         if (NumElements == 4)  return v4i16;
177         if (NumElements == 8)  return v8i16;
178         break;
179       case i32:
180         if (NumElements == 2)  return v2i32;
181         if (NumElements == 3)  return v3i32;
182         if (NumElements == 4)  return v4i32;
183         break;
184       case i64:
185         if (NumElements == 1)  return v1i64;
186         if (NumElements == 2)  return v2i64;
187         break;
188       case f32:
189         if (NumElements == 2)  return v2f32;
190         if (NumElements == 3)  return v3f32;
191         if (NumElements == 4)  return v4f32;
192         break;
193       case f64:
194         if (NumElements == 2)  return v2f64;
195         break;
196       }
197       // Set the length with the top bit forced to zero (needed by the verifier).
198       MVT Result;
199       Result.V = VT.V | (((NumElements + 1) << (33 - VectorBits)) >> 1);
200       assert(Result.getVectorElementType() == VT &&
201              "Bad vector element type!");
202       assert(Result.getVectorNumElements() == NumElements &&
203              "Bad vector length!");
204       return Result;
205     }
206
207     /// getIntVectorWithNumElements - Return any integer vector type that has
208     /// the specified number of elements.
209     static inline MVT getIntVectorWithNumElements(unsigned NumElts) {
210       switch (NumElts) {
211       default: return getVectorVT(i8, NumElts);
212       case  1: return v1i64;
213       case  2: return v2i32;
214       case  3: return v3i32;
215       case  4: return v4i16;
216       case  8: return v8i8;
217       case 16: return v16i8;
218       }
219     }
220
221
222     /// isSimple - Test if the given MVT is simple (as opposed to being
223     /// extended).
224     inline bool isSimple() const {
225       return V <= SimpleTypeMask;
226     }
227
228     /// isExtended - Test if the given MVT is extended (as opposed to
229     /// being simple).
230     inline bool isExtended() const {
231       return !isSimple();
232     }
233
234     /// isFloatingPoint - Return true if this is a FP, or a vector FP type.
235     inline bool isFloatingPoint() const {
236       uint32_t SVT = V & SimpleTypeMask;
237       return (SVT >= f32 && SVT <= ppcf128) || (SVT >= v2f32 && SVT <= v2f64);
238     }
239
240     /// isInteger - Return true if this is an integer, or a vector integer type.
241     inline bool isInteger() const {
242       uint32_t SVT = V & SimpleTypeMask;
243       return (SVT >= FIRST_INTEGER_VALUETYPE && SVT <= LAST_INTEGER_VALUETYPE) ||
244         (SVT >= v8i8 && SVT <= v2i64) || (SVT == iAny && (V & PrecisionMask));
245     }
246
247     /// isVector - Return true if this is a vector value type.
248     inline bool isVector() const {
249       return (V >= FIRST_VECTOR_VALUETYPE && V <= LAST_VECTOR_VALUETYPE) ||
250              (V & VectorMask);
251     }
252
253     /// is64BitVector - Return true if this is a 64-bit vector type.
254     inline bool is64BitVector() const {
255       return (V==v8i8 || V==v4i16 || V==v2i32 || V==v1i64 || V==v2f32 ||
256               (isExtended() && isVector() && getSizeInBits()==64));
257     }
258
259     /// is128BitVector - Return true if this is a 128-bit vector type.
260     inline bool is128BitVector() const {
261       return (V==v16i8 || V==v8i16 || V==v4i32 || V==v2i64 ||
262               V==v4f32 || V==v2f64 ||
263               (isExtended() && isVector() && getSizeInBits()==128));
264     }
265
266
267     /// getSimpleVT - Return the SimpleValueType held in the specified
268     /// simple MVT.
269     inline SimpleValueType getSimpleVT() const {
270       assert(isSimple() && "Expected a SimpleValueType!");
271       return (SimpleValueType)V;
272     }
273
274     /// getVectorElementType - Given a vector type, return the type of
275     /// each element.
276     inline MVT getVectorElementType() const {
277       assert(isVector() && "Invalid vector type!");
278       switch (V) {
279       default: {
280         assert(isExtended() && "Unknown simple vector type!");
281         MVT VT;
282         VT.V = V & ElementMask;
283         return VT;
284       }
285       case v8i8 :
286       case v16i8: return i8;
287       case v4i16:
288       case v8i16: return i16;
289       case v2i32:
290       case v3i32:
291       case v4i32: return i32;
292       case v1i64:
293       case v2i64: return i64;
294       case v2f32:
295       case v3f32:
296       case v4f32: return f32;
297       case v2f64: return f64;
298       }
299     }
300
301     /// getVectorNumElements - Given a vector type, return the number of
302     /// elements it contains.
303     inline unsigned getVectorNumElements() const {
304       assert(isVector() && "Invalid vector type!");
305       switch (V) {
306       default:
307         assert(isExtended() && "Unknown simple vector type!");
308         return ((V & VectorMask) >> (32 - VectorBits)) - 1;
309       case v16i8: return 16;
310       case v8i8 :
311       case v8i16: return 8;
312       case v4i16:
313       case v4i32:
314       case v4f32: return 4;
315       case v3i32:
316       case v3f32: return 3;
317       case v2i32:
318       case v2i64:
319       case v2f32:
320       case v2f64: return 2;
321       case v1i64: return 1;
322       }
323     }
324
325     /// getSizeInBits - Return the size of the specified value type in bits.
326     inline unsigned getSizeInBits() const {
327       switch (V) {
328       default:
329         assert(isExtended() && "MVT has no known size!");
330         if (isVector())
331           return getVectorElementType().getSizeInBits()*getVectorNumElements();
332         if (isInteger())
333           return ((V & PrecisionMask) >> SimpleTypeBits) + 1;
334         assert(false && "Unknown value type!");
335         return 0;
336       case i1  :  return 1;
337       case i8  :  return 8;
338       case i16 :  return 16;
339       case f32 :
340       case i32 :  return 32;
341       case f64 :
342       case i64 :
343       case v8i8:
344       case v4i16:
345       case v2i32:
346       case v1i64:
347       case v2f32: return 64;
348       case f80 :  return 80;
349       case v3i32:
350       case v3f32: return 96;
351       case f128:
352       case ppcf128:
353       case i128:
354       case v16i8:
355       case v8i16:
356       case v4i32:
357       case v2i64:
358       case v4f32:
359       case v2f64: return 128;
360       }
361     }
362
363     /// getStoreSizeInBits - Return the number of bits overwritten by a store
364     /// of the specified value type.
365     inline unsigned getStoreSizeInBits() const {
366       return (getSizeInBits() + 7)/8*8;
367     }
368
369     /// getRoundIntegerType - Rounds the bit-width of the given integer MVT up
370     /// to the nearest power of two (and at least to eight), and returns the
371     /// integer MVT with that number of bits.
372     inline MVT getRoundIntegerType() const {
373       assert(isInteger() && !isVector() && "Invalid integer type!");
374       unsigned BitWidth = getSizeInBits();
375       if (BitWidth <= 8)
376         return i8;
377       else
378         return getIntegerVT(1 << Log2_32_Ceil(BitWidth));
379     }
380
381     /// getIntegerVTBitMask - Return an integer with 1's every place there are
382     /// bits in the specified integer value type. FIXME: Should return an apint.
383     inline uint64_t getIntegerVTBitMask() const {
384       assert(isInteger() && !isVector() && "Only applies to int scalars!");
385       return ~uint64_t(0UL) >> (64-getSizeInBits());
386     }
387
388     /// getIntegerVTSignBit - Return an integer with a 1 in the position of the
389     /// sign bit for the specified integer value type. FIXME: Should return an
390     /// apint.
391     inline uint64_t getIntegerVTSignBit() const {
392       assert(isInteger() && !isVector() && "Only applies to int scalars!");
393       return uint64_t(1UL) << (getSizeInBits()-1);
394     }
395
396     /// getMVTString - This function returns value type as a string,
397     /// e.g. "i32".
398     std::string getMVTString() const;
399
400     /// getTypeForMVT - This method returns an LLVM type corresponding to the
401     /// specified MVT.  For integer types, this returns an unsigned type.  Note
402     /// that this will abort for types that cannot be represented.
403     const Type *getTypeForMVT() const;
404
405     /// getMVT - Return the value type corresponding to the specified type.
406     /// This returns all pointers as iPTR.  If HandleUnknown is true, unknown
407     /// types are returned as Other, otherwise they are invalid.
408     static MVT getMVT(const Type *Ty, bool HandleUnknown = false);
409   };
410
411 } // End llvm namespace
412
413 #endif