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