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