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 /// isSimple - Test if the given EVT is simple (as opposed to being
94 bool isSimple() const {
95 return V.SimpleTy >= 0;
98 /// isExtended - Test if the given EVT is extended (as opposed to
100 bool isExtended() const {
104 /// isFloatingPoint - Return true if this is a FP, or a vector FP type.
105 bool isFloatingPoint() const {
106 return isSimple() ? V.isFloatingPoint() : isExtendedFloatingPoint();
109 /// isInteger - Return true if this is an integer, or a vector integer type.
110 bool isInteger() const {
111 return isSimple() ? V.isInteger() : isExtendedInteger();
114 /// isVector - Return true if this is a vector value type.
115 bool isVector() const {
116 return isSimple() ? V.isVector() : isExtendedVector();
119 /// is16BitVector - Return true if this is a 16-bit vector type.
120 bool is16BitVector() const {
121 return isSimple() ? V.is16BitVector() : isExtended16BitVector();
124 /// is32BitVector - Return true if this is a 32-bit vector type.
125 bool is32BitVector() const {
126 return isSimple() ? V.is32BitVector() : isExtended32BitVector();
129 /// is64BitVector - Return true if this is a 64-bit vector type.
130 bool is64BitVector() const {
131 return isSimple() ? V.is64BitVector() : isExtended64BitVector();
134 /// is128BitVector - Return true if this is a 128-bit vector type.
135 bool is128BitVector() const {
136 return isSimple() ? V.is128BitVector() : isExtended128BitVector();
139 /// is256BitVector - Return true if this is a 256-bit vector type.
140 bool is256BitVector() const {
141 return isSimple() ? V.is256BitVector() : isExtended256BitVector();
144 /// is512BitVector - Return true if this is a 512-bit vector type.
145 bool is512BitVector() const {
146 return isSimple() ? V.is512BitVector() : isExtended512BitVector();
149 /// is1024BitVector - Return true if this is a 1024-bit vector type.
150 bool is1024BitVector() const {
151 return isSimple() ? V.is1024BitVector() : isExtended1024BitVector();
154 /// isOverloaded - Return true if this is an overloaded type for TableGen.
155 bool isOverloaded() const {
156 return (V==MVT::iAny || V==MVT::fAny || V==MVT::vAny || V==MVT::iPTRAny);
159 /// isByteSized - Return true if the bit size is a multiple of 8.
160 bool isByteSized() const {
161 return (getSizeInBits() & 7) == 0;
164 /// isRound - Return true if the size is a power-of-two number of bytes.
165 bool isRound() const {
166 unsigned BitSize = getSizeInBits();
167 return BitSize >= 8 && !(BitSize & (BitSize - 1));
170 /// bitsEq - Return true if this has the same number of bits as VT.
171 bool bitsEq(EVT VT) const {
172 if (EVT::operator==(VT)) return true;
173 return getSizeInBits() == VT.getSizeInBits();
176 /// bitsGT - Return true if this has more bits than VT.
177 bool bitsGT(EVT VT) const {
178 if (EVT::operator==(VT)) return false;
179 return getSizeInBits() > VT.getSizeInBits();
182 /// bitsGE - Return true if this has no less bits than VT.
183 bool bitsGE(EVT VT) const {
184 if (EVT::operator==(VT)) return true;
185 return getSizeInBits() >= VT.getSizeInBits();
188 /// bitsLT - Return true if this has less bits than VT.
189 bool bitsLT(EVT VT) const {
190 if (EVT::operator==(VT)) return false;
191 return getSizeInBits() < VT.getSizeInBits();
194 /// bitsLE - Return true if this has no more bits than VT.
195 bool bitsLE(EVT VT) const {
196 if (EVT::operator==(VT)) return true;
197 return getSizeInBits() <= VT.getSizeInBits();
201 /// getSimpleVT - Return the SimpleValueType held in the specified
203 MVT getSimpleVT() const {
204 assert(isSimple() && "Expected a SimpleValueType!");
208 /// getScalarType - If this is a vector type, return the element type,
209 /// otherwise return this.
210 EVT getScalarType() const {
211 return isVector() ? getVectorElementType() : *this;
214 /// getVectorElementType - Given a vector type, return the type of
216 EVT getVectorElementType() const {
217 assert(isVector() && "Invalid vector type!");
219 return V.getVectorElementType();
220 return getExtendedVectorElementType();
223 /// getVectorNumElements - Given a vector type, return the number of
224 /// elements it contains.
225 unsigned getVectorNumElements() const {
226 assert(isVector() && "Invalid vector type!");
228 return V.getVectorNumElements();
229 return getExtendedVectorNumElements();
232 /// getSizeInBits - Return the size of the specified value type in bits.
233 unsigned getSizeInBits() const {
235 return V.getSizeInBits();
236 return getExtendedSizeInBits();
239 unsigned getScalarSizeInBits() const {
240 return getScalarType().getSizeInBits();
243 /// getStoreSize - Return the number of bytes overwritten by a store
244 /// of the specified value type.
245 unsigned getStoreSize() const {
246 return (getSizeInBits() + 7) / 8;
249 /// getStoreSizeInBits - Return the number of bits overwritten by a store
250 /// of the specified value type.
251 unsigned getStoreSizeInBits() const {
252 return getStoreSize() * 8;
255 /// getRoundIntegerType - Rounds the bit-width of the given integer EVT up
256 /// to the nearest power of two (and at least to eight), and returns the
257 /// integer EVT with that number of bits.
258 EVT getRoundIntegerType(LLVMContext &Context) const {
259 assert(isInteger() && !isVector() && "Invalid integer type!");
260 unsigned BitWidth = getSizeInBits();
263 return getIntegerVT(Context, 1 << Log2_32_Ceil(BitWidth));
266 /// getHalfSizedIntegerVT - Finds the smallest simple value type that is
267 /// greater than or equal to half the width of this EVT. If no simple
268 /// value type can be found, an extended integer value type of half the
269 /// size (rounded up) is returned.
270 EVT getHalfSizedIntegerVT(LLVMContext &Context) const {
271 assert(isInteger() && !isVector() && "Invalid integer type!");
272 unsigned EVTSize = getSizeInBits();
273 for (unsigned IntVT = MVT::FIRST_INTEGER_VALUETYPE;
274 IntVT <= MVT::LAST_INTEGER_VALUETYPE; ++IntVT) {
275 EVT HalfVT = EVT((MVT::SimpleValueType)IntVT);
276 if (HalfVT.getSizeInBits() * 2 >= EVTSize)
279 return getIntegerVT(Context, (EVTSize + 1) / 2);
282 /// \brief Return a VT for an integer vector type with the size of the
283 /// elements doubled. The typed returned may be an extended type.
284 EVT widenIntegerVectorElementType(LLVMContext &Context) const {
285 EVT EltVT = getVectorElementType();
286 EltVT = EVT::getIntegerVT(Context, 2 * EltVT.getSizeInBits());
287 return EVT::getVectorVT(Context, EltVT, getVectorNumElements());
290 /// isPow2VectorType - Returns true if the given vector is a power of 2.
291 bool isPow2VectorType() const {
292 unsigned NElts = getVectorNumElements();
293 return !(NElts & (NElts - 1));
296 /// getPow2VectorType - Widens the length of the given vector EVT up to
297 /// the nearest power of 2 and returns that type.
298 EVT getPow2VectorType(LLVMContext &Context) const {
299 if (!isPow2VectorType()) {
300 unsigned NElts = getVectorNumElements();
301 unsigned Pow2NElts = 1 << Log2_32_Ceil(NElts);
302 return EVT::getVectorVT(Context, getVectorElementType(), Pow2NElts);
309 /// getEVTString - This function returns value type as a string,
311 std::string getEVTString() const;
313 /// getTypeForEVT - This method returns an LLVM type corresponding to the
314 /// specified EVT. For integer types, this returns an unsigned type. Note
315 /// that this will abort for types that cannot be represented.
316 Type *getTypeForEVT(LLVMContext &Context) const;
318 /// getEVT - Return the value type corresponding to the specified type.
319 /// This returns all pointers as iPTR. If HandleUnknown is true, unknown
320 /// types are returned as Other, otherwise they are invalid.
321 static EVT getEVT(Type *Ty, bool HandleUnknown = false);
323 intptr_t getRawBits() const {
327 return (intptr_t)(LLVMTy);
330 /// compareRawBits - A meaningless but well-behaved order, useful for
331 /// constructing containers.
332 struct compareRawBits {
333 bool operator()(EVT L, EVT R) const {
334 if (L.V.SimpleTy == R.V.SimpleTy)
335 return L.LLVMTy < R.LLVMTy;
337 return L.V.SimpleTy < R.V.SimpleTy;
342 // Methods for handling the Extended-type case in functions above.
343 // These are all out-of-line to prevent users of this header file
344 // from having a dependency on Type.h.
345 EVT changeExtendedVectorElementTypeToInteger() const;
346 static EVT getExtendedIntegerVT(LLVMContext &C, unsigned BitWidth);
347 static EVT getExtendedVectorVT(LLVMContext &C, EVT VT,
348 unsigned NumElements);
349 bool isExtendedFloatingPoint() const LLVM_READONLY;
350 bool isExtendedInteger() const LLVM_READONLY;
351 bool isExtendedVector() const LLVM_READONLY;
352 bool isExtended16BitVector() const LLVM_READONLY;
353 bool isExtended32BitVector() const LLVM_READONLY;
354 bool isExtended64BitVector() const LLVM_READONLY;
355 bool isExtended128BitVector() const LLVM_READONLY;
356 bool isExtended256BitVector() const LLVM_READONLY;
357 bool isExtended512BitVector() const LLVM_READONLY;
358 bool isExtended1024BitVector() const LLVM_READONLY;
359 EVT getExtendedVectorElementType() const;
360 unsigned getExtendedVectorNumElements() const LLVM_READONLY;
361 unsigned getExtendedSizeInBits() const;
364 } // End llvm namespace