• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     /// Return the type converted to an equivalently sized integer or vector
93     /// with integer element type. Similar to changeVectorElementTypeToInteger,
94     /// but also handles scalars.
changeTypeToIntegerEVT95     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).
isSimpleEVT107     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).
isExtendedEVT113     bool isExtended() const {
114       return !isSimple();
115     }
116 
117     /// isFloatingPoint - Return true if this is a FP, or a vector FP type.
isFloatingPointEVT118     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.
isIntegerEVT123     bool isInteger() const {
124       return isSimple() ? V.isInteger() : isExtendedInteger();
125     }
126 
127     /// isScalarInteger - Return true if this is an integer, but not a vector.
isScalarIntegerEVT128     bool isScalarInteger() const {
129       return isSimple() ? V.isScalarInteger() : isExtendedScalarInteger();
130     }
131 
132     /// isVector - Return true if this is a vector value type.
isVectorEVT133     bool isVector() const {
134       return isSimple() ? V.isVector() : isExtendedVector();
135     }
136 
137     /// is16BitVector - Return true if this is a 16-bit vector type.
is16BitVectorEVT138     bool is16BitVector() const {
139       return isSimple() ? V.is16BitVector() : isExtended16BitVector();
140     }
141 
142     /// is32BitVector - Return true if this is a 32-bit vector type.
is32BitVectorEVT143     bool is32BitVector() const {
144       return isSimple() ? V.is32BitVector() : isExtended32BitVector();
145     }
146 
147     /// is64BitVector - Return true if this is a 64-bit vector type.
is64BitVectorEVT148     bool is64BitVector() const {
149       return isSimple() ? V.is64BitVector() : isExtended64BitVector();
150     }
151 
152     /// is128BitVector - Return true if this is a 128-bit vector type.
is128BitVectorEVT153     bool is128BitVector() const {
154       return isSimple() ? V.is128BitVector() : isExtended128BitVector();
155     }
156 
157     /// is256BitVector - Return true if this is a 256-bit vector type.
is256BitVectorEVT158     bool is256BitVector() const {
159       return isSimple() ? V.is256BitVector() : isExtended256BitVector();
160     }
161 
162     /// is512BitVector - Return true if this is a 512-bit vector type.
is512BitVectorEVT163     bool is512BitVector() const {
164       return isSimple() ? V.is512BitVector() : isExtended512BitVector();
165     }
166 
167     /// is1024BitVector - Return true if this is a 1024-bit vector type.
is1024BitVectorEVT168     bool is1024BitVector() const {
169       return isSimple() ? V.is1024BitVector() : isExtended1024BitVector();
170     }
171 
172     /// is2048BitVector - Return true if this is a 2048-bit vector type.
is2048BitVectorEVT173     bool is2048BitVector() const {
174       return isSimple() ? V.is2048BitVector() : isExtended2048BitVector();
175     }
176 
177     /// isOverloaded - Return true if this is an overloaded type for TableGen.
isOverloadedEVT178     bool isOverloaded() const {
179       return (V==MVT::iAny || V==MVT::fAny || V==MVT::vAny || V==MVT::iPTRAny);
180     }
181 
182     /// isByteSized - Return true if the bit size is a multiple of 8.
isByteSizedEVT183     bool isByteSized() const {
184       return (getSizeInBits() & 7) == 0;
185     }
186 
187     /// isRound - Return true if the size is a power-of-two number of bytes.
isRoundEVT188     bool isRound() const {
189       unsigned BitSize = getSizeInBits();
190       return BitSize >= 8 && !(BitSize & (BitSize - 1));
191     }
192 
193     /// bitsEq - Return true if this has the same number of bits as VT.
bitsEqEVT194     bool bitsEq(EVT VT) const {
195       if (EVT::operator==(VT)) return true;
196       return getSizeInBits() == VT.getSizeInBits();
197     }
198 
199     /// bitsGT - Return true if this has more bits than VT.
bitsGTEVT200     bool bitsGT(EVT VT) const {
201       if (EVT::operator==(VT)) return false;
202       return getSizeInBits() > VT.getSizeInBits();
203     }
204 
205     /// bitsGE - Return true if this has no less bits than VT.
bitsGEEVT206     bool bitsGE(EVT VT) const {
207       if (EVT::operator==(VT)) return true;
208       return getSizeInBits() >= VT.getSizeInBits();
209     }
210 
211     /// bitsLT - Return true if this has less bits than VT.
bitsLTEVT212     bool bitsLT(EVT VT) const {
213       if (EVT::operator==(VT)) return false;
214       return getSizeInBits() < VT.getSizeInBits();
215     }
216 
217     /// bitsLE - Return true if this has no more bits than VT.
bitsLEEVT218     bool bitsLE(EVT VT) const {
219       if (EVT::operator==(VT)) return true;
220       return getSizeInBits() <= VT.getSizeInBits();
221     }
222 
223 
224     /// getSimpleVT - Return the SimpleValueType held in the specified
225     /// simple EVT.
getSimpleVTEVT226     MVT getSimpleVT() const {
227       assert(isSimple() && "Expected a SimpleValueType!");
228       return V;
229     }
230 
231     /// getScalarType - If this is a vector type, return the element type,
232     /// otherwise return this.
getScalarTypeEVT233     EVT getScalarType() const {
234       return isVector() ? getVectorElementType() : *this;
235     }
236 
237     /// getVectorElementType - Given a vector type, return the type of
238     /// each element.
getVectorElementTypeEVT239     EVT getVectorElementType() const {
240       assert(isVector() && "Invalid vector type!");
241       if (isSimple())
242         return V.getVectorElementType();
243       return getExtendedVectorElementType();
244     }
245 
246     /// getVectorNumElements - Given a vector type, return the number of
247     /// elements it contains.
getVectorNumElementsEVT248     unsigned getVectorNumElements() const {
249       assert(isVector() && "Invalid vector type!");
250       if (isSimple())
251         return V.getVectorNumElements();
252       return getExtendedVectorNumElements();
253     }
254 
255     /// getSizeInBits - Return the size of the specified value type in bits.
getSizeInBitsEVT256     unsigned getSizeInBits() const {
257       if (isSimple())
258         return V.getSizeInBits();
259       return getExtendedSizeInBits();
260     }
261 
getScalarSizeInBitsEVT262     unsigned getScalarSizeInBits() const {
263       return getScalarType().getSizeInBits();
264     }
265 
266     /// getStoreSize - Return the number of bytes overwritten by a store
267     /// of the specified value type.
getStoreSizeEVT268     unsigned getStoreSize() const {
269       return (getSizeInBits() + 7) / 8;
270     }
271 
272     /// getStoreSizeInBits - Return the number of bits overwritten by a store
273     /// of the specified value type.
getStoreSizeInBitsEVT274     unsigned getStoreSizeInBits() const {
275       return getStoreSize() * 8;
276     }
277 
278     /// getRoundIntegerType - Rounds the bit-width of the given integer EVT up
279     /// to the nearest power of two (and at least to eight), and returns the
280     /// integer EVT with that number of bits.
getRoundIntegerTypeEVT281     EVT getRoundIntegerType(LLVMContext &Context) const {
282       assert(isInteger() && !isVector() && "Invalid integer type!");
283       unsigned BitWidth = getSizeInBits();
284       if (BitWidth <= 8)
285         return EVT(MVT::i8);
286       return getIntegerVT(Context, 1 << Log2_32_Ceil(BitWidth));
287     }
288 
289     /// getHalfSizedIntegerVT - Finds the smallest simple value type that is
290     /// greater than or equal to half the width of this EVT. If no simple
291     /// value type can be found, an extended integer value type of half the
292     /// size (rounded up) is returned.
getHalfSizedIntegerVTEVT293     EVT getHalfSizedIntegerVT(LLVMContext &Context) const {
294       assert(isInteger() && !isVector() && "Invalid integer type!");
295       unsigned EVTSize = getSizeInBits();
296       for (unsigned IntVT = MVT::FIRST_INTEGER_VALUETYPE;
297           IntVT <= MVT::LAST_INTEGER_VALUETYPE; ++IntVT) {
298         EVT HalfVT = EVT((MVT::SimpleValueType)IntVT);
299         if (HalfVT.getSizeInBits() * 2 >= EVTSize)
300           return HalfVT;
301       }
302       return getIntegerVT(Context, (EVTSize + 1) / 2);
303     }
304 
305     /// \brief Return a VT for an integer vector type with the size of the
306     /// elements doubled. The typed returned may be an extended type.
widenIntegerVectorElementTypeEVT307     EVT widenIntegerVectorElementType(LLVMContext &Context) const {
308       EVT EltVT = getVectorElementType();
309       EltVT = EVT::getIntegerVT(Context, 2 * EltVT.getSizeInBits());
310       return EVT::getVectorVT(Context, EltVT, getVectorNumElements());
311     }
312 
313     /// isPow2VectorType - Returns true if the given vector is a power of 2.
isPow2VectorTypeEVT314     bool isPow2VectorType() const {
315       unsigned NElts = getVectorNumElements();
316       return !(NElts & (NElts - 1));
317     }
318 
319     /// getPow2VectorType - Widens the length of the given vector EVT up to
320     /// the nearest power of 2 and returns that type.
getPow2VectorTypeEVT321     EVT getPow2VectorType(LLVMContext &Context) const {
322       if (!isPow2VectorType()) {
323         unsigned NElts = getVectorNumElements();
324         unsigned Pow2NElts = 1 <<  Log2_32_Ceil(NElts);
325         return EVT::getVectorVT(Context, getVectorElementType(), Pow2NElts);
326       }
327       else {
328         return *this;
329       }
330     }
331 
332     /// getEVTString - This function returns value type as a string,
333     /// e.g. "i32".
334     std::string getEVTString() const;
335 
336     /// getTypeForEVT - This method returns an LLVM type corresponding to the
337     /// specified EVT.  For integer types, this returns an unsigned type.  Note
338     /// that this will abort for types that cannot be represented.
339     Type *getTypeForEVT(LLVMContext &Context) const;
340 
341     /// getEVT - Return the value type corresponding to the specified type.
342     /// This returns all pointers as iPTR.  If HandleUnknown is true, unknown
343     /// types are returned as Other, otherwise they are invalid.
344     static EVT getEVT(Type *Ty, bool HandleUnknown = false);
345 
getRawBitsEVT346     intptr_t getRawBits() const {
347       if (isSimple())
348         return V.SimpleTy;
349       else
350         return (intptr_t)(LLVMTy);
351     }
352 
353     /// compareRawBits - A meaningless but well-behaved order, useful for
354     /// constructing containers.
355     struct compareRawBits {
operatorEVT::compareRawBits356       bool operator()(EVT L, EVT R) const {
357         if (L.V.SimpleTy == R.V.SimpleTy)
358           return L.LLVMTy < R.LLVMTy;
359         else
360           return L.V.SimpleTy < R.V.SimpleTy;
361       }
362     };
363 
364   private:
365     // Methods for handling the Extended-type case in functions above.
366     // These are all out-of-line to prevent users of this header file
367     // from having a dependency on Type.h.
368     EVT changeExtendedTypeToInteger() const;
369     EVT changeExtendedVectorElementTypeToInteger() const;
370     static EVT getExtendedIntegerVT(LLVMContext &C, unsigned BitWidth);
371     static EVT getExtendedVectorVT(LLVMContext &C, EVT VT,
372                                    unsigned NumElements);
373     bool isExtendedFloatingPoint() const LLVM_READONLY;
374     bool isExtendedInteger() const LLVM_READONLY;
375     bool isExtendedScalarInteger() const LLVM_READONLY;
376     bool isExtendedVector() const LLVM_READONLY;
377     bool isExtended16BitVector() const LLVM_READONLY;
378     bool isExtended32BitVector() const LLVM_READONLY;
379     bool isExtended64BitVector() const LLVM_READONLY;
380     bool isExtended128BitVector() const LLVM_READONLY;
381     bool isExtended256BitVector() const LLVM_READONLY;
382     bool isExtended512BitVector() const LLVM_READONLY;
383     bool isExtended1024BitVector() const LLVM_READONLY;
384     bool isExtended2048BitVector() const LLVM_READONLY;
385     EVT getExtendedVectorElementType() const;
386     unsigned getExtendedVectorNumElements() const LLVM_READONLY;
387     unsigned getExtendedSizeInBits() const LLVM_READONLY;
388   };
389 
390 } // End llvm namespace
391 
392 #endif
393