1 //===- CodeGen/ValueTypes.h - Low-Level Target independ. types --*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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.
14 //===----------------------------------------------------------------------===//
16 #ifndef LLVM_CODEGEN_VALUETYPES_H
17 #define LLVM_CODEGEN_VALUETYPES_H
19 #include "llvm/CodeGen/MachineValueType.h"
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.
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) {}
41 bool operator==(EVT VT) const {
42 return !(*this != VT);
44 bool operator!=(EVT VT) const {
45 if (V.SimpleTy != VT.V.SimpleTy)
48 return LLVMTy != VT.LLVMTy;
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);
59 /// getIntegerVT - Returns the EVT that represents an integer with the given
61 static EVT getIntegerVT(LLVMContext &Context, unsigned BitWidth) {
62 MVT M = MVT::getIntegerVT(BitWidth);
65 return getExtendedIntegerVT(Context, BitWidth);
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);
74 return getExtendedVectorVT(Context, VT, NumElements);
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 {
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!");
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() {
97 return changeVectorElementTypeToInteger();
100 return MVT::getIntegerVT(getSizeInBits());
102 return changeExtendedTypeToInteger();
105 /// isSimple - Test if the given EVT is simple (as opposed to being
107 bool isSimple() const {
108 return V.SimpleTy >= 0;
111 /// isExtended - Test if the given EVT is extended (as opposed to
113 bool isExtended() const {
117 /// isFloatingPoint - Return true if this is a FP, or a vector FP type.
118 bool isFloatingPoint() const {
119 return isSimple() ? V.isFloatingPoint() : isExtendedFloatingPoint();
122 /// isInteger - Return true if this is an integer, or a vector integer type.
123 bool isInteger() const {
124 return isSimple() ? V.isInteger() : isExtendedInteger();
127 /// isVector - Return true if this is a vector value type.
128 bool isVector() const {
129 return isSimple() ? V.isVector() : isExtendedVector();
132 /// is16BitVector - Return true if this is a 16-bit vector type.
133 bool is16BitVector() const {
134 return isSimple() ? V.is16BitVector() : isExtended16BitVector();
137 /// is32BitVector - Return true if this is a 32-bit vector type.
138 bool is32BitVector() const {
139 return isSimple() ? V.is32BitVector() : isExtended32BitVector();
142 /// is64BitVector - Return true if this is a 64-bit vector type.
143 bool is64BitVector() const {
144 return isSimple() ? V.is64BitVector() : isExtended64BitVector();
147 /// is128BitVector - Return true if this is a 128-bit vector type.
148 bool is128BitVector() const {
149 return isSimple() ? V.is128BitVector() : isExtended128BitVector();
152 /// is256BitVector - Return true if this is a 256-bit vector type.
153 bool is256BitVector() const {
154 return isSimple() ? V.is256BitVector() : isExtended256BitVector();
157 /// is512BitVector - Return true if this is a 512-bit vector type.
158 bool is512BitVector() const {
159 return isSimple() ? V.is512BitVector() : isExtended512BitVector();
162 /// is1024BitVector - Return true if this is a 1024-bit vector type.
163 bool is1024BitVector() const {
164 return isSimple() ? V.is1024BitVector() : isExtended1024BitVector();
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);
172 /// isByteSized - Return true if the bit size is a multiple of 8.
173 bool isByteSized() const {
174 return (getSizeInBits() & 7) == 0;
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));
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();
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();
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();
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();
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();
214 /// getSimpleVT - Return the SimpleValueType held in the specified
216 MVT getSimpleVT() const {
217 assert(isSimple() && "Expected a SimpleValueType!");
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;
227 /// getVectorElementType - Given a vector type, return the type of
229 EVT getVectorElementType() const {
230 assert(isVector() && "Invalid vector type!");
232 return V.getVectorElementType();
233 return getExtendedVectorElementType();
236 /// getVectorNumElements - Given a vector type, return the number of
237 /// elements it contains.
238 unsigned getVectorNumElements() const {
239 assert(isVector() && "Invalid vector type!");
241 return V.getVectorNumElements();
242 return getExtendedVectorNumElements();
245 /// getSizeInBits - Return the size of the specified value type in bits.
246 unsigned getSizeInBits() const {
248 return V.getSizeInBits();
249 return getExtendedSizeInBits();
252 unsigned getScalarSizeInBits() const {
253 return getScalarType().getSizeInBits();
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;
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;
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();
276 return getIntegerVT(Context, 1 << Log2_32_Ceil(BitWidth));
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)
292 return getIntegerVT(Context, (EVTSize + 1) / 2);
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());
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));
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);
322 /// getEVTString - This function returns value type as a string,
324 std::string getEVTString() const;
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;
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);
336 intptr_t getRawBits() const {
340 return (intptr_t)(LLVMTy);
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;
350 return L.V.SimpleTy < R.V.SimpleTy;
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;
378 } // End llvm namespace