[SDAG] Introduce a new BITREVERSE node along with a corresponding LLVM intrinsic
[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 is distributed under the University of Illinois Open Source
6 // 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 "llvm/CodeGen/MachineValueType.h"
20 #include <cassert>
21 #include <string>
22
23 namespace llvm {
24
25   class LLVMContext;
26   class Type;
27
28   /// EVT - Extended Value Type.  Capable of holding value types which are not
29   /// native for any processor (such as the i12345 type), as well as the types
30   /// a MVT can represent.
31   struct EVT {
32   private:
33     MVT V;
34     Type *LLVMTy;
35
36   public:
37     LLVM_CONSTEXPR EVT() : V(MVT::INVALID_SIMPLE_VALUE_TYPE), LLVMTy(nullptr) {}
38     LLVM_CONSTEXPR EVT(MVT::SimpleValueType SVT) : V(SVT), LLVMTy(nullptr) {}
39     LLVM_CONSTEXPR EVT(MVT S) : V(S), LLVMTy(nullptr) {}
40
41     bool operator==(EVT VT) const {
42       return !(*this != VT);
43     }
44     bool operator!=(EVT VT) const {
45       if (V.SimpleTy != VT.V.SimpleTy)
46         return true;
47       if (V.SimpleTy < 0)
48         return LLVMTy != VT.LLVMTy;
49       return false;
50     }
51
52     /// getFloatingPointVT - Returns the EVT that represents a floating point
53     /// type with the given number of bits.  There are two floating point types
54     /// with 128 bits - this returns f128 rather than ppcf128.
55     static EVT getFloatingPointVT(unsigned BitWidth) {
56       return MVT::getFloatingPointVT(BitWidth);
57     }
58
59     /// getIntegerVT - Returns the EVT that represents an integer with the given
60     /// number of bits.
61     static EVT getIntegerVT(LLVMContext &Context, unsigned BitWidth) {
62       MVT M = MVT::getIntegerVT(BitWidth);
63       if (M.SimpleTy >= 0)
64         return M;
65       return getExtendedIntegerVT(Context, BitWidth);
66     }
67
68     /// getVectorVT - Returns the EVT that represents a vector NumElements in
69     /// length, where each element is of type VT.
70     static EVT getVectorVT(LLVMContext &Context, EVT VT, unsigned NumElements) {
71       MVT M = MVT::getVectorVT(VT.V, NumElements);
72       if (M.SimpleTy >= 0)
73         return M;
74       return getExtendedVectorVT(Context, VT, NumElements);
75     }
76
77     /// changeVectorElementTypeToInteger - Return a vector with the same number
78     /// of elements as this vector, but with the element type converted to an
79     /// integer type with the same bitwidth.
80     EVT changeVectorElementTypeToInteger() const {
81       if (!isSimple())
82         return changeExtendedVectorElementTypeToInteger();
83       MVT EltTy = getSimpleVT().getVectorElementType();
84       unsigned BitWidth = EltTy.getSizeInBits();
85       MVT IntTy = MVT::getIntegerVT(BitWidth);
86       MVT VecTy = MVT::getVectorVT(IntTy, getVectorNumElements());
87       assert(VecTy.SimpleTy >= 0 &&
88              "Simple vector VT not representable by simple integer vector VT!");
89       return VecTy;
90     }
91
92     /// Return the type converted to an equivalently sized integer or vector
93     /// with integer element type. Similar to changeVectorElementTypeToInteger,
94     /// but also handles scalars.
95     EVT changeTypeToInteger() {
96       if (isVector())
97         return changeVectorElementTypeToInteger();
98
99       if (isSimple())
100         return MVT::getIntegerVT(getSizeInBits());
101
102       return changeExtendedTypeToInteger();
103     }
104
105     /// isSimple - Test if the given EVT is simple (as opposed to being
106     /// extended).
107     bool isSimple() const {
108       return V.SimpleTy >= 0;
109     }
110
111     /// isExtended - Test if the given EVT is extended (as opposed to
112     /// being simple).
113     bool isExtended() const {
114       return !isSimple();
115     }
116
117     /// isFloatingPoint - Return true if this is a FP, or a vector FP type.
118     bool isFloatingPoint() const {
119       return isSimple() ? V.isFloatingPoint() : isExtendedFloatingPoint();
120     }
121
122     /// isInteger - Return true if this is an integer, or a vector integer type.
123     bool isInteger() const {
124       return isSimple() ? V.isInteger() : isExtendedInteger();
125     }
126
127     /// isVector - Return true if this is a vector value type.
128     bool isVector() const {
129       return isSimple() ? V.isVector() : isExtendedVector();
130     }
131
132     /// is16BitVector - Return true if this is a 16-bit vector type.
133     bool is16BitVector() const {
134       return isSimple() ? V.is16BitVector() : isExtended16BitVector();
135     }
136
137     /// is32BitVector - Return true if this is a 32-bit vector type.
138     bool is32BitVector() const {
139       return isSimple() ? V.is32BitVector() : isExtended32BitVector();
140     }
141
142     /// is64BitVector - Return true if this is a 64-bit vector type.
143     bool is64BitVector() const {
144       return isSimple() ? V.is64BitVector() : isExtended64BitVector();
145     }
146
147     /// is128BitVector - Return true if this is a 128-bit vector type.
148     bool is128BitVector() const {
149       return isSimple() ? V.is128BitVector() : isExtended128BitVector();
150     }
151
152     /// is256BitVector - Return true if this is a 256-bit vector type.
153     bool is256BitVector() const {
154       return isSimple() ? V.is256BitVector() : isExtended256BitVector();
155     }
156
157     /// is512BitVector - Return true if this is a 512-bit vector type.
158     bool is512BitVector() const {
159       return isSimple() ? V.is512BitVector() : isExtended512BitVector();
160     }
161
162     /// is1024BitVector - Return true if this is a 1024-bit vector type.
163     bool is1024BitVector() const {
164       return isSimple() ? V.is1024BitVector() : isExtended1024BitVector();
165     }
166
167     /// isOverloaded - Return true if this is an overloaded type for TableGen.
168     bool isOverloaded() const {
169       return (V==MVT::iAny || V==MVT::fAny || V==MVT::vAny || V==MVT::iPTRAny);
170     }
171
172     /// isByteSized - Return true if the bit size is a multiple of 8.
173     bool isByteSized() const {
174       return (getSizeInBits() & 7) == 0;
175     }
176
177     /// isRound - Return true if the size is a power-of-two number of bytes.
178     bool isRound() const {
179       unsigned BitSize = getSizeInBits();
180       return BitSize >= 8 && !(BitSize & (BitSize - 1));
181     }
182
183     /// bitsEq - Return true if this has the same number of bits as VT.
184     bool bitsEq(EVT VT) const {
185       if (EVT::operator==(VT)) return true;
186       return getSizeInBits() == VT.getSizeInBits();
187     }
188
189     /// bitsGT - Return true if this has more bits than VT.
190     bool bitsGT(EVT VT) const {
191       if (EVT::operator==(VT)) return false;
192       return getSizeInBits() > VT.getSizeInBits();
193     }
194
195     /// bitsGE - Return true if this has no less bits than VT.
196     bool bitsGE(EVT VT) const {
197       if (EVT::operator==(VT)) return true;
198       return getSizeInBits() >= VT.getSizeInBits();
199     }
200
201     /// bitsLT - Return true if this has less bits than VT.
202     bool bitsLT(EVT VT) const {
203       if (EVT::operator==(VT)) return false;
204       return getSizeInBits() < VT.getSizeInBits();
205     }
206
207     /// bitsLE - Return true if this has no more bits than VT.
208     bool bitsLE(EVT VT) const {
209       if (EVT::operator==(VT)) return true;
210       return getSizeInBits() <= VT.getSizeInBits();
211     }
212
213
214     /// getSimpleVT - Return the SimpleValueType held in the specified
215     /// simple EVT.
216     MVT getSimpleVT() const {
217       assert(isSimple() && "Expected a SimpleValueType!");
218       return V;
219     }
220
221     /// getScalarType - If this is a vector type, return the element type,
222     /// otherwise return this.
223     EVT getScalarType() const {
224       return isVector() ? getVectorElementType() : *this;
225     }
226
227     /// getVectorElementType - Given a vector type, return the type of
228     /// each element.
229     EVT getVectorElementType() const {
230       assert(isVector() && "Invalid vector type!");
231       if (isSimple())
232         return V.getVectorElementType();
233       return getExtendedVectorElementType();
234     }
235
236     /// getVectorNumElements - Given a vector type, return the number of
237     /// elements it contains.
238     unsigned getVectorNumElements() const {
239       assert(isVector() && "Invalid vector type!");
240       if (isSimple())
241         return V.getVectorNumElements();
242       return getExtendedVectorNumElements();
243     }
244
245     /// getSizeInBits - Return the size of the specified value type in bits.
246     unsigned getSizeInBits() const {
247       if (isSimple())
248         return V.getSizeInBits();
249       return getExtendedSizeInBits();
250     }
251
252     unsigned getScalarSizeInBits() const {
253       return getScalarType().getSizeInBits();
254     }
255
256     /// getStoreSize - Return the number of bytes overwritten by a store
257     /// of the specified value type.
258     unsigned getStoreSize() const {
259       return (getSizeInBits() + 7) / 8;
260     }
261
262     /// getStoreSizeInBits - Return the number of bits overwritten by a store
263     /// of the specified value type.
264     unsigned getStoreSizeInBits() const {
265       return getStoreSize() * 8;
266     }
267
268     /// getRoundIntegerType - Rounds the bit-width of the given integer EVT up
269     /// to the nearest power of two (and at least to eight), and returns the
270     /// integer EVT with that number of bits.
271     EVT getRoundIntegerType(LLVMContext &Context) const {
272       assert(isInteger() && !isVector() && "Invalid integer type!");
273       unsigned BitWidth = getSizeInBits();
274       if (BitWidth <= 8)
275         return EVT(MVT::i8);
276       return getIntegerVT(Context, 1 << Log2_32_Ceil(BitWidth));
277     }
278
279     /// getHalfSizedIntegerVT - Finds the smallest simple value type that is
280     /// greater than or equal to half the width of this EVT. If no simple
281     /// value type can be found, an extended integer value type of half the
282     /// size (rounded up) is returned.
283     EVT getHalfSizedIntegerVT(LLVMContext &Context) const {
284       assert(isInteger() && !isVector() && "Invalid integer type!");
285       unsigned EVTSize = getSizeInBits();
286       for (unsigned IntVT = MVT::FIRST_INTEGER_VALUETYPE;
287           IntVT <= MVT::LAST_INTEGER_VALUETYPE; ++IntVT) {
288         EVT HalfVT = EVT((MVT::SimpleValueType)IntVT);
289         if (HalfVT.getSizeInBits() * 2 >= EVTSize)
290           return HalfVT;
291       }
292       return getIntegerVT(Context, (EVTSize + 1) / 2);
293     }
294
295     /// \brief Return a VT for an integer vector type with the size of the
296     /// elements doubled. The typed returned may be an extended type.
297     EVT widenIntegerVectorElementType(LLVMContext &Context) const {
298       EVT EltVT = getVectorElementType();
299       EltVT = EVT::getIntegerVT(Context, 2 * EltVT.getSizeInBits());
300       return EVT::getVectorVT(Context, EltVT, getVectorNumElements());
301     }
302
303     /// isPow2VectorType - Returns true if the given vector is a power of 2.
304     bool isPow2VectorType() const {
305       unsigned NElts = getVectorNumElements();
306       return !(NElts & (NElts - 1));
307     }
308
309     /// getPow2VectorType - Widens the length of the given vector EVT up to
310     /// the nearest power of 2 and returns that type.
311     EVT getPow2VectorType(LLVMContext &Context) const {
312       if (!isPow2VectorType()) {
313         unsigned NElts = getVectorNumElements();
314         unsigned Pow2NElts = 1 <<  Log2_32_Ceil(NElts);
315         return EVT::getVectorVT(Context, getVectorElementType(), Pow2NElts);
316       }
317       else {
318         return *this;
319       }
320     }
321
322     /// getEVTString - This function returns value type as a string,
323     /// e.g. "i32".
324     std::string getEVTString() const;
325
326     /// getTypeForEVT - This method returns an LLVM type corresponding to the
327     /// specified EVT.  For integer types, this returns an unsigned type.  Note
328     /// that this will abort for types that cannot be represented.
329     Type *getTypeForEVT(LLVMContext &Context) const;
330
331     /// getEVT - Return the value type corresponding to the specified type.
332     /// This returns all pointers as iPTR.  If HandleUnknown is true, unknown
333     /// types are returned as Other, otherwise they are invalid.
334     static EVT getEVT(Type *Ty, bool HandleUnknown = false);
335
336     intptr_t getRawBits() const {
337       if (isSimple())
338         return V.SimpleTy;
339       else
340         return (intptr_t)(LLVMTy);
341     }
342
343     /// compareRawBits - A meaningless but well-behaved order, useful for
344     /// constructing containers.
345     struct compareRawBits {
346       bool operator()(EVT L, EVT R) const {
347         if (L.V.SimpleTy == R.V.SimpleTy)
348           return L.LLVMTy < R.LLVMTy;
349         else
350           return L.V.SimpleTy < R.V.SimpleTy;
351       }
352     };
353
354   private:
355     // Methods for handling the Extended-type case in functions above.
356     // These are all out-of-line to prevent users of this header file
357     // from having a dependency on Type.h.
358     EVT changeExtendedTypeToInteger() const;
359     EVT changeExtendedVectorElementTypeToInteger() const;
360     static EVT getExtendedIntegerVT(LLVMContext &C, unsigned BitWidth);
361     static EVT getExtendedVectorVT(LLVMContext &C, EVT VT,
362                                    unsigned NumElements);
363     bool isExtendedFloatingPoint() const LLVM_READONLY;
364     bool isExtendedInteger() const LLVM_READONLY;
365     bool isExtendedVector() const LLVM_READONLY;
366     bool isExtended16BitVector() const LLVM_READONLY;
367     bool isExtended32BitVector() const LLVM_READONLY;
368     bool isExtended64BitVector() const LLVM_READONLY;
369     bool isExtended128BitVector() const LLVM_READONLY;
370     bool isExtended256BitVector() const LLVM_READONLY;
371     bool isExtended512BitVector() const LLVM_READONLY;
372     bool isExtended1024BitVector() const LLVM_READONLY;
373     EVT getExtendedVectorElementType() const;
374     unsigned getExtendedVectorNumElements() const LLVM_READONLY;
375     unsigned getExtendedSizeInBits() const;
376   };
377
378 } // End llvm namespace
379
380 #endif