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