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