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