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