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:
EVTEVT37     LLVM_CONSTEXPR EVT() : V(MVT::INVALID_SIMPLE_VALUE_TYPE), LLVMTy(nullptr) {}
EVTEVT38     LLVM_CONSTEXPR EVT(MVT::SimpleValueType SVT) : V(SVT), LLVMTy(nullptr) {}
EVTEVT39     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.
getFloatingPointVTEVT55     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.
getIntegerVTEVT61     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.
getVectorVTEVT70     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.
changeVectorElementTypeToIntegerEVT80     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     /// isSimple - Test if the given EVT is simple (as opposed to being
93     /// extended).
isSimpleEVT94     bool isSimple() const {
95       return V.SimpleTy >= 0;
96     }
97 
98     /// isExtended - Test if the given EVT is extended (as opposed to
99     /// being simple).
isExtendedEVT100     bool isExtended() const {
101       return !isSimple();
102     }
103 
104     /// isFloatingPoint - Return true if this is a FP, or a vector FP type.
isFloatingPointEVT105     bool isFloatingPoint() const {
106       return isSimple() ? V.isFloatingPoint() : isExtendedFloatingPoint();
107     }
108 
109     /// isInteger - Return true if this is an integer, or a vector integer type.
isIntegerEVT110     bool isInteger() const {
111       return isSimple() ? V.isInteger() : isExtendedInteger();
112     }
113 
114     /// isVector - Return true if this is a vector value type.
isVectorEVT115     bool isVector() const {
116       return isSimple() ? V.isVector() : isExtendedVector();
117     }
118 
119     /// is16BitVector - Return true if this is a 16-bit vector type.
is16BitVectorEVT120     bool is16BitVector() const {
121       return isSimple() ? V.is16BitVector() : isExtended16BitVector();
122     }
123 
124     /// is32BitVector - Return true if this is a 32-bit vector type.
is32BitVectorEVT125     bool is32BitVector() const {
126       return isSimple() ? V.is32BitVector() : isExtended32BitVector();
127     }
128 
129     /// is64BitVector - Return true if this is a 64-bit vector type.
is64BitVectorEVT130     bool is64BitVector() const {
131       return isSimple() ? V.is64BitVector() : isExtended64BitVector();
132     }
133 
134     /// is128BitVector - Return true if this is a 128-bit vector type.
is128BitVectorEVT135     bool is128BitVector() const {
136       return isSimple() ? V.is128BitVector() : isExtended128BitVector();
137     }
138 
139     /// is256BitVector - Return true if this is a 256-bit vector type.
is256BitVectorEVT140     bool is256BitVector() const {
141       return isSimple() ? V.is256BitVector() : isExtended256BitVector();
142     }
143 
144     /// is512BitVector - Return true if this is a 512-bit vector type.
is512BitVectorEVT145     bool is512BitVector() const {
146       return isSimple() ? V.is512BitVector() : isExtended512BitVector();
147     }
148 
149     /// is1024BitVector - Return true if this is a 1024-bit vector type.
is1024BitVectorEVT150     bool is1024BitVector() const {
151       return isSimple() ? V.is1024BitVector() : isExtended1024BitVector();
152     }
153 
154     /// isOverloaded - Return true if this is an overloaded type for TableGen.
isOverloadedEVT155     bool isOverloaded() const {
156       return (V==MVT::iAny || V==MVT::fAny || V==MVT::vAny || V==MVT::iPTRAny);
157     }
158 
159     /// isByteSized - Return true if the bit size is a multiple of 8.
isByteSizedEVT160     bool isByteSized() const {
161       return (getSizeInBits() & 7) == 0;
162     }
163 
164     /// isRound - Return true if the size is a power-of-two number of bytes.
isRoundEVT165     bool isRound() const {
166       unsigned BitSize = getSizeInBits();
167       return BitSize >= 8 && !(BitSize & (BitSize - 1));
168     }
169 
170     /// bitsEq - Return true if this has the same number of bits as VT.
bitsEqEVT171     bool bitsEq(EVT VT) const {
172       if (EVT::operator==(VT)) return true;
173       return getSizeInBits() == VT.getSizeInBits();
174     }
175 
176     /// bitsGT - Return true if this has more bits than VT.
bitsGTEVT177     bool bitsGT(EVT VT) const {
178       if (EVT::operator==(VT)) return false;
179       return getSizeInBits() > VT.getSizeInBits();
180     }
181 
182     /// bitsGE - Return true if this has no less bits than VT.
bitsGEEVT183     bool bitsGE(EVT VT) const {
184       if (EVT::operator==(VT)) return true;
185       return getSizeInBits() >= VT.getSizeInBits();
186     }
187 
188     /// bitsLT - Return true if this has less bits than VT.
bitsLTEVT189     bool bitsLT(EVT VT) const {
190       if (EVT::operator==(VT)) return false;
191       return getSizeInBits() < VT.getSizeInBits();
192     }
193 
194     /// bitsLE - Return true if this has no more bits than VT.
bitsLEEVT195     bool bitsLE(EVT VT) const {
196       if (EVT::operator==(VT)) return true;
197       return getSizeInBits() <= VT.getSizeInBits();
198     }
199 
200 
201     /// getSimpleVT - Return the SimpleValueType held in the specified
202     /// simple EVT.
getSimpleVTEVT203     MVT getSimpleVT() const {
204       assert(isSimple() && "Expected a SimpleValueType!");
205       return V;
206     }
207 
208     /// getScalarType - If this is a vector type, return the element type,
209     /// otherwise return this.
getScalarTypeEVT210     EVT getScalarType() const {
211       return isVector() ? getVectorElementType() : *this;
212     }
213 
214     /// getVectorElementType - Given a vector type, return the type of
215     /// each element.
getVectorElementTypeEVT216     EVT getVectorElementType() const {
217       assert(isVector() && "Invalid vector type!");
218       if (isSimple())
219         return V.getVectorElementType();
220       return getExtendedVectorElementType();
221     }
222 
223     /// getVectorNumElements - Given a vector type, return the number of
224     /// elements it contains.
getVectorNumElementsEVT225     unsigned getVectorNumElements() const {
226       assert(isVector() && "Invalid vector type!");
227       if (isSimple())
228         return V.getVectorNumElements();
229       return getExtendedVectorNumElements();
230     }
231 
232     /// getSizeInBits - Return the size of the specified value type in bits.
getSizeInBitsEVT233     unsigned getSizeInBits() const {
234       if (isSimple())
235         return V.getSizeInBits();
236       return getExtendedSizeInBits();
237     }
238 
getScalarSizeInBitsEVT239     unsigned getScalarSizeInBits() const {
240       return getScalarType().getSizeInBits();
241     }
242 
243     /// getStoreSize - Return the number of bytes overwritten by a store
244     /// of the specified value type.
getStoreSizeEVT245     unsigned getStoreSize() const {
246       return (getSizeInBits() + 7) / 8;
247     }
248 
249     /// getStoreSizeInBits - Return the number of bits overwritten by a store
250     /// of the specified value type.
getStoreSizeInBitsEVT251     unsigned getStoreSizeInBits() const {
252       return getStoreSize() * 8;
253     }
254 
255     /// getRoundIntegerType - Rounds the bit-width of the given integer EVT up
256     /// to the nearest power of two (and at least to eight), and returns the
257     /// integer EVT with that number of bits.
getRoundIntegerTypeEVT258     EVT getRoundIntegerType(LLVMContext &Context) const {
259       assert(isInteger() && !isVector() && "Invalid integer type!");
260       unsigned BitWidth = getSizeInBits();
261       if (BitWidth <= 8)
262         return EVT(MVT::i8);
263       return getIntegerVT(Context, 1 << Log2_32_Ceil(BitWidth));
264     }
265 
266     /// getHalfSizedIntegerVT - Finds the smallest simple value type that is
267     /// greater than or equal to half the width of this EVT. If no simple
268     /// value type can be found, an extended integer value type of half the
269     /// size (rounded up) is returned.
getHalfSizedIntegerVTEVT270     EVT getHalfSizedIntegerVT(LLVMContext &Context) const {
271       assert(isInteger() && !isVector() && "Invalid integer type!");
272       unsigned EVTSize = getSizeInBits();
273       for (unsigned IntVT = MVT::FIRST_INTEGER_VALUETYPE;
274           IntVT <= MVT::LAST_INTEGER_VALUETYPE; ++IntVT) {
275         EVT HalfVT = EVT((MVT::SimpleValueType)IntVT);
276         if (HalfVT.getSizeInBits() * 2 >= EVTSize)
277           return HalfVT;
278       }
279       return getIntegerVT(Context, (EVTSize + 1) / 2);
280     }
281 
282     /// \brief Return a VT for an integer vector type with the size of the
283     /// elements doubled. The typed returned may be an extended type.
widenIntegerVectorElementTypeEVT284     EVT widenIntegerVectorElementType(LLVMContext &Context) const {
285       EVT EltVT = getVectorElementType();
286       EltVT = EVT::getIntegerVT(Context, 2 * EltVT.getSizeInBits());
287       return EVT::getVectorVT(Context, EltVT, getVectorNumElements());
288     }
289 
290     /// isPow2VectorType - Returns true if the given vector is a power of 2.
isPow2VectorTypeEVT291     bool isPow2VectorType() const {
292       unsigned NElts = getVectorNumElements();
293       return !(NElts & (NElts - 1));
294     }
295 
296     /// getPow2VectorType - Widens the length of the given vector EVT up to
297     /// the nearest power of 2 and returns that type.
getPow2VectorTypeEVT298     EVT getPow2VectorType(LLVMContext &Context) const {
299       if (!isPow2VectorType()) {
300         unsigned NElts = getVectorNumElements();
301         unsigned Pow2NElts = 1 <<  Log2_32_Ceil(NElts);
302         return EVT::getVectorVT(Context, getVectorElementType(), Pow2NElts);
303       }
304       else {
305         return *this;
306       }
307     }
308 
309     /// getEVTString - This function returns value type as a string,
310     /// e.g. "i32".
311     std::string getEVTString() const;
312 
313     /// getTypeForEVT - This method returns an LLVM type corresponding to the
314     /// specified EVT.  For integer types, this returns an unsigned type.  Note
315     /// that this will abort for types that cannot be represented.
316     Type *getTypeForEVT(LLVMContext &Context) const;
317 
318     /// getEVT - Return the value type corresponding to the specified type.
319     /// This returns all pointers as iPTR.  If HandleUnknown is true, unknown
320     /// types are returned as Other, otherwise they are invalid.
321     static EVT getEVT(Type *Ty, bool HandleUnknown = false);
322 
getRawBitsEVT323     intptr_t getRawBits() const {
324       if (isSimple())
325         return V.SimpleTy;
326       else
327         return (intptr_t)(LLVMTy);
328     }
329 
330     /// compareRawBits - A meaningless but well-behaved order, useful for
331     /// constructing containers.
332     struct compareRawBits {
operatorEVT::compareRawBits333       bool operator()(EVT L, EVT R) const {
334         if (L.V.SimpleTy == R.V.SimpleTy)
335           return L.LLVMTy < R.LLVMTy;
336         else
337           return L.V.SimpleTy < R.V.SimpleTy;
338       }
339     };
340 
341   private:
342     // Methods for handling the Extended-type case in functions above.
343     // These are all out-of-line to prevent users of this header file
344     // from having a dependency on Type.h.
345     EVT changeExtendedVectorElementTypeToInteger() const;
346     static EVT getExtendedIntegerVT(LLVMContext &C, unsigned BitWidth);
347     static EVT getExtendedVectorVT(LLVMContext &C, EVT VT,
348                                    unsigned NumElements);
349     bool isExtendedFloatingPoint() const LLVM_READONLY;
350     bool isExtendedInteger() const LLVM_READONLY;
351     bool isExtendedVector() const LLVM_READONLY;
352     bool isExtended16BitVector() const LLVM_READONLY;
353     bool isExtended32BitVector() const LLVM_READONLY;
354     bool isExtended64BitVector() const LLVM_READONLY;
355     bool isExtended128BitVector() const LLVM_READONLY;
356     bool isExtended256BitVector() const LLVM_READONLY;
357     bool isExtended512BitVector() const LLVM_READONLY;
358     bool isExtended1024BitVector() const LLVM_READONLY;
359     EVT getExtendedVectorElementType() const;
360     unsigned getExtendedVectorNumElements() const LLVM_READONLY;
361     unsigned getExtendedSizeInBits() const;
362   };
363 
364 } // End llvm namespace
365 
366 #endif
367