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