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