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