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 EVT() : V((MVT::SimpleValueType)(MVT::INVALID_SIMPLE_VALUE_TYPE)),
39 EVT(MVT::SimpleValueType SVT) : V(SVT), LLVMTy(nullptr) { }
40 EVT(MVT S) : V(S), LLVMTy(nullptr) {}
42 bool operator==(EVT VT) const {
43 return !(*this != VT);
45 bool operator!=(EVT VT) const {
46 if (V.SimpleTy != VT.V.SimpleTy)
49 return LLVMTy != VT.LLVMTy;
53 /// getFloatingPointVT - Returns the EVT that represents a floating point
54 /// type with the given number of bits. There are two floating point types
55 /// with 128 bits - this returns f128 rather than ppcf128.
56 static EVT getFloatingPointVT(unsigned BitWidth) {
57 return MVT::getFloatingPointVT(BitWidth);
60 /// getIntegerVT - Returns the EVT that represents an integer with the given
62 static EVT getIntegerVT(LLVMContext &Context, unsigned BitWidth) {
63 MVT M = MVT::getIntegerVT(BitWidth);
66 return getExtendedIntegerVT(Context, BitWidth);
69 /// getVectorVT - Returns the EVT that represents a vector NumElements in
70 /// length, where each element is of type VT.
71 static EVT getVectorVT(LLVMContext &Context, EVT VT, unsigned NumElements) {
72 MVT M = MVT::getVectorVT(VT.V, NumElements);
75 return getExtendedVectorVT(Context, VT, NumElements);
78 /// changeVectorElementTypeToInteger - Return a vector with the same number
79 /// of elements as this vector, but with the element type converted to an
80 /// integer type with the same bitwidth.
81 EVT changeVectorElementTypeToInteger() const {
83 return changeExtendedVectorElementTypeToInteger();
84 MVT EltTy = getSimpleVT().getVectorElementType();
85 unsigned BitWidth = EltTy.getSizeInBits();
86 MVT IntTy = MVT::getIntegerVT(BitWidth);
87 MVT VecTy = MVT::getVectorVT(IntTy, getVectorNumElements());
88 assert(VecTy.SimpleTy >= 0 &&
89 "Simple vector VT not representable by simple integer vector VT!");
93 /// isSimple - Test if the given EVT is simple (as opposed to being
95 bool isSimple() const {
96 return V.SimpleTy >= 0;
99 /// isExtended - Test if the given EVT is extended (as opposed to
101 bool isExtended() const {
105 /// isFloatingPoint - Return true if this is a FP, or a vector FP type.
106 bool isFloatingPoint() const {
107 return isSimple() ? V.isFloatingPoint() : isExtendedFloatingPoint();
110 /// isInteger - Return true if this is an integer, or a vector integer type.
111 bool isInteger() const {
112 return isSimple() ? V.isInteger() : isExtendedInteger();
115 /// isVector - Return true if this is a vector value type.
116 bool isVector() const {
117 return isSimple() ? V.isVector() : isExtendedVector();
120 /// is16BitVector - Return true if this is a 16-bit vector type.
121 bool is16BitVector() const {
122 return isSimple() ? V.is16BitVector() : isExtended16BitVector();
125 /// is32BitVector - Return true if this is a 32-bit vector type.
126 bool is32BitVector() const {
127 return isSimple() ? V.is32BitVector() : isExtended32BitVector();
130 /// is64BitVector - Return true if this is a 64-bit vector type.
131 bool is64BitVector() const {
132 return isSimple() ? V.is64BitVector() : isExtended64BitVector();
135 /// is128BitVector - Return true if this is a 128-bit vector type.
136 bool is128BitVector() const {
137 return isSimple() ? V.is128BitVector() : isExtended128BitVector();
140 /// is256BitVector - Return true if this is a 256-bit vector type.
141 bool is256BitVector() const {
142 return isSimple() ? V.is256BitVector() : isExtended256BitVector();
145 /// is512BitVector - Return true if this is a 512-bit vector type.
146 bool is512BitVector() const {
147 return isSimple() ? V.is512BitVector() : isExtended512BitVector();
150 /// is1024BitVector - Return true if this is a 1024-bit vector type.
151 bool is1024BitVector() const {
152 return isSimple() ? V.is1024BitVector() : isExtended1024BitVector();
155 /// isOverloaded - Return true if this is an overloaded type for TableGen.
156 bool isOverloaded() const {
157 return (V==MVT::iAny || V==MVT::fAny || V==MVT::vAny || V==MVT::iPTRAny);
160 /// isByteSized - Return true if the bit size is a multiple of 8.
161 bool isByteSized() const {
162 return (getSizeInBits() & 7) == 0;
165 /// isRound - Return true if the size is a power-of-two number of bytes.
166 bool isRound() const {
167 unsigned BitSize = getSizeInBits();
168 return BitSize >= 8 && !(BitSize & (BitSize - 1));
171 /// bitsEq - Return true if this has the same number of bits as VT.
172 bool bitsEq(EVT VT) const {
173 if (EVT::operator==(VT)) return true;
174 return getSizeInBits() == VT.getSizeInBits();
177 /// bitsGT - Return true if this has more bits than VT.
178 bool bitsGT(EVT VT) const {
179 if (EVT::operator==(VT)) return false;
180 return getSizeInBits() > VT.getSizeInBits();
183 /// bitsGE - Return true if this has no less bits than VT.
184 bool bitsGE(EVT VT) const {
185 if (EVT::operator==(VT)) return true;
186 return getSizeInBits() >= VT.getSizeInBits();
189 /// bitsLT - Return true if this has less bits than VT.
190 bool bitsLT(EVT VT) const {
191 if (EVT::operator==(VT)) return false;
192 return getSizeInBits() < VT.getSizeInBits();
195 /// bitsLE - Return true if this has no more bits than VT.
196 bool bitsLE(EVT VT) const {
197 if (EVT::operator==(VT)) return true;
198 return getSizeInBits() <= VT.getSizeInBits();
202 /// getSimpleVT - Return the SimpleValueType held in the specified
204 MVT getSimpleVT() const {
205 assert(isSimple() && "Expected a SimpleValueType!");
209 /// getScalarType - If this is a vector type, return the element type,
210 /// otherwise return this.
211 EVT getScalarType() const {
212 return isVector() ? getVectorElementType() : *this;
215 /// getVectorElementType - Given a vector type, return the type of
217 EVT getVectorElementType() const {
218 assert(isVector() && "Invalid vector type!");
220 return V.getVectorElementType();
221 return getExtendedVectorElementType();
224 /// getVectorNumElements - Given a vector type, return the number of
225 /// elements it contains.
226 unsigned getVectorNumElements() const {
227 assert(isVector() && "Invalid vector type!");
229 return V.getVectorNumElements();
230 return getExtendedVectorNumElements();
233 /// getSizeInBits - Return the size of the specified value type in bits.
234 unsigned getSizeInBits() const {
236 return V.getSizeInBits();
237 return getExtendedSizeInBits();
240 unsigned getScalarSizeInBits() const {
241 return getScalarType().getSizeInBits();
244 /// getStoreSize - Return the number of bytes overwritten by a store
245 /// of the specified value type.
246 unsigned getStoreSize() const {
247 return (getSizeInBits() + 7) / 8;
250 /// getStoreSizeInBits - Return the number of bits overwritten by a store
251 /// of the specified value type.
252 unsigned getStoreSizeInBits() const {
253 return getStoreSize() * 8;
256 /// getRoundIntegerType - Rounds the bit-width of the given integer EVT up
257 /// to the nearest power of two (and at least to eight), and returns the
258 /// integer EVT with that number of bits.
259 EVT getRoundIntegerType(LLVMContext &Context) const {
260 assert(isInteger() && !isVector() && "Invalid integer type!");
261 unsigned BitWidth = getSizeInBits();
264 return getIntegerVT(Context, 1 << Log2_32_Ceil(BitWidth));
267 /// getHalfSizedIntegerVT - Finds the smallest simple value type that is
268 /// greater than or equal to half the width of this EVT. If no simple
269 /// value type can be found, an extended integer value type of half the
270 /// size (rounded up) is returned.
271 EVT getHalfSizedIntegerVT(LLVMContext &Context) const {
272 assert(isInteger() && !isVector() && "Invalid integer type!");
273 unsigned EVTSize = getSizeInBits();
274 for (unsigned IntVT = MVT::FIRST_INTEGER_VALUETYPE;
275 IntVT <= MVT::LAST_INTEGER_VALUETYPE; ++IntVT) {
276 EVT HalfVT = EVT((MVT::SimpleValueType)IntVT);
277 if (HalfVT.getSizeInBits() * 2 >= EVTSize)
280 return getIntegerVT(Context, (EVTSize + 1) / 2);
283 /// \brief Return a VT for an integer vector type with the size of the
284 /// elements doubled. The typed returned may be an extended type.
285 EVT widenIntegerVectorElementType(LLVMContext &Context) const {
286 EVT EltVT = getVectorElementType();
287 EltVT = EVT::getIntegerVT(Context, 2 * EltVT.getSizeInBits());
288 return EVT::getVectorVT(Context, EltVT, getVectorNumElements());
291 /// isPow2VectorType - Returns true if the given vector is a power of 2.
292 bool isPow2VectorType() const {
293 unsigned NElts = getVectorNumElements();
294 return !(NElts & (NElts - 1));
297 /// getPow2VectorType - Widens the length of the given vector EVT up to
298 /// the nearest power of 2 and returns that type.
299 EVT getPow2VectorType(LLVMContext &Context) const {
300 if (!isPow2VectorType()) {
301 unsigned NElts = getVectorNumElements();
302 unsigned Pow2NElts = 1 << Log2_32_Ceil(NElts);
303 return EVT::getVectorVT(Context, getVectorElementType(), Pow2NElts);
310 /// getEVTString - This function returns value type as a string,
312 std::string getEVTString() const;
314 /// getTypeForEVT - This method returns an LLVM type corresponding to the
315 /// specified EVT. For integer types, this returns an unsigned type. Note
316 /// that this will abort for types that cannot be represented.
317 Type *getTypeForEVT(LLVMContext &Context) const;
319 /// getEVT - Return the value type corresponding to the specified type.
320 /// This returns all pointers as iPTR. If HandleUnknown is true, unknown
321 /// types are returned as Other, otherwise they are invalid.
322 static EVT getEVT(Type *Ty, bool HandleUnknown = false);
324 intptr_t getRawBits() const {
328 return (intptr_t)(LLVMTy);
331 /// compareRawBits - A meaningless but well-behaved order, useful for
332 /// constructing containers.
333 struct compareRawBits {
334 bool operator()(EVT L, EVT R) const {
335 if (L.V.SimpleTy == R.V.SimpleTy)
336 return L.LLVMTy < R.LLVMTy;
338 return L.V.SimpleTy < R.V.SimpleTy;
343 // Methods for handling the Extended-type case in functions above.
344 // These are all out-of-line to prevent users of this header file
345 // from having a dependency on Type.h.
346 EVT changeExtendedVectorElementTypeToInteger() const;
347 static EVT getExtendedIntegerVT(LLVMContext &C, unsigned BitWidth);
348 static EVT getExtendedVectorVT(LLVMContext &C, EVT VT,
349 unsigned NumElements);
350 bool isExtendedFloatingPoint() const LLVM_READONLY;
351 bool isExtendedInteger() const LLVM_READONLY;
352 bool isExtendedVector() const LLVM_READONLY;
353 bool isExtended16BitVector() const LLVM_READONLY;
354 bool isExtended32BitVector() const LLVM_READONLY;
355 bool isExtended64BitVector() const LLVM_READONLY;
356 bool isExtended128BitVector() const LLVM_READONLY;
357 bool isExtended256BitVector() const LLVM_READONLY;
358 bool isExtended512BitVector() const LLVM_READONLY;
359 bool isExtended1024BitVector() const LLVM_READONLY;
360 EVT getExtendedVectorElementType() const;
361 unsigned getExtendedVectorNumElements() const LLVM_READONLY;
362 unsigned getExtendedSizeInBits() const;
365 } // End llvm namespace