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