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