Add a helper method
[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 "llvm/Support/DataTypes.h"
21
22 namespace llvm {
23   class Type;
24
25 /// MVT namespace - This namespace defines the ValueType enum, which contains
26 /// the various low-level value types.
27 ///
28 namespace MVT {  // MVT = Machine Value Types
29   enum ValueType {
30     // If you change this numbering, you must change the values in ValueTypes.td
31     // well!
32     Other          =   0,   // This is a non-standard value
33     i1             =   1,   // This is a 1 bit integer value
34     i8             =   2,   // This is an 8 bit integer value
35     i16            =   3,   // This is a 16 bit integer value
36     i32            =   4,   // This is a 32 bit integer value
37     i64            =   5,   // This is a 64 bit integer value
38     i128           =   6,   // This is a 128 bit integer value
39
40     f32            =   7,   // This is a 32 bit floating point value
41     f64            =   8,   // This is a 64 bit floating point value
42     f80            =   9,   // This is a 80 bit floating point value
43     f128           =  10,   // This is a 128 bit floating point value
44     Flag           =  11,   // This is a condition code or machine flag.
45
46     isVoid         =  12,   // This has no value
47     
48     Vector         =  13,   // This is an abstract vector type, which will
49                             // be expanded into a target vector type, or scalars
50                             // if no matching vector type is available.
51
52     v8i8           =  14,   //  8 x i8
53     v4i16          =  15,   //  4 x i16
54     v2i32          =  16,   //  2 x i32
55     v16i8          =  17,   // 16 x i8
56     v8i16          =  18,   //  8 x i16
57     v4i32          =  19,   //  4 x i32
58     v2i64          =  20,   //  2 x i64
59
60     v2f32          =  21,   //  2 x f32
61     v4f32          =  22,   //  4 x f32
62     v2f64          =  23,   //  2 x f64
63     FIRST_VECTOR_VALUETYPE = v8i8,
64     LAST_VECTOR_VALUETYPE  = v2f64,
65
66     LAST_VALUETYPE =  24    // This always remains at the end of the list.
67   };
68
69   /// MVT::isInteger - Return true if this is a simple integer, or a packed
70   /// vector integer type.
71   static inline bool isInteger(ValueType VT) {
72     return (VT >= i1 && VT <= i128) || (VT >= v8i8 && VT <= v2i64);
73   }
74
75   /// MVT::isFloatingPoint - Return true if this is a simple FP, or a packed
76   /// vector FP type.
77   static inline bool isFloatingPoint(ValueType VT) {
78     return (VT >= f32 && VT <= f128) || (VT >= v4f32 && VT <= v2f64);
79   }
80   
81   /// MVT::isVector - Return true if this is a packed vector type (i.e. not 
82   /// MVT::Vector).
83   static inline bool isVector(ValueType VT) {
84     return VT >= FIRST_VECTOR_VALUETYPE && VT <= LAST_VECTOR_VALUETYPE;
85   }
86   
87   /// MVT::getSizeInBits - Return the size of the specified value type in bits.
88   ///
89   static inline unsigned getSizeInBits(ValueType VT) {
90     switch (VT) {
91     default: assert(0 && "ValueType has no known size!");
92     case MVT::i1  :  return 1;
93     case MVT::i8  :  return 8;
94     case MVT::i16 :  return 16;
95     case MVT::f32 :
96     case MVT::i32 :  return 32;
97     case MVT::f64 :
98     case MVT::i64 :
99     case MVT::v8i8:
100     case MVT::v4i16:
101     case MVT::v2i32: 
102     case MVT::v2f32: return 64;
103     case MVT::f80 :  return 80;
104     case MVT::f128:
105     case MVT::i128: 
106     case MVT::v16i8:
107     case MVT::v8i16:
108     case MVT::v4i32:
109     case MVT::v2i64:
110     case MVT::v4f32:
111     case MVT::v2f64: return 128;
112     }
113   }
114   
115   /// MVT::getVectorType - Returns the ValueType that represents a vector
116   /// NumElements in length, where each element is of type VT.  If there is no
117   /// ValueType that represents this vector, a ValueType of Other is returned.
118   ///
119   static inline ValueType getVectorType(ValueType VT, unsigned NumElements) {
120     switch (VT) {
121     default: 
122       break;
123     case MVT::i8:
124       if (NumElements == 8)  return MVT::v8i8;
125       if (NumElements == 16) return MVT::v16i8;
126         break;
127     case MVT::i16:
128       if (NumElements == 4)  return MVT::v4i16;
129       if (NumElements == 8)  return MVT::v8i16;
130         break;
131     case MVT::i32:
132       if (NumElements == 2)  return MVT::v2i32;
133       if (NumElements == 4)  return MVT::v4i32;
134         break;
135     case MVT::f32:
136       if (NumElements == 2)  return MVT::v2f32;
137       if (NumElements == 4)  return MVT::v4f32;
138         break;
139     case MVT::f64:
140       if (NumElements == 2)  return MVT::v2f64;
141       break;
142     }
143     return MVT::Other;
144   }
145   
146   /// MVT::getVectorBaseType - Given a packed vector type, return the type of
147   /// each element.
148   static inline ValueType getVectorBaseType(ValueType VT) {
149     switch (VT) {
150     default: assert(0 && "Invalid vector type!");
151     case v8i8 :
152     case v16i8: return i8;
153     case v4i16:
154     case v8i16: return i16; 
155     case v2i32:
156     case v4i32: return i32;
157     case v2i64: return i64;
158     case v2f32:
159     case v4f32: return f32;
160     case v2f64: return f64;
161     }
162   }
163   
164   /// MVT::getVectorNumElements - Given a packed vector type, return the number
165   /// of elements it contains.
166   static inline unsigned getVectorNumElements(ValueType VT) {
167     switch (VT) {
168       default: assert(0 && "Invalid vector type!");
169       case v16i8: return 16;
170       case v8i8 :
171       case v8i16: return 8;
172       case v4i16:
173       case v4i32: 
174       case v4f32: return 4;
175       case v2i32:
176       case v2i64:
177       case v2f32:
178       case v2f64: return 2;
179     }
180   }
181   
182   /// MVT::getIntVTBitMask - Return an integer with 1's every place there are
183   /// bits in the specified integer value type.
184   static inline uint64_t getIntVTBitMask(ValueType VT) {
185     assert(isInteger(VT) && !isVector(VT) && "Only applies to int scalars!");
186     return ~0ULL >> (64-getSizeInBits(VT));
187   }
188   /// MVT::getIntVTSignBit - Return an integer with a 1 in the position of the
189   /// sign bit for the specified integer value type.
190   static inline uint64_t getIntVTSignBit(ValueType VT) {
191     assert(isInteger(VT) && !isVector(VT) && "Only applies to int scalars!");
192     return 1ULL << (getSizeInBits(VT)-1);
193   }
194
195   /// MVT::getValueTypeString - This function returns value type as a string,
196   /// e.g. "i32".
197   const char *getValueTypeString(ValueType VT);
198
199   /// MVT::getTypeForValueType - This method returns an LLVM type corresponding
200   /// to the specified ValueType.  For integer types, this returns an unsigned
201   /// type.  Note that this will abort for types that cannot be represented.
202   const Type *getTypeForValueType(ValueType VT);
203 }
204
205 } // End llvm namespace
206
207 #endif