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