Initial support for carrying MachineInstrs in SUnits.
[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     /// bitsEq - Return true if this has the same number of bits as VT.
276     bool bitsEq(MVT VT) const {
277       return getSizeInBits() == VT.getSizeInBits();
278     }
279
280     /// bitsGT - Return true if this has more bits than VT.
281     bool bitsGT(MVT VT) const {
282       return getSizeInBits() > VT.getSizeInBits();
283     }
284
285     /// bitsGE - Return true if this has no less bits than VT.
286     bool bitsGE(MVT VT) const {
287       return getSizeInBits() >= VT.getSizeInBits();
288     }
289
290     /// bitsLT - Return true if this has less bits than VT.
291     bool bitsLT(MVT VT) const {
292       return getSizeInBits() < VT.getSizeInBits();
293     }
294
295     /// bitsLE - Return true if this has no more bits than VT.
296     bool bitsLE(MVT VT) const {
297       return getSizeInBits() <= VT.getSizeInBits();
298     }
299
300
301     /// getSimpleVT - Return the SimpleValueType held in the specified
302     /// simple MVT.
303     SimpleValueType getSimpleVT() const {
304       assert(isSimple() && "Expected a SimpleValueType!");
305       return SimpleTy;
306     }
307
308     /// getVectorElementType - Given a vector type, return the type of
309     /// each element.
310     MVT getVectorElementType() const {
311       assert(isVector() && "Invalid vector type!");
312       switch (V) {
313       default:
314         return getExtendedVectorElementType();
315       case v8i8 :
316       case v16i8: return i8;
317       case v4i16:
318       case v8i16: return i16;
319       case v2i32:
320       case v3i32:
321       case v4i32: return i32;
322       case v1i64:
323       case v2i64: return i64;
324       case v2f32:
325       case v3f32:
326       case v4f32: return f32;
327       case v2f64: return f64;
328       }
329     }
330
331     /// getVectorNumElements - Given a vector type, return the number of
332     /// elements it contains.
333     unsigned getVectorNumElements() const {
334       assert(isVector() && "Invalid vector type!");
335       switch (V) {
336       default:
337         return getExtendedVectorNumElements();
338       case v16i8: return 16;
339       case v8i8 :
340       case v8i16: return 8;
341       case v4i16:
342       case v4i32:
343       case v4f32: return 4;
344       case v3i32:
345       case v3f32: return 3;
346       case v2i32:
347       case v2i64:
348       case v2f32:
349       case v2f64: return 2;
350       case v1i64: return 1;
351       }
352     }
353
354     /// getSizeInBits - Return the size of the specified value type in bits.
355     unsigned getSizeInBits() const {
356       switch (V) {
357       case iPTR:
358         assert(0 && "Value type size is target-dependent. Ask TLI.");
359       case iPTRAny:
360       case iAny:
361       case fAny:
362         assert(0 && "Value type is overloaded.");
363       default:
364         return getExtendedSizeInBits();
365       case i1  :  return 1;
366       case i8  :  return 8;
367       case i16 :  return 16;
368       case f32 :
369       case i32 :  return 32;
370       case f64 :
371       case i64 :
372       case v8i8:
373       case v4i16:
374       case v2i32:
375       case v1i64:
376       case v2f32: return 64;
377       case f80 :  return 80;
378       case v3i32:
379       case v3f32: return 96;
380       case f128:
381       case ppcf128:
382       case i128:
383       case v16i8:
384       case v8i16:
385       case v4i32:
386       case v2i64:
387       case v4f32:
388       case v2f64: return 128;
389       }
390     }
391
392     /// getStoreSizeInBits - Return the number of bits overwritten by a store
393     /// of the specified value type.
394     unsigned getStoreSizeInBits() const {
395       return (getSizeInBits() + 7)/8*8;
396     }
397
398     /// getRoundIntegerType - Rounds the bit-width of the given integer MVT up
399     /// to the nearest power of two (and at least to eight), and returns the
400     /// integer MVT with that number of bits.
401     MVT getRoundIntegerType() const {
402       assert(isInteger() && !isVector() && "Invalid integer type!");
403       unsigned BitWidth = getSizeInBits();
404       if (BitWidth <= 8)
405         return i8;
406       else
407         return getIntegerVT(1 << Log2_32_Ceil(BitWidth));
408     }
409
410     /// getIntegerVTBitMask - Return an integer with 1's every place there are
411     /// bits in the specified integer value type. FIXME: Should return an apint.
412     uint64_t getIntegerVTBitMask() const {
413       assert(isInteger() && !isVector() && "Only applies to int scalars!");
414       return ~uint64_t(0UL) >> (64-getSizeInBits());
415     }
416
417     /// getIntegerVTSignBit - Return an integer with a 1 in the position of the
418     /// sign bit for the specified integer value type. FIXME: Should return an
419     /// apint.
420     uint64_t getIntegerVTSignBit() const {
421       assert(isInteger() && !isVector() && "Only applies to int scalars!");
422       return uint64_t(1UL) << (getSizeInBits()-1);
423     }
424
425     /// getMVTString - This function returns value type as a string,
426     /// e.g. "i32".
427     std::string getMVTString() const;
428
429     /// getTypeForMVT - This method returns an LLVM type corresponding to the
430     /// specified MVT.  For integer types, this returns an unsigned type.  Note
431     /// that this will abort for types that cannot be represented.
432     const Type *getTypeForMVT() const;
433
434     /// getMVT - Return the value type corresponding to the specified type.
435     /// This returns all pointers as iPTR.  If HandleUnknown is true, unknown
436     /// types are returned as Other, otherwise they are invalid.
437     static MVT getMVT(const Type *Ty, bool HandleUnknown = false);
438
439     /// getRawBits - Represent the type as a bunch of bits.
440     uintptr_t getRawBits() const { return V; }
441
442     /// compareRawBits - A meaningless but well-behaved order, useful for
443     /// constructing containers.
444     struct compareRawBits {
445       bool operator()(MVT L, MVT R) const {
446         return L.getRawBits() < R.getRawBits();
447       }
448     };
449
450   private:
451     // Methods for handling the Extended-type case in functions above.
452     // These are all out-of-line to prevent users of this header file
453     // from having a dependency on Type.h.
454     static MVT getExtendedIntegerVT(unsigned BitWidth);
455     static MVT getExtendedVectorVT(MVT VT, unsigned NumElements);
456     bool isExtendedFloatingPoint() const;
457     bool isExtendedInteger() const;
458     bool isExtendedVector() const;
459     bool isExtended64BitVector() const;
460     bool isExtended128BitVector() const;
461     MVT getExtendedVectorElementType() const;
462     unsigned getExtendedVectorNumElements() const;
463     unsigned getExtendedSizeInBits() const;
464   };
465
466 } // End llvm namespace
467
468 #endif