1 //===- CodeGen/ValueTypes.h - Low-Level Target independ. types --*- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This file defines the set of low-level target independent types which various
10 // values in the code generator are. This allows the target specific behavior
11 // of instructions to be described to target independent passes.
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_CODEGEN_VALUETYPES_H
16 #define LLVM_CODEGEN_VALUETYPES_H
18 #include "llvm/Support/Compiler.h"
19 #include "llvm/Support/MachineValueType.h"
20 #include "llvm/Support/MathExtras.h"
30 /// Extended Value Type. Capable of holding value types which are not native
31 /// for any processor (such as the i12345 type), as well as the types an MVT
35 MVT V
= MVT::INVALID_SIMPLE_VALUE_TYPE
;
36 Type
*LLVMTy
= nullptr;
39 constexpr EVT() = default;
40 constexpr EVT(MVT::SimpleValueType SVT
) : V(SVT
) {}
41 constexpr EVT(MVT S
) : V(S
) {}
43 bool operator==(EVT VT
) const {
44 return !(*this != VT
);
46 bool operator!=(EVT VT
) const {
47 if (V
.SimpleTy
!= VT
.V
.SimpleTy
)
49 if (V
.SimpleTy
== MVT::INVALID_SIMPLE_VALUE_TYPE
)
50 return LLVMTy
!= VT
.LLVMTy
;
54 /// Returns the EVT that represents a floating-point type with the given
55 /// number of bits. There are two floating-point types with 128 bits - this
56 /// returns f128 rather than ppcf128.
57 static EVT
getFloatingPointVT(unsigned BitWidth
) {
58 return MVT::getFloatingPointVT(BitWidth
);
61 /// Returns the EVT that represents an integer with the given number of
63 static EVT
getIntegerVT(LLVMContext
&Context
, unsigned BitWidth
) {
64 MVT M
= MVT::getIntegerVT(BitWidth
);
65 if (M
.SimpleTy
!= MVT::INVALID_SIMPLE_VALUE_TYPE
)
67 return getExtendedIntegerVT(Context
, BitWidth
);
70 /// Returns the EVT that represents a vector NumElements in length, where
71 /// each element is of type VT.
72 static EVT
getVectorVT(LLVMContext
&Context
, EVT VT
, unsigned NumElements
,
73 bool IsScalable
= false) {
74 MVT M
= MVT::getVectorVT(VT
.V
, NumElements
, IsScalable
);
75 if (M
.SimpleTy
!= MVT::INVALID_SIMPLE_VALUE_TYPE
)
78 assert(!IsScalable
&& "We don't support extended scalable types yet");
79 return getExtendedVectorVT(Context
, VT
, NumElements
);
82 /// Returns the EVT that represents a vector EC.Min elements in length,
83 /// where each element is of type VT.
84 static EVT
getVectorVT(LLVMContext
&Context
, EVT VT
, ElementCount EC
) {
85 MVT M
= MVT::getVectorVT(VT
.V
, EC
);
86 if (M
.SimpleTy
!= MVT::INVALID_SIMPLE_VALUE_TYPE
)
88 assert (!EC
.Scalable
&& "We don't support extended scalable types yet");
89 return getExtendedVectorVT(Context
, VT
, EC
.Min
);
92 /// Return a vector with the same number of elements as this vector, but
93 /// with the element type converted to an integer type with the same
95 EVT
changeVectorElementTypeToInteger() const {
97 assert (!isScalableVector() &&
98 "We don't support extended scalable types yet");
99 return changeExtendedVectorElementTypeToInteger();
101 MVT EltTy
= getSimpleVT().getVectorElementType();
102 unsigned BitWidth
= EltTy
.getSizeInBits();
103 MVT IntTy
= MVT::getIntegerVT(BitWidth
);
104 MVT VecTy
= MVT::getVectorVT(IntTy
, getVectorNumElements(),
106 assert(VecTy
.SimpleTy
!= MVT::INVALID_SIMPLE_VALUE_TYPE
&&
107 "Simple vector VT not representable by simple integer vector VT!");
111 /// Return the type converted to an equivalently sized integer or vector
112 /// with integer element type. Similar to changeVectorElementTypeToInteger,
113 /// but also handles scalars.
114 EVT
changeTypeToInteger() {
116 return changeVectorElementTypeToInteger();
119 return MVT::getIntegerVT(getSizeInBits());
121 return changeExtendedTypeToInteger();
124 /// Test if the given EVT is simple (as opposed to being extended).
125 bool isSimple() const {
126 return V
.SimpleTy
!= MVT::INVALID_SIMPLE_VALUE_TYPE
;
129 /// Test if the given EVT is extended (as opposed to being simple).
130 bool isExtended() const {
134 /// Return true if this is a FP or a vector FP type.
135 bool isFloatingPoint() const {
136 return isSimple() ? V
.isFloatingPoint() : isExtendedFloatingPoint();
139 /// Return true if this is an integer or a vector integer type.
140 bool isInteger() const {
141 return isSimple() ? V
.isInteger() : isExtendedInteger();
144 /// Return true if this is an integer, but not a vector.
145 bool isScalarInteger() const {
146 return isSimple() ? V
.isScalarInteger() : isExtendedScalarInteger();
149 /// Return true if this is a vector value type.
150 bool isVector() const {
151 return isSimple() ? V
.isVector() : isExtendedVector();
154 /// Return true if this is a vector type where the runtime
155 /// length is machine dependent
156 bool isScalableVector() const {
157 // FIXME: We don't support extended scalable types yet, because the
158 // matching IR type doesn't exist. Once it has been added, this can
159 // be changed to call isExtendedScalableVector.
162 return V
.isScalableVector();
165 /// Return true if this is a 16-bit vector type.
166 bool is16BitVector() const {
167 return isSimple() ? V
.is16BitVector() : isExtended16BitVector();
170 /// Return true if this is a 32-bit vector type.
171 bool is32BitVector() const {
172 return isSimple() ? V
.is32BitVector() : isExtended32BitVector();
175 /// Return true if this is a 64-bit vector type.
176 bool is64BitVector() const {
177 return isSimple() ? V
.is64BitVector() : isExtended64BitVector();
180 /// Return true if this is a 128-bit vector type.
181 bool is128BitVector() const {
182 return isSimple() ? V
.is128BitVector() : isExtended128BitVector();
185 /// Return true if this is a 256-bit vector type.
186 bool is256BitVector() const {
187 return isSimple() ? V
.is256BitVector() : isExtended256BitVector();
190 /// Return true if this is a 512-bit vector type.
191 bool is512BitVector() const {
192 return isSimple() ? V
.is512BitVector() : isExtended512BitVector();
195 /// Return true if this is a 1024-bit vector type.
196 bool is1024BitVector() const {
197 return isSimple() ? V
.is1024BitVector() : isExtended1024BitVector();
200 /// Return true if this is a 2048-bit vector type.
201 bool is2048BitVector() const {
202 return isSimple() ? V
.is2048BitVector() : isExtended2048BitVector();
205 /// Return true if this is an overloaded type for TableGen.
206 bool isOverloaded() const {
207 return (V
==MVT::iAny
|| V
==MVT::fAny
|| V
==MVT::vAny
|| V
==MVT::iPTRAny
);
210 /// Return true if the bit size is a multiple of 8.
211 bool isByteSized() const {
212 return (getSizeInBits() & 7) == 0;
215 /// Return true if the size is a power-of-two number of bytes.
216 bool isRound() const {
217 unsigned BitSize
= getSizeInBits();
218 return BitSize
>= 8 && !(BitSize
& (BitSize
- 1));
221 /// Return true if this has the same number of bits as VT.
222 bool bitsEq(EVT VT
) const {
223 if (EVT::operator==(VT
)) return true;
224 return getSizeInBits() == VT
.getSizeInBits();
227 /// Return true if this has more bits than VT.
228 bool bitsGT(EVT VT
) const {
229 if (EVT::operator==(VT
)) return false;
230 return getSizeInBits() > VT
.getSizeInBits();
233 /// Return true if this has no less bits than VT.
234 bool bitsGE(EVT VT
) const {
235 if (EVT::operator==(VT
)) return true;
236 return getSizeInBits() >= VT
.getSizeInBits();
239 /// Return true if this has less bits than VT.
240 bool bitsLT(EVT VT
) const {
241 if (EVT::operator==(VT
)) return false;
242 return getSizeInBits() < VT
.getSizeInBits();
245 /// Return true if this has no more bits than VT.
246 bool bitsLE(EVT VT
) const {
247 if (EVT::operator==(VT
)) return true;
248 return getSizeInBits() <= VT
.getSizeInBits();
251 /// Return the SimpleValueType held in the specified simple EVT.
252 MVT
getSimpleVT() const {
253 assert(isSimple() && "Expected a SimpleValueType!");
257 /// If this is a vector type, return the element type, otherwise return
259 EVT
getScalarType() const {
260 return isVector() ? getVectorElementType() : *this;
263 /// Given a vector type, return the type of each element.
264 EVT
getVectorElementType() const {
265 assert(isVector() && "Invalid vector type!");
267 return V
.getVectorElementType();
268 return getExtendedVectorElementType();
271 /// Given a vector type, return the number of elements it contains.
272 unsigned getVectorNumElements() const {
273 assert(isVector() && "Invalid vector type!");
275 return V
.getVectorNumElements();
276 return getExtendedVectorNumElements();
279 // Given a (possibly scalable) vector type, return the ElementCount
280 ElementCount
getVectorElementCount() const {
281 assert((isVector()) && "Invalid vector type!");
283 return V
.getVectorElementCount();
285 assert(!isScalableVector() &&
286 "We don't support extended scalable types yet");
287 return {getExtendedVectorNumElements(), false};
290 /// Return the size of the specified value type in bits.
291 unsigned getSizeInBits() const {
293 return V
.getSizeInBits();
294 return getExtendedSizeInBits();
297 unsigned getScalarSizeInBits() const {
298 return getScalarType().getSizeInBits();
301 /// Return the number of bytes overwritten by a store of the specified value
303 unsigned getStoreSize() const {
304 return (getSizeInBits() + 7) / 8;
307 /// Return the number of bits overwritten by a store of the specified value
309 unsigned getStoreSizeInBits() const {
310 return getStoreSize() * 8;
313 /// Rounds the bit-width of the given integer EVT up to the nearest power of
314 /// two (and at least to eight), and returns the integer EVT with that
316 EVT
getRoundIntegerType(LLVMContext
&Context
) const {
317 assert(isInteger() && !isVector() && "Invalid integer type!");
318 unsigned BitWidth
= getSizeInBits();
321 return getIntegerVT(Context
, 1 << Log2_32_Ceil(BitWidth
));
324 /// Finds the smallest simple value type that is greater than or equal to
325 /// half the width of this EVT. If no simple value type can be found, an
326 /// extended integer value type of half the size (rounded up) is returned.
327 EVT
getHalfSizedIntegerVT(LLVMContext
&Context
) const {
328 assert(isInteger() && !isVector() && "Invalid integer type!");
329 unsigned EVTSize
= getSizeInBits();
330 for (unsigned IntVT
= MVT::FIRST_INTEGER_VALUETYPE
;
331 IntVT
<= MVT::LAST_INTEGER_VALUETYPE
; ++IntVT
) {
332 EVT HalfVT
= EVT((MVT::SimpleValueType
)IntVT
);
333 if (HalfVT
.getSizeInBits() * 2 >= EVTSize
)
336 return getIntegerVT(Context
, (EVTSize
+ 1) / 2);
339 /// Return a VT for an integer vector type with the size of the
340 /// elements doubled. The typed returned may be an extended type.
341 EVT
widenIntegerVectorElementType(LLVMContext
&Context
) const {
342 EVT EltVT
= getVectorElementType();
343 EltVT
= EVT::getIntegerVT(Context
, 2 * EltVT
.getSizeInBits());
344 return EVT::getVectorVT(Context
, EltVT
, getVectorElementCount());
347 // Return a VT for a vector type with the same element type but
348 // half the number of elements. The type returned may be an
350 EVT
getHalfNumVectorElementsVT(LLVMContext
&Context
) const {
351 EVT EltVT
= getVectorElementType();
352 auto EltCnt
= getVectorElementCount();
353 assert(!(EltCnt
.Min
& 1) && "Splitting vector, but not in half!");
354 return EVT::getVectorVT(Context
, EltVT
, EltCnt
/ 2);
357 /// Returns true if the given vector is a power of 2.
358 bool isPow2VectorType() const {
359 unsigned NElts
= getVectorNumElements();
360 return !(NElts
& (NElts
- 1));
363 /// Widens the length of the given vector EVT up to the nearest power of 2
364 /// and returns that type.
365 EVT
getPow2VectorType(LLVMContext
&Context
) const {
366 if (!isPow2VectorType()) {
367 unsigned NElts
= getVectorNumElements();
368 unsigned Pow2NElts
= 1 << Log2_32_Ceil(NElts
);
369 return EVT::getVectorVT(Context
, getVectorElementType(), Pow2NElts
,
377 /// This function returns value type as a string, e.g. "i32".
378 std::string
getEVTString() const;
380 /// This method returns an LLVM type corresponding to the specified EVT.
381 /// For integer types, this returns an unsigned type. Note that this will
382 /// abort for types that cannot be represented.
383 Type
*getTypeForEVT(LLVMContext
&Context
) const;
385 /// Return the value type corresponding to the specified type.
386 /// This returns all pointers as iPTR. If HandleUnknown is true, unknown
387 /// types are returned as Other, otherwise they are invalid.
388 static EVT
getEVT(Type
*Ty
, bool HandleUnknown
= false);
390 intptr_t getRawBits() const {
394 return (intptr_t)(LLVMTy
);
397 /// A meaningless but well-behaved order, useful for constructing
399 struct compareRawBits
{
400 bool operator()(EVT L
, EVT R
) const {
401 if (L
.V
.SimpleTy
== R
.V
.SimpleTy
)
402 return L
.LLVMTy
< R
.LLVMTy
;
404 return L
.V
.SimpleTy
< R
.V
.SimpleTy
;
409 // Methods for handling the Extended-type case in functions above.
410 // These are all out-of-line to prevent users of this header file
411 // from having a dependency on Type.h.
412 EVT
changeExtendedTypeToInteger() const;
413 EVT
changeExtendedVectorElementTypeToInteger() const;
414 static EVT
getExtendedIntegerVT(LLVMContext
&C
, unsigned BitWidth
);
415 static EVT
getExtendedVectorVT(LLVMContext
&C
, EVT VT
,
416 unsigned NumElements
);
417 bool isExtendedFloatingPoint() const LLVM_READONLY
;
418 bool isExtendedInteger() const LLVM_READONLY
;
419 bool isExtendedScalarInteger() const LLVM_READONLY
;
420 bool isExtendedVector() const LLVM_READONLY
;
421 bool isExtended16BitVector() const LLVM_READONLY
;
422 bool isExtended32BitVector() const LLVM_READONLY
;
423 bool isExtended64BitVector() const LLVM_READONLY
;
424 bool isExtended128BitVector() const LLVM_READONLY
;
425 bool isExtended256BitVector() const LLVM_READONLY
;
426 bool isExtended512BitVector() const LLVM_READONLY
;
427 bool isExtended1024BitVector() const LLVM_READONLY
;
428 bool isExtended2048BitVector() const LLVM_READONLY
;
429 EVT
getExtendedVectorElementType() const;
430 unsigned getExtendedVectorNumElements() const LLVM_READONLY
;
431 unsigned getExtendedSizeInBits() const LLVM_READONLY
;
434 } // end namespace llvm
436 #endif // LLVM_CODEGEN_VALUETYPES_H