d22fc52b6ac98acf42b0f15a9890b1d26db153cb
[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     // iPTR - An int value the size of the pointer of the current
69     // target.  This should only be used internal to tblgen!
70     iPTR           = 255
71   };
72
73   /// MVT::isInteger - Return true if this is a simple integer, or a packed
74   /// vector integer type.
75   static inline bool isInteger(ValueType VT) {
76     return (VT >= i1 && VT <= i128) || (VT >= v8i8 && VT <= v2i64);
77   }
78
79   /// MVT::isFloatingPoint - Return true if this is a simple FP, or a packed
80   /// vector FP type.
81   static inline bool isFloatingPoint(ValueType VT) {
82     return (VT >= f32 && VT <= f128) || (VT >= v4f32 && VT <= v2f64);
83   }
84   
85   /// MVT::isVector - Return true if this is a packed vector type (i.e. not 
86   /// MVT::Vector).
87   static inline bool isVector(ValueType VT) {
88     return VT >= FIRST_VECTOR_VALUETYPE && VT <= LAST_VECTOR_VALUETYPE;
89   }
90   
91   /// MVT::getSizeInBits - Return the size of the specified value type in bits.
92   ///
93   static inline unsigned getSizeInBits(ValueType VT) {
94     switch (VT) {
95     default: assert(0 && "ValueType has no known size!");
96     case MVT::i1  :  return 1;
97     case MVT::i8  :  return 8;
98     case MVT::i16 :  return 16;
99     case MVT::f32 :
100     case MVT::i32 :  return 32;
101     case MVT::f64 :
102     case MVT::i64 :
103     case MVT::v8i8:
104     case MVT::v4i16:
105     case MVT::v2i32: 
106     case MVT::v2f32: return 64;
107     case MVT::f80 :  return 80;
108     case MVT::f128:
109     case MVT::i128: 
110     case MVT::v16i8:
111     case MVT::v8i16:
112     case MVT::v4i32:
113     case MVT::v2i64:
114     case MVT::v4f32:
115     case MVT::v2f64: return 128;
116     }
117   }
118   
119   /// MVT::getVectorType - Returns the ValueType that represents a vector
120   /// NumElements in length, where each element is of type VT.  If there is no
121   /// ValueType that represents this vector, a ValueType of Other is returned.
122   ///
123   static inline ValueType getVectorType(ValueType VT, unsigned NumElements) {
124     switch (VT) {
125     default: 
126       break;
127     case MVT::i8:
128       if (NumElements == 8)  return MVT::v8i8;
129       if (NumElements == 16) return MVT::v16i8;
130         break;
131     case MVT::i16:
132       if (NumElements == 4)  return MVT::v4i16;
133       if (NumElements == 8)  return MVT::v8i16;
134         break;
135     case MVT::i32:
136       if (NumElements == 2)  return MVT::v2i32;
137       if (NumElements == 4)  return MVT::v4i32;
138         break;
139     case MVT::f32:
140       if (NumElements == 2)  return MVT::v2f32;
141       if (NumElements == 4)  return MVT::v4f32;
142         break;
143     case MVT::f64:
144       if (NumElements == 2)  return MVT::v2f64;
145       break;
146     }
147     return MVT::Other;
148   }
149   
150   /// MVT::getVectorBaseType - Given a packed vector type, return the type of
151   /// each element.
152   static inline ValueType getVectorBaseType(ValueType VT) {
153     switch (VT) {
154     default: assert(0 && "Invalid vector type!");
155     case v8i8 :
156     case v16i8: return i8;
157     case v4i16:
158     case v8i16: return i16; 
159     case v2i32:
160     case v4i32: return i32;
161     case v2i64: return i64;
162     case v2f32:
163     case v4f32: return f32;
164     case v2f64: return f64;
165     }
166   }
167   
168   /// MVT::getVectorNumElements - Given a packed vector type, return the number
169   /// of elements it contains.
170   static inline unsigned getVectorNumElements(ValueType VT) {
171     switch (VT) {
172       default: assert(0 && "Invalid vector type!");
173       case v16i8: return 16;
174       case v8i8 :
175       case v8i16: return 8;
176       case v4i16:
177       case v4i32: 
178       case v4f32: return 4;
179       case v2i32:
180       case v2i64:
181       case v2f32:
182       case v2f64: return 2;
183     }
184   }
185   
186   /// MVT::getIntVectorWithNumElements - Return any integer vector type that has
187   /// the specified number of elements.
188   static inline ValueType getIntVectorWithNumElements(unsigned NumElts) {
189     switch (NumElts) {
190     default: assert(0 && "Invalid vector type!");
191     case  2: return v2i32;
192     case  4: return v4i16;
193     case  8: return v8i8;
194     case 16: return v16i8;
195     }
196   }
197   
198   
199   /// MVT::getIntVTBitMask - Return an integer with 1's every place there are
200   /// bits in the specified integer value type.
201   static inline uint64_t getIntVTBitMask(ValueType VT) {
202     assert(isInteger(VT) && !isVector(VT) && "Only applies to int scalars!");
203     return ~0ULL >> (64-getSizeInBits(VT));
204   }
205   /// MVT::getIntVTSignBit - Return an integer with a 1 in the position of the
206   /// sign bit for the specified integer value type.
207   static inline uint64_t getIntVTSignBit(ValueType VT) {
208     assert(isInteger(VT) && !isVector(VT) && "Only applies to int scalars!");
209     return 1ULL << (getSizeInBits(VT)-1);
210   }
211
212   /// MVT::getValueTypeString - This function returns value type as a string,
213   /// e.g. "i32".
214   const char *getValueTypeString(ValueType VT);
215
216   /// MVT::getTypeForValueType - This method returns an LLVM type corresponding
217   /// to the specified ValueType.  For integer types, this returns an unsigned
218   /// type.  Note that this will abort for types that cannot be represented.
219   const Type *getTypeForValueType(ValueType VT);
220 }
221
222 } // End llvm namespace
223
224 #endif