965043a1093365e88b505f9cd22f04ce557aecbf
[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 was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source 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
23 namespace llvm {
24   class Type;
25
26 /// MVT namespace - This namespace defines the SimpleValueType enum, which
27 /// contains the various low-level value types, and the ValueType typedef.
28 ///
29 namespace MVT {  // MVT = Machine Value Types
30   enum SimpleValueType {
31     // If you change this numbering, you must change the values in ValueTypes.td
32     // 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     f32            =   7,   // This is a 32 bit floating point value
42     f64            =   8,   // This is a 64 bit floating point value
43     f80            =   9,   // This is a 80 bit floating point value
44     f128           =  10,   // This is a 128 bit floating point value
45     ppcf128        =  11,   // This is a PPC 128-bit floating point value
46     Flag           =  12,   // This is a condition code or machine flag.
47
48     isVoid         =  13,   // This has no value
49     
50     v8i8           =  14,   //  8 x i8
51     v4i16          =  15,   //  4 x i16
52     v2i32          =  16,   //  2 x i32
53     v1i64          =  17,   //  1 x i64
54     v16i8          =  18,   // 16 x i8
55     v8i16          =  19,   //  8 x i16
56     v3i32           = 20,   //  3 x i32
57     v4i32          =  21,   //  4 x i32
58     v2i64          =  22,   //  2 x i64
59
60     v2f32          =  23,   //  2 x f32
61     v3f32           = 24,   //  3 x f32
62     v4f32          =  25,   //  4 x f32
63     v2f64          =  26,   //  2 x f64
64     
65     FIRST_VECTOR_VALUETYPE = v8i8,
66     LAST_VECTOR_VALUETYPE  = v2f64,
67
68     LAST_VALUETYPE =  27,   // This always remains at the end of the list.
69
70     // fAny - Any floating-point or vector floating-point value. This is used
71     // for intrinsics that have overloadings based on floating-point types.
72     // This is only for tblgen's consumption!
73     fAny           =  253,   
74
75     // iAny - An integer or vector integer value of any bit width. This is
76     // used for intrinsics that have overloadings based on integer bit widths.
77     // This is only for tblgen's consumption!
78     iAny           =  254,   
79
80     // iPTR - An int value the size of the pointer of the current
81     // target.  This should only be used internal to tblgen!
82     iPTR           =  255
83   };
84
85   /// MVT::ValueType - This type holds low-level value types. Valid values
86   /// include any of the values in the SimpleValueType enum, or any value
87   /// returned from a function in the MVT namespace that has a ValueType
88   /// return type. Any value type equal to one of the SimpleValueType enum
89   /// values is a "simple" value type. All other value types are "extended".
90   ///
91   /// Note that simple doesn't necessary mean legal for the target machine.
92   /// All legal value types must be simple, but often there are some simple
93   /// value types that are not legal.
94   ///
95   /// @internal
96   /// Currently extended types are always vector types. Extended types are
97   /// encoded by having the first SimpleTypeBits bits encode the vector
98   /// element type (which must be a scalar type) and the remaining upper
99   /// bits encode the vector length, offset by one.
100   typedef uint32_t ValueType;
101
102   static const int SimpleTypeBits = 8;
103
104   static const uint32_t SimpleTypeMask =
105     (~uint32_t(0) << (32 - SimpleTypeBits)) >> (32 - SimpleTypeBits);
106
107   /// MVT::isExtendedVT - Test if the given ValueType is extended
108   /// (as opposed to being simple).
109   static inline bool isExtendedVT(ValueType VT) {
110     return VT > SimpleTypeMask;
111   }
112
113   /// MVT::isInteger - Return true if this is an integer, or a vector integer
114   /// type.
115   static inline bool isInteger(ValueType VT) {
116     ValueType SVT = VT & SimpleTypeMask;
117     return (SVT >= i1 && SVT <= i128) || (SVT >= v8i8 && SVT <= v2i64);
118   }
119   
120   /// MVT::isFloatingPoint - Return true if this is an FP, or a vector FP type.
121   static inline bool isFloatingPoint(ValueType VT) {
122     ValueType SVT = VT & SimpleTypeMask;
123     return (SVT >= f32 && SVT <= ppcf128) || (SVT >= v2f32 && SVT <= v2f64);
124   }
125   
126   /// MVT::isVector - Return true if this is a vector value type.
127   static inline bool isVector(ValueType VT) {
128     return (VT >= FIRST_VECTOR_VALUETYPE && VT <= LAST_VECTOR_VALUETYPE) ||
129            isExtendedVT(VT);
130   }
131   
132   /// MVT::getVectorElementType - Given a vector type, return the type of
133   /// each element.
134   static inline ValueType getVectorElementType(ValueType VT) {
135     switch (VT) {
136     default:
137       if (isExtendedVT(VT))
138         return VT & SimpleTypeMask;
139       assert(0 && "Invalid vector type!");
140     case v8i8 :
141     case v16i8: return i8;
142     case v4i16:
143     case v8i16: return i16; 
144     case v2i32:
145     case v3i32:
146     case v4i32: return i32;
147     case v1i64:
148     case v2i64: return i64;
149     case v2f32:
150     case v3f32:
151     case v4f32: return f32;
152     case v2f64: return f64;
153     }
154   }
155   
156   /// MVT::getVectorNumElements - Given a vector type, return the
157   /// number of elements it contains.
158   static inline unsigned getVectorNumElements(ValueType VT) {
159     switch (VT) {
160     default:
161       if (isExtendedVT(VT))
162         return ((VT & ~SimpleTypeMask) >> SimpleTypeBits) - 1;
163       assert(0 && "Invalid vector type!");
164     case v16i8: return 16;
165     case v8i8 :
166     case v8i16: return 8;
167     case v4i16:
168     case v4i32: 
169     case v4f32: return 4;
170     case v3i32:
171     case v3f32: return 3;
172     case v2i32:
173     case v2i64:
174     case v2f32:
175     case v2f64: return 2;
176     case v1i64: return 1;
177     }
178   }
179   
180   /// MVT::getSizeInBits - Return the size of the specified value type
181   /// in bits.
182   ///
183   static inline unsigned getSizeInBits(ValueType VT) {
184     switch (VT) {
185     default:
186       if (isExtendedVT(VT))
187         return getSizeInBits(getVectorElementType(VT)) *
188                getVectorNumElements(VT);
189       assert(0 && "ValueType has no known size!");
190     case MVT::i1  :  return 1;
191     case MVT::i8  :  return 8;
192     case MVT::i16 :  return 16;
193     case MVT::f32 :
194     case MVT::i32 :  return 32;
195     case MVT::f64 :
196     case MVT::i64 :
197     case MVT::v8i8:
198     case MVT::v4i16:
199     case MVT::v2i32: 
200     case MVT::v1i64:
201     case MVT::v2f32: return 64;
202     case MVT::f80 :  return 80;
203     case MVT::v3i32:
204     case MVT::v3f32: return 96;
205     case MVT::f128:
206     case MVT::ppcf128:
207     case MVT::i128: 
208     case MVT::v16i8:
209     case MVT::v8i16:
210     case MVT::v4i32:
211     case MVT::v2i64:
212     case MVT::v4f32:
213     case MVT::v2f64: return 128;
214     }
215   }
216   
217   /// MVT::getVectorType - Returns the ValueType that represents a vector
218   /// NumElements in length, where each element is of type VT.
219   ///
220   static inline ValueType getVectorType(ValueType VT, unsigned NumElements) {
221     switch (VT) {
222     default:
223       break;
224     case MVT::i8:
225       if (NumElements == 8)  return MVT::v8i8;
226       if (NumElements == 16) return MVT::v16i8;
227       break;
228     case MVT::i16:
229       if (NumElements == 4)  return MVT::v4i16;
230       if (NumElements == 8)  return MVT::v8i16;
231       break;
232     case MVT::i32:
233       if (NumElements == 2)  return MVT::v2i32;
234       if (NumElements == 3)  return MVT::v3i32;
235       if (NumElements == 4)  return MVT::v4i32;
236       break;
237     case MVT::i64:
238       if (NumElements == 1)  return MVT::v1i64;
239       if (NumElements == 2)  return MVT::v2i64;
240       break;
241     case MVT::f32:
242       if (NumElements == 2)  return MVT::v2f32;
243       if (NumElements == 3)  return MVT::v3f32;
244       if (NumElements == 4)  return MVT::v4f32;
245       break;
246     case MVT::f64:
247       if (NumElements == 2)  return MVT::v2f64;
248       break;
249     }
250     ValueType Result = VT | ((NumElements + 1) << SimpleTypeBits);
251     assert(getVectorElementType(Result) == VT &&
252            "Bad vector element type!");
253     assert(getVectorNumElements(Result) == NumElements &&
254            "Bad vector length!");
255     return Result;
256   }
257
258   /// MVT::getIntVectorWithNumElements - Return any integer vector type that has
259   /// the specified number of elements.
260   static inline ValueType getIntVectorWithNumElements(unsigned NumElts) {
261     switch (NumElts) {
262     default: return getVectorType(i8, NumElts);
263     case  1: return v1i64;
264     case  2: return v2i32;
265     case  3: return v3i32;
266     case  4: return v4i16;
267     case  8: return v8i8;
268     case 16: return v16i8;
269     }
270   }
271   
272   
273   /// MVT::getIntVTBitMask - Return an integer with 1's every place there are
274   /// bits in the specified integer value type.
275   static inline uint64_t getIntVTBitMask(ValueType VT) {
276     assert(isInteger(VT) && !isVector(VT) && "Only applies to int scalars!");
277     return ~uint64_t(0UL) >> (64-getSizeInBits(VT));
278   }
279   /// MVT::getIntVTSignBit - Return an integer with a 1 in the position of the
280   /// sign bit for the specified integer value type.
281   static inline uint64_t getIntVTSignBit(ValueType VT) {
282     assert(isInteger(VT) && !isVector(VT) && "Only applies to int scalars!");
283     return uint64_t(1UL) << (getSizeInBits(VT)-1);
284   }
285
286   /// MVT::getValueTypeString - This function returns value type as a string,
287   /// e.g. "i32".
288   std::string getValueTypeString(ValueType VT);
289
290   /// MVT::getTypeForValueType - This method returns an LLVM type corresponding
291   /// to the specified ValueType.  For integer types, this returns an unsigned
292   /// type.  Note that this will abort for types that cannot be represented.
293   const Type *getTypeForValueType(ValueType VT);
294   
295   /// MVT::getValueType - Return the value type corresponding to the specified
296   /// type.  This returns all pointers as MVT::iPTR.  If HandleUnknown is true,
297   /// unknown types are returned as Other, otherwise they are invalid.
298   ValueType getValueType(const Type *Ty, bool HandleUnknown = false);
299 }
300
301 } // End llvm namespace
302
303 #endif