Renumber the SimpleValueType values to fill in the hole left by
[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   typedef uint32_t ValueType;
86
87   static const int SimpleTypeBits = 8;
88
89   static const uint32_t SimpleTypeMask =
90     (~uint32_t(0) << (32 - SimpleTypeBits)) >> (32 - SimpleTypeBits);
91
92   /// MVT::isExtendedValueType - Test if the given ValueType is extended
93   /// (as opposed to being simple).
94   static inline bool isExtendedValueType(ValueType VT) {
95     return VT & ~SimpleTypeMask;
96   }
97
98   /// MVT::isInteger - Return true if this is an integer, or a vector integer
99   /// type.
100   static inline bool isInteger(ValueType VT) {
101     ValueType SVT = VT & SimpleTypeMask;
102     return (SVT >= i1 && SVT <= i128) || (SVT >= v8i8 && SVT <= v2i64);
103   }
104   
105   /// MVT::isFloatingPoint - Return true if this is an FP, or a vector FP type.
106   static inline bool isFloatingPoint(ValueType VT) {
107     ValueType SVT = VT & SimpleTypeMask;
108     return (SVT >= f32 && SVT <= f128) || (SVT >= v2f32 && SVT <= v2f64);
109   }
110   
111   /// MVT::isVector - Return true if this is a vector value type.
112   static inline bool isVector(ValueType VT) {
113     return (VT >= FIRST_VECTOR_VALUETYPE && VT <= LAST_VECTOR_VALUETYPE) ||
114            isExtendedValueType(VT);
115   }
116   
117   /// MVT::getVectorElementType - Given a vector type, return the type of
118   /// each element.
119   static inline ValueType getVectorElementType(ValueType VT) {
120     switch (VT) {
121     default:
122       if (isExtendedValueType(VT))
123         return VT & SimpleTypeMask;
124       assert(0 && "Invalid vector type!");
125     case v8i8 :
126     case v16i8: return i8;
127     case v4i16:
128     case v8i16: return i16; 
129     case v2i32:
130     case v4i32: return i32;
131     case v1i64:
132     case v2i64: return i64;
133     case v2f32:
134     case v4f32: return f32;
135     case v2f64: return f64;
136     }
137   }
138   
139   /// MVT::getVectorNumElements - Given a vector type, return the
140   /// number of elements it contains.
141   static inline unsigned getVectorNumElements(ValueType VT) {
142     switch (VT) {
143     default:
144       if (isExtendedValueType(VT))
145         return ((VT & ~SimpleTypeMask) >> SimpleTypeBits) - 1;
146       assert(0 && "Invalid vector type!");
147     case v16i8: return 16;
148     case v8i8 :
149     case v8i16: return 8;
150     case v4i16:
151     case v4i32: 
152     case v4f32: return 4;
153     case v2i32:
154     case v2i64:
155     case v2f32:
156     case v2f64: return 2;
157     case v1i64: return 1;
158     }
159   }
160   
161   /// MVT::getSizeInBits - Return the size of the specified value type
162   /// in bits.
163   ///
164   static inline unsigned getSizeInBits(ValueType VT) {
165     switch (VT) {
166     default:
167       if (isExtendedValueType(VT))
168         return getSizeInBits(getVectorElementType(VT)) *
169                getVectorNumElements(VT);
170       assert(0 && "ValueType has no known size!");
171     case MVT::i1  :  return 1;
172     case MVT::i8  :  return 8;
173     case MVT::i16 :  return 16;
174     case MVT::f32 :
175     case MVT::i32 :  return 32;
176     case MVT::f64 :
177     case MVT::i64 :
178     case MVT::v8i8:
179     case MVT::v4i16:
180     case MVT::v2i32: 
181     case MVT::v1i64:
182     case MVT::v2f32: return 64;
183     case MVT::f80 :  return 80;
184     case MVT::f128:
185     case MVT::i128: 
186     case MVT::v16i8:
187     case MVT::v8i16:
188     case MVT::v4i32:
189     case MVT::v2i64:
190     case MVT::v4f32:
191     case MVT::v2f64: return 128;
192     }
193   }
194   
195   /// MVT::getVectorType - Returns the ValueType that represents a vector
196   /// NumElements in length, where each element is of type VT.
197   ///
198   static inline ValueType getVectorType(ValueType VT, unsigned NumElements) {
199     switch (VT) {
200     default:
201       break;
202     case MVT::i8:
203       if (NumElements == 8)  return MVT::v8i8;
204       if (NumElements == 16) return MVT::v16i8;
205       break;
206     case MVT::i16:
207       if (NumElements == 4)  return MVT::v4i16;
208       if (NumElements == 8)  return MVT::v8i16;
209       break;
210     case MVT::i32:
211       if (NumElements == 2)  return MVT::v2i32;
212       if (NumElements == 4)  return MVT::v4i32;
213       break;
214     case MVT::i64:
215       if (NumElements == 1)  return MVT::v1i64;
216       if (NumElements == 2)  return MVT::v2i64;
217       break;
218     case MVT::f32:
219       if (NumElements == 2)  return MVT::v2f32;
220       if (NumElements == 4)  return MVT::v4f32;
221       break;
222     case MVT::f64:
223       if (NumElements == 2)  return MVT::v2f64;
224       break;
225     }
226     ValueType Result = VT | ((NumElements + 1) << SimpleTypeBits);
227     assert(getVectorElementType(Result) == VT &&
228            "Bad vector element type!");
229     assert(getVectorNumElements(Result) == NumElements &&
230            "Bad vector length!");
231     return Result;
232   }
233
234   /// MVT::getIntVectorWithNumElements - Return any integer vector type that has
235   /// the specified number of elements.
236   static inline ValueType getIntVectorWithNumElements(unsigned NumElts) {
237     switch (NumElts) {
238     default: return getVectorType(i8, NumElts);
239     case  1: return v1i64;
240     case  2: return v2i32;
241     case  4: return v4i16;
242     case  8: return v8i8;
243     case 16: return v16i8;
244     }
245   }
246   
247   
248   /// MVT::getIntVTBitMask - Return an integer with 1's every place there are
249   /// bits in the specified integer value type.
250   static inline uint64_t getIntVTBitMask(ValueType VT) {
251     assert(isInteger(VT) && !isVector(VT) && "Only applies to int scalars!");
252     return ~uint64_t(0UL) >> (64-getSizeInBits(VT));
253   }
254   /// MVT::getIntVTSignBit - Return an integer with a 1 in the position of the
255   /// sign bit for the specified integer value type.
256   static inline uint64_t getIntVTSignBit(ValueType VT) {
257     assert(isInteger(VT) && !isVector(VT) && "Only applies to int scalars!");
258     return uint64_t(1UL) << (getSizeInBits(VT)-1);
259   }
260
261   /// MVT::getValueTypeString - This function returns value type as a string,
262   /// e.g. "i32".
263   std::string getValueTypeString(ValueType VT);
264
265   /// MVT::getTypeForValueType - This method returns an LLVM type corresponding
266   /// to the specified ValueType.  For integer types, this returns an unsigned
267   /// type.  Note that this will abort for types that cannot be represented.
268   const Type *getTypeForValueType(ValueType VT);
269   
270   /// MVT::getValueType - Return the value type corresponding to the specified
271   /// type.  This returns all pointers as MVT::iPTR.  If HandleUnknown is true,
272   /// unknown types are returned as Other, otherwise they are invalid.
273   ValueType getValueType(const Type *Ty, bool HandleUnknown = false);
274 }
275
276 } // End llvm namespace
277
278 #endif