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