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