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 /// Extended Value Type. Capable of holding value types which are not native
29 /// for any processor (such as the i12345 type), as well as the types an MVT
37 constexpr EVT() : V(MVT::INVALID_SIMPLE_VALUE_TYPE), LLVMTy(nullptr) {}
38 constexpr EVT(MVT::SimpleValueType SVT) : V(SVT), LLVMTy(nullptr) {}
39 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)
47 if (V.SimpleTy == MVT::INVALID_SIMPLE_VALUE_TYPE)
48 return LLVMTy != VT.LLVMTy;
52 /// Returns the EVT that represents a floating-point type with the given
53 /// number of bits. There are two floating-point types with 128 bits - this
54 /// returns f128 rather than ppcf128.
55 static EVT getFloatingPointVT(unsigned BitWidth) {
56 return MVT::getFloatingPointVT(BitWidth);
59 /// Returns the EVT that represents an integer with the given number of
61 static EVT getIntegerVT(LLVMContext &Context, unsigned BitWidth) {
62 MVT M = MVT::getIntegerVT(BitWidth);
63 if (M.SimpleTy != MVT::INVALID_SIMPLE_VALUE_TYPE)
65 return getExtendedIntegerVT(Context, BitWidth);
68 /// Returns the EVT that represents a vector NumElements in length, where
69 /// each element is of type VT.
70 static EVT getVectorVT(LLVMContext &Context, EVT VT, unsigned NumElements,
71 bool IsScalable = false) {
72 MVT M = MVT::getVectorVT(VT.V, NumElements, IsScalable);
73 if (M.SimpleTy != MVT::INVALID_SIMPLE_VALUE_TYPE)
76 assert(!IsScalable && "We don't support extended scalable types yet");
77 return getExtendedVectorVT(Context, VT, NumElements);
80 /// Returns the EVT that represents a vector EC.Min elements in length,
81 /// where each element is of type VT.
82 static EVT getVectorVT(LLVMContext &Context, EVT VT, MVT::ElementCount EC) {
83 MVT M = MVT::getVectorVT(VT.V, EC);
84 if (M.SimpleTy != MVT::INVALID_SIMPLE_VALUE_TYPE)
86 assert (!EC.Scalable && "We don't support extended scalable types yet");
87 return getExtendedVectorVT(Context, VT, EC.Min);
90 /// Return a vector with the same number of elements as this vector, but
91 /// with the element type converted to an integer type with the same
93 EVT changeVectorElementTypeToInteger() const {
95 assert (!isScalableVector() &&
96 "We don't support extended scalable types yet");
97 return changeExtendedVectorElementTypeToInteger();
99 MVT EltTy = getSimpleVT().getVectorElementType();
100 unsigned BitWidth = EltTy.getSizeInBits();
101 MVT IntTy = MVT::getIntegerVT(BitWidth);
102 MVT VecTy = MVT::getVectorVT(IntTy, getVectorNumElements(),
104 assert(VecTy.SimpleTy != MVT::INVALID_SIMPLE_VALUE_TYPE &&
105 "Simple vector VT not representable by simple integer vector VT!");
109 /// Return the type converted to an equivalently sized integer or vector
110 /// with integer element type. Similar to changeVectorElementTypeToInteger,
111 /// but also handles scalars.
112 EVT changeTypeToInteger() {
114 return changeVectorElementTypeToInteger();
117 return MVT::getIntegerVT(getSizeInBits());
119 return changeExtendedTypeToInteger();
122 /// Test if the given EVT is simple (as opposed to being extended).
123 bool isSimple() const {
124 return V.SimpleTy != MVT::INVALID_SIMPLE_VALUE_TYPE;
127 /// Test if the given EVT is extended (as opposed to being simple).
128 bool isExtended() const {
132 /// Return true if this is a FP or a vector FP type.
133 bool isFloatingPoint() const {
134 return isSimple() ? V.isFloatingPoint() : isExtendedFloatingPoint();
137 /// Return true if this is an integer or a vector integer type.
138 bool isInteger() const {
139 return isSimple() ? V.isInteger() : isExtendedInteger();
142 /// Return true if this is an integer, but not a vector.
143 bool isScalarInteger() const {
144 return isSimple() ? V.isScalarInteger() : isExtendedScalarInteger();
147 /// Return true if this is a vector value type.
148 bool isVector() const {
149 return isSimple() ? V.isVector() : isExtendedVector();
152 /// Return true if this is a vector type where the runtime
153 /// length is machine dependent
154 bool isScalableVector() const {
155 // FIXME: We don't support extended scalable types yet, because the
156 // matching IR type doesn't exist. Once it has been added, this can
157 // be changed to call isExtendedScalableVector.
160 return V.isScalableVector();
163 /// Return true if this is a 16-bit vector type.
164 bool is16BitVector() const {
165 return isSimple() ? V.is16BitVector() : isExtended16BitVector();
168 /// Return true if this is a 32-bit vector type.
169 bool is32BitVector() const {
170 return isSimple() ? V.is32BitVector() : isExtended32BitVector();
173 /// Return true if this is a 64-bit vector type.
174 bool is64BitVector() const {
175 return isSimple() ? V.is64BitVector() : isExtended64BitVector();
178 /// Return true if this is a 128-bit vector type.
179 bool is128BitVector() const {
180 return isSimple() ? V.is128BitVector() : isExtended128BitVector();
183 /// Return true if this is a 256-bit vector type.
184 bool is256BitVector() const {
185 return isSimple() ? V.is256BitVector() : isExtended256BitVector();
188 /// Return true if this is a 512-bit vector type.
189 bool is512BitVector() const {
190 return isSimple() ? V.is512BitVector() : isExtended512BitVector();
193 /// Return true if this is a 1024-bit vector type.
194 bool is1024BitVector() const {
195 return isSimple() ? V.is1024BitVector() : isExtended1024BitVector();
198 /// Return true if this is a 2048-bit vector type.
199 bool is2048BitVector() const {
200 return isSimple() ? V.is2048BitVector() : isExtended2048BitVector();
203 /// Return true if this is an overloaded type for TableGen.
204 bool isOverloaded() const {
205 return (V==MVT::iAny || V==MVT::fAny || V==MVT::vAny || V==MVT::iPTRAny);
208 /// Return true if the bit size is a multiple of 8.
209 bool isByteSized() const {
210 return (getSizeInBits() & 7) == 0;
213 /// Return true if the size is a power-of-two number of bytes.
214 bool isRound() const {
215 unsigned BitSize = getSizeInBits();
216 return BitSize >= 8 && !(BitSize & (BitSize - 1));
219 /// Return true if this has the same number of bits as VT.
220 bool bitsEq(EVT VT) const {
221 if (EVT::operator==(VT)) return true;
222 return getSizeInBits() == VT.getSizeInBits();
225 /// Return true if this has more bits than VT.
226 bool bitsGT(EVT VT) const {
227 if (EVT::operator==(VT)) return false;
228 return getSizeInBits() > VT.getSizeInBits();
231 /// Return true if this has no less bits than VT.
232 bool bitsGE(EVT VT) const {
233 if (EVT::operator==(VT)) return true;
234 return getSizeInBits() >= VT.getSizeInBits();
237 /// Return true if this has less bits than VT.
238 bool bitsLT(EVT VT) const {
239 if (EVT::operator==(VT)) return false;
240 return getSizeInBits() < VT.getSizeInBits();
243 /// Return true if this has no more bits than VT.
244 bool bitsLE(EVT VT) const {
245 if (EVT::operator==(VT)) return true;
246 return getSizeInBits() <= VT.getSizeInBits();
250 /// Return the SimpleValueType held in the specified simple EVT.
251 MVT getSimpleVT() const {
252 assert(isSimple() && "Expected a SimpleValueType!");
256 /// If this is a vector type, return the element type, otherwise return
258 EVT getScalarType() const {
259 return isVector() ? getVectorElementType() : *this;
262 /// Given a vector type, return the type of each element.
263 EVT getVectorElementType() const {
264 assert(isVector() && "Invalid vector type!");
266 return V.getVectorElementType();
267 return getExtendedVectorElementType();
270 /// Given a vector type, return the number of elements it contains.
271 unsigned getVectorNumElements() const {
272 assert(isVector() && "Invalid vector type!");
274 return V.getVectorNumElements();
275 return getExtendedVectorNumElements();
278 // Given a (possibly scalable) vector type, return the ElementCount
279 MVT::ElementCount getVectorElementCount() const {
280 assert((isVector()) && "Invalid vector type!");
282 return V.getVectorElementCount();
284 assert(!isScalableVector() &&
285 "We don't support extended scalable types yet");
286 return {getExtendedVectorNumElements(), false};
289 /// Return the size of the specified value type in bits.
290 unsigned getSizeInBits() const {
292 return V.getSizeInBits();
293 return getExtendedSizeInBits();
296 unsigned getScalarSizeInBits() const {
297 return getScalarType().getSizeInBits();
300 /// Return the number of bytes overwritten by a store of the specified value
302 unsigned getStoreSize() const {
303 return (getSizeInBits() + 7) / 8;
306 /// Return the number of bits overwritten by a store of the specified value
308 unsigned getStoreSizeInBits() const {
309 return getStoreSize() * 8;
312 /// Rounds the bit-width of the given integer EVT up to the nearest power of
313 /// two (and at least to eight), and returns the integer EVT with that
315 EVT getRoundIntegerType(LLVMContext &Context) const {
316 assert(isInteger() && !isVector() && "Invalid integer type!");
317 unsigned BitWidth = getSizeInBits();
320 return getIntegerVT(Context, 1 << Log2_32_Ceil(BitWidth));
323 /// Finds the smallest simple value type that is greater than or equal to
324 /// half the width of this EVT. If no simple value type can be found, an
325 /// extended integer value type of half the size (rounded up) is returned.
326 EVT getHalfSizedIntegerVT(LLVMContext &Context) const {
327 assert(isInteger() && !isVector() && "Invalid integer type!");
328 unsigned EVTSize = getSizeInBits();
329 for (unsigned IntVT = MVT::FIRST_INTEGER_VALUETYPE;
330 IntVT <= MVT::LAST_INTEGER_VALUETYPE; ++IntVT) {
331 EVT HalfVT = EVT((MVT::SimpleValueType)IntVT);
332 if (HalfVT.getSizeInBits() * 2 >= EVTSize)
335 return getIntegerVT(Context, (EVTSize + 1) / 2);
338 /// Return a VT for an integer vector type with the size of the
339 /// elements doubled. The typed returned may be an extended type.
340 EVT widenIntegerVectorElementType(LLVMContext &Context) const {
341 EVT EltVT = getVectorElementType();
342 EltVT = EVT::getIntegerVT(Context, 2 * EltVT.getSizeInBits());
343 return EVT::getVectorVT(Context, EltVT, getVectorElementCount());
346 // Return a VT for a vector type with the same element type but
347 // half the number of elements. The type returned may be an
349 EVT getHalfNumVectorElementsVT(LLVMContext &Context) const {
350 EVT EltVT = getVectorElementType();
351 auto EltCnt = getVectorElementCount();
352 assert(!(EltCnt.Min & 1) && "Splitting vector, but not in half!");
353 return EVT::getVectorVT(Context, EltVT, EltCnt / 2);
356 /// Returns true if the given vector is a power of 2.
357 bool isPow2VectorType() const {
358 unsigned NElts = getVectorNumElements();
359 return !(NElts & (NElts - 1));
362 /// Widens the length of the given vector EVT up to the nearest power of 2
363 /// and returns that type.
364 EVT getPow2VectorType(LLVMContext &Context) const {
365 if (!isPow2VectorType()) {
366 unsigned NElts = getVectorNumElements();
367 unsigned Pow2NElts = 1 << Log2_32_Ceil(NElts);
368 return EVT::getVectorVT(Context, getVectorElementType(), Pow2NElts,
376 /// This function returns value type as a string, e.g. "i32".
377 std::string getEVTString() const;
379 /// This method returns an LLVM type corresponding to the specified EVT.
380 /// For integer types, this returns an unsigned type. Note that this will
381 /// abort for types that cannot be represented.
382 Type *getTypeForEVT(LLVMContext &Context) const;
384 /// Return the value type corresponding to the specified type.
385 /// This returns all pointers as iPTR. If HandleUnknown is true, unknown
386 /// types are returned as Other, otherwise they are invalid.
387 static EVT getEVT(Type *Ty, bool HandleUnknown = false);
389 intptr_t getRawBits() const {
393 return (intptr_t)(LLVMTy);
396 /// A meaningless but well-behaved order, useful for constructing
398 struct compareRawBits {
399 bool operator()(EVT L, EVT R) const {
400 if (L.V.SimpleTy == R.V.SimpleTy)
401 return L.LLVMTy < R.LLVMTy;
403 return L.V.SimpleTy < R.V.SimpleTy;
408 // Methods for handling the Extended-type case in functions above.
409 // These are all out-of-line to prevent users of this header file
410 // from having a dependency on Type.h.
411 EVT changeExtendedTypeToInteger() const;
412 EVT changeExtendedVectorElementTypeToInteger() const;
413 static EVT getExtendedIntegerVT(LLVMContext &C, unsigned BitWidth);
414 static EVT getExtendedVectorVT(LLVMContext &C, EVT VT,
415 unsigned NumElements);
416 bool isExtendedFloatingPoint() const LLVM_READONLY;
417 bool isExtendedInteger() const LLVM_READONLY;
418 bool isExtendedScalarInteger() const LLVM_READONLY;
419 bool isExtendedVector() const LLVM_READONLY;
420 bool isExtended16BitVector() const LLVM_READONLY;
421 bool isExtended32BitVector() const LLVM_READONLY;
422 bool isExtended64BitVector() const LLVM_READONLY;
423 bool isExtended128BitVector() const LLVM_READONLY;
424 bool isExtended256BitVector() const LLVM_READONLY;
425 bool isExtended512BitVector() const LLVM_READONLY;
426 bool isExtended1024BitVector() const LLVM_READONLY;
427 bool isExtended2048BitVector() const LLVM_READONLY;
428 EVT getExtendedVectorElementType() const;
429 unsigned getExtendedVectorNumElements() const LLVM_READONLY;
430 unsigned getExtendedSizeInBits() const LLVM_READONLY;
433 } // End llvm namespace