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 /// is2048BitVector - Return true if this is a 2048-bit vector type.
168 bool is2048BitVector() const {
169 return isSimple() ? V.is2048BitVector() : isExtended2048BitVector();
172 /// isOverloaded - Return true if this is an overloaded type for TableGen.
173 bool isOverloaded() const {
174 return (V==MVT::iAny || V==MVT::fAny || V==MVT::vAny || V==MVT::iPTRAny);
177 /// isByteSized - Return true if the bit size is a multiple of 8.
178 bool isByteSized() const {
179 return (getSizeInBits() & 7) == 0;
182 /// isRound - Return true if the size is a power-of-two number of bytes.
183 bool isRound() const {
184 unsigned BitSize = getSizeInBits();
185 return BitSize >= 8 && !(BitSize & (BitSize - 1));
188 /// bitsEq - Return true if this has the same number of bits as VT.
189 bool bitsEq(EVT VT) const {
190 if (EVT::operator==(VT)) return true;
191 return getSizeInBits() == VT.getSizeInBits();
194 /// bitsGT - Return true if this has more bits than VT.
195 bool bitsGT(EVT VT) const {
196 if (EVT::operator==(VT)) return false;
197 return getSizeInBits() > VT.getSizeInBits();
200 /// bitsGE - Return true if this has no less bits than VT.
201 bool bitsGE(EVT VT) const {
202 if (EVT::operator==(VT)) return true;
203 return getSizeInBits() >= VT.getSizeInBits();
206 /// bitsLT - Return true if this has less bits than VT.
207 bool bitsLT(EVT VT) const {
208 if (EVT::operator==(VT)) return false;
209 return getSizeInBits() < VT.getSizeInBits();
212 /// bitsLE - Return true if this has no more bits than VT.
213 bool bitsLE(EVT VT) const {
214 if (EVT::operator==(VT)) return true;
215 return getSizeInBits() <= VT.getSizeInBits();
219 /// getSimpleVT - Return the SimpleValueType held in the specified
221 MVT getSimpleVT() const {
222 assert(isSimple() && "Expected a SimpleValueType!");
226 /// getScalarType - If this is a vector type, return the element type,
227 /// otherwise return this.
228 EVT getScalarType() const {
229 return isVector() ? getVectorElementType() : *this;
232 /// getVectorElementType - Given a vector type, return the type of
234 EVT getVectorElementType() const {
235 assert(isVector() && "Invalid vector type!");
237 return V.getVectorElementType();
238 return getExtendedVectorElementType();
241 /// getVectorNumElements - Given a vector type, return the number of
242 /// elements it contains.
243 unsigned getVectorNumElements() const {
244 assert(isVector() && "Invalid vector type!");
246 return V.getVectorNumElements();
247 return getExtendedVectorNumElements();
250 /// getSizeInBits - Return the size of the specified value type in bits.
251 unsigned getSizeInBits() const {
253 return V.getSizeInBits();
254 return getExtendedSizeInBits();
257 unsigned getScalarSizeInBits() const {
258 return getScalarType().getSizeInBits();
261 /// getStoreSize - Return the number of bytes overwritten by a store
262 /// of the specified value type.
263 unsigned getStoreSize() const {
264 return (getSizeInBits() + 7) / 8;
267 /// getStoreSizeInBits - Return the number of bits overwritten by a store
268 /// of the specified value type.
269 unsigned getStoreSizeInBits() const {
270 return getStoreSize() * 8;
273 /// getRoundIntegerType - Rounds the bit-width of the given integer EVT up
274 /// to the nearest power of two (and at least to eight), and returns the
275 /// integer EVT with that number of bits.
276 EVT getRoundIntegerType(LLVMContext &Context) const {
277 assert(isInteger() && !isVector() && "Invalid integer type!");
278 unsigned BitWidth = getSizeInBits();
281 return getIntegerVT(Context, 1 << Log2_32_Ceil(BitWidth));
284 /// getHalfSizedIntegerVT - Finds the smallest simple value type that is
285 /// greater than or equal to half the width of this EVT. If no simple
286 /// value type can be found, an extended integer value type of half the
287 /// size (rounded up) is returned.
288 EVT getHalfSizedIntegerVT(LLVMContext &Context) const {
289 assert(isInteger() && !isVector() && "Invalid integer type!");
290 unsigned EVTSize = getSizeInBits();
291 for (unsigned IntVT = MVT::FIRST_INTEGER_VALUETYPE;
292 IntVT <= MVT::LAST_INTEGER_VALUETYPE; ++IntVT) {
293 EVT HalfVT = EVT((MVT::SimpleValueType)IntVT);
294 if (HalfVT.getSizeInBits() * 2 >= EVTSize)
297 return getIntegerVT(Context, (EVTSize + 1) / 2);
300 /// \brief Return a VT for an integer vector type with the size of the
301 /// elements doubled. The typed returned may be an extended type.
302 EVT widenIntegerVectorElementType(LLVMContext &Context) const {
303 EVT EltVT = getVectorElementType();
304 EltVT = EVT::getIntegerVT(Context, 2 * EltVT.getSizeInBits());
305 return EVT::getVectorVT(Context, EltVT, getVectorNumElements());
308 /// isPow2VectorType - Returns true if the given vector is a power of 2.
309 bool isPow2VectorType() const {
310 unsigned NElts = getVectorNumElements();
311 return !(NElts & (NElts - 1));
314 /// getPow2VectorType - Widens the length of the given vector EVT up to
315 /// the nearest power of 2 and returns that type.
316 EVT getPow2VectorType(LLVMContext &Context) const {
317 if (!isPow2VectorType()) {
318 unsigned NElts = getVectorNumElements();
319 unsigned Pow2NElts = 1 << Log2_32_Ceil(NElts);
320 return EVT::getVectorVT(Context, getVectorElementType(), Pow2NElts);
327 /// getEVTString - This function returns value type as a string,
329 std::string getEVTString() const;
331 /// getTypeForEVT - This method returns an LLVM type corresponding to the
332 /// specified EVT. For integer types, this returns an unsigned type. Note
333 /// that this will abort for types that cannot be represented.
334 Type *getTypeForEVT(LLVMContext &Context) const;
336 /// getEVT - Return the value type corresponding to the specified type.
337 /// This returns all pointers as iPTR. If HandleUnknown is true, unknown
338 /// types are returned as Other, otherwise they are invalid.
339 static EVT getEVT(Type *Ty, bool HandleUnknown = false);
341 intptr_t getRawBits() const {
345 return (intptr_t)(LLVMTy);
348 /// compareRawBits - A meaningless but well-behaved order, useful for
349 /// constructing containers.
350 struct compareRawBits {
351 bool operator()(EVT L, EVT R) const {
352 if (L.V.SimpleTy == R.V.SimpleTy)
353 return L.LLVMTy < R.LLVMTy;
355 return L.V.SimpleTy < R.V.SimpleTy;
360 // Methods for handling the Extended-type case in functions above.
361 // These are all out-of-line to prevent users of this header file
362 // from having a dependency on Type.h.
363 EVT changeExtendedTypeToInteger() const;
364 EVT changeExtendedVectorElementTypeToInteger() const;
365 static EVT getExtendedIntegerVT(LLVMContext &C, unsigned BitWidth);
366 static EVT getExtendedVectorVT(LLVMContext &C, EVT VT,
367 unsigned NumElements);
368 bool isExtendedFloatingPoint() const LLVM_READONLY;
369 bool isExtendedInteger() const LLVM_READONLY;
370 bool isExtendedVector() const LLVM_READONLY;
371 bool isExtended16BitVector() const LLVM_READONLY;
372 bool isExtended32BitVector() const LLVM_READONLY;
373 bool isExtended64BitVector() const LLVM_READONLY;
374 bool isExtended128BitVector() const LLVM_READONLY;
375 bool isExtended256BitVector() const LLVM_READONLY;
376 bool isExtended512BitVector() const LLVM_READONLY;
377 bool isExtended1024BitVector() const LLVM_READONLY;
378 bool isExtended2048BitVector() const LLVM_READONLY;
379 EVT getExtendedVectorElementType() const;
380 unsigned getExtendedVectorNumElements() const LLVM_READONLY;
381 unsigned getExtendedSizeInBits() const;
384 } // End llvm namespace