1 //===- CodeGen/ValueTypes.h - Low-Level Target independ. types --*- C++ -*-===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
8 //
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.
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #ifndef LLVM_CODEGEN_VALUETYPES_H
16 #define LLVM_CODEGEN_VALUETYPES_H
17 
18 #include "llvm/Support/Compiler.h"
19 #include "llvm/Support/MachineValueType.h"
20 #include "llvm/Support/MathExtras.h"
21 #include "llvm/Support/TypeSize.h"
22 #include "llvm/Support/WithColor.h"
23 #include <cassert>
24 #include <cstdint>
25 #include <string>
26 
27 namespace llvm {
28 
29   class LLVMContext;
30   class Type;
31 
32   /// Extended Value Type. Capable of holding value types which are not native
33   /// for any processor (such as the i12345 type), as well as the types an MVT
34   /// can represent.
35   struct EVT {
36   private:
37     MVT V = MVT::INVALID_SIMPLE_VALUE_TYPE;
38     Type *LLVMTy = nullptr;
39 
40   public:
41     constexpr EVT() = default;
42     constexpr EVT(MVT::SimpleValueType SVT) : V(SVT) {}
43     constexpr EVT(MVT S) : V(S) {}
44 
45     bool operator==(EVT VT) const {
46       return !(*this != VT);
47     }
48     bool operator!=(EVT VT) const {
49       if (V.SimpleTy != VT.V.SimpleTy)
50         return true;
51       if (V.SimpleTy == MVT::INVALID_SIMPLE_VALUE_TYPE)
52         return LLVMTy != VT.LLVMTy;
53       return false;
54     }
55 
56     /// Returns the EVT that represents a floating-point type with the given
57     /// number of bits. There are two floating-point types with 128 bits - this
58     /// returns f128 rather than ppcf128.
59     static EVT getFloatingPointVT(unsigned BitWidth) {
60       return MVT::getFloatingPointVT(BitWidth);
61     }
62 
63     /// Returns the EVT that represents an integer with the given number of
64     /// bits.
65     static EVT getIntegerVT(LLVMContext &Context, unsigned BitWidth) {
66       MVT M = MVT::getIntegerVT(BitWidth);
67       if (M.SimpleTy != MVT::INVALID_SIMPLE_VALUE_TYPE)
68         return M;
69       return getExtendedIntegerVT(Context, BitWidth);
70     }
71 
72     /// Returns the EVT that represents a vector NumElements in length, where
73     /// each element is of type VT.
74     static EVT getVectorVT(LLVMContext &Context, EVT VT, unsigned NumElements,
75                            bool IsScalable = false) {
76       MVT M = MVT::getVectorVT(VT.V, NumElements, IsScalable);
77       if (M.SimpleTy != MVT::INVALID_SIMPLE_VALUE_TYPE)
78         return M;
79       return getExtendedVectorVT(Context, VT, NumElements, IsScalable);
80     }
81 
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)
87         return M;
88       return getExtendedVectorVT(Context, VT, EC);
89     }
90 
91     /// Return a vector with the same number of elements as this vector, but
92     /// with the element type converted to an integer type with the same
93     /// bitwidth.
94     EVT changeVectorElementTypeToInteger() const {
95       if (isSimple())
96         return getSimpleVT().changeVectorElementTypeToInteger();
97       return changeExtendedVectorElementTypeToInteger();
98     }
99 
100     /// Return a VT for a vector type whose attributes match ourselves
101     /// with the exception of the element type that is chosen by the caller.
102     EVT changeVectorElementType(EVT EltVT) const {
103       if (isSimple() && EltVT.isSimple())
104         return getSimpleVT().changeVectorElementType(EltVT.getSimpleVT());
105       return changeExtendedVectorElementType(EltVT);
106     }
107 
108     /// Return the type converted to an equivalently sized integer or vector
109     /// with integer element type. Similar to changeVectorElementTypeToInteger,
110     /// but also handles scalars.
111     EVT changeTypeToInteger() {
112       if (isVector())
113         return changeVectorElementTypeToInteger();
114 
115       if (isSimple())
116         return getSimpleVT().changeTypeToInteger();
117       return changeExtendedTypeToInteger();
118     }
119 
120     /// Test if the given EVT is simple (as opposed to being extended).
121     bool isSimple() const {
122       return V.SimpleTy != MVT::INVALID_SIMPLE_VALUE_TYPE;
123     }
124 
125     /// Test if the given EVT is extended (as opposed to being simple).
126     bool isExtended() const {
127       return !isSimple();
128     }
129 
130     /// Return true if this is a FP or a vector FP type.
131     bool isFloatingPoint() const {
132       return isSimple() ? V.isFloatingPoint() : isExtendedFloatingPoint();
133     }
134 
135     /// Return true if this is an integer or a vector integer type.
136     bool isInteger() const {
137       return isSimple() ? V.isInteger() : isExtendedInteger();
138     }
139 
140     /// Return true if this is an integer, but not a vector.
141     bool isScalarInteger() const {
142       return isSimple() ? V.isScalarInteger() : isExtendedScalarInteger();
143     }
144 
145     /// Return true if this is a vector value type.
146     bool isVector() const {
147       return isSimple() ? V.isVector() : isExtendedVector();
148     }
149 
150     /// Return true if this is a vector type where the runtime
151     /// length is machine dependent
152     bool isScalableVector() const {
153       return isSimple() ? V.isScalableVector() : isExtendedScalableVector();
154     }
155 
156     bool isFixedLengthVector() const {
157       return isSimple() ? V.isFixedLengthVector()
158                         : isExtendedFixedLengthVector();
159     }
160 
161     /// Return true if this is a 16-bit vector type.
162     bool is16BitVector() const {
163       return isSimple() ? V.is16BitVector() : isExtended16BitVector();
164     }
165 
166     /// Return true if this is a 32-bit vector type.
167     bool is32BitVector() const {
168       return isSimple() ? V.is32BitVector() : isExtended32BitVector();
169     }
170 
171     /// Return true if this is a 64-bit vector type.
172     bool is64BitVector() const {
173       return isSimple() ? V.is64BitVector() : isExtended64BitVector();
174     }
175 
176     /// Return true if this is a 128-bit vector type.
177     bool is128BitVector() const {
178       return isSimple() ? V.is128BitVector() : isExtended128BitVector();
179     }
180 
181     /// Return true if this is a 256-bit vector type.
182     bool is256BitVector() const {
183       return isSimple() ? V.is256BitVector() : isExtended256BitVector();
184     }
185 
186     /// Return true if this is a 512-bit vector type.
187     bool is512BitVector() const {
188       return isSimple() ? V.is512BitVector() : isExtended512BitVector();
189     }
190 
191     /// Return true if this is a 1024-bit vector type.
192     bool is1024BitVector() const {
193       return isSimple() ? V.is1024BitVector() : isExtended1024BitVector();
194     }
195 
196     /// Return true if this is a 2048-bit vector type.
197     bool is2048BitVector() const {
198       return isSimple() ? V.is2048BitVector() : isExtended2048BitVector();
199     }
200 
201     /// Return true if this is an overloaded type for TableGen.
202     bool isOverloaded() const {
203       return (V==MVT::iAny || V==MVT::fAny || V==MVT::vAny || V==MVT::iPTRAny);
204     }
205 
206     /// Return true if the bit size is a multiple of 8.
207     bool isByteSized() const { return getSizeInBits().isKnownMultipleOf(8); }
208 
209     /// Return true if the size is a power-of-two number of bytes.
210     bool isRound() const {
211       if (isScalableVector())
212         return false;
213       unsigned BitSize = getSizeInBits();
214       return BitSize >= 8 && !(BitSize & (BitSize - 1));
215     }
216 
217     /// Return true if this has the same number of bits as VT.
218     bool bitsEq(EVT VT) const {
219       if (EVT::operator==(VT)) return true;
220       return getSizeInBits() == VT.getSizeInBits();
221     }
222 
223     /// Return true if we know at compile time this has more bits than VT.
224     bool knownBitsGT(EVT VT) const {
225       return TypeSize::isKnownGT(getSizeInBits(), VT.getSizeInBits());
226     }
227 
228     /// Return true if we know at compile time this has more than or the same
229     /// bits as VT.
230     bool knownBitsGE(EVT VT) const {
231       return TypeSize::isKnownGE(getSizeInBits(), VT.getSizeInBits());
232     }
233 
234     /// Return true if we know at compile time this has fewer bits than VT.
235     bool knownBitsLT(EVT VT) const {
236       return TypeSize::isKnownLT(getSizeInBits(), VT.getSizeInBits());
237     }
238 
239     /// Return true if we know at compile time this has fewer than or the same
240     /// bits as VT.
241     bool knownBitsLE(EVT VT) const {
242       return TypeSize::isKnownLE(getSizeInBits(), VT.getSizeInBits());
243     }
244 
245     /// Return true if this has more bits than VT.
246     bool bitsGT(EVT VT) const {
247       if (EVT::operator==(VT)) return false;
248       assert(isScalableVector() == VT.isScalableVector() &&
249              "Comparison between scalable and fixed types");
250       return knownBitsGT(VT);
251     }
252 
253     /// Return true if this has no less bits than VT.
254     bool bitsGE(EVT VT) const {
255       if (EVT::operator==(VT)) return true;
256       assert(isScalableVector() == VT.isScalableVector() &&
257              "Comparison between scalable and fixed types");
258       return knownBitsGE(VT);
259     }
260 
261     /// Return true if this has less bits than VT.
262     bool bitsLT(EVT VT) const {
263       if (EVT::operator==(VT)) return false;
264       assert(isScalableVector() == VT.isScalableVector() &&
265              "Comparison between scalable and fixed types");
266       return knownBitsLT(VT);
267     }
268 
269     /// Return true if this has no more bits than VT.
270     bool bitsLE(EVT VT) const {
271       if (EVT::operator==(VT)) return true;
272       assert(isScalableVector() == VT.isScalableVector() &&
273              "Comparison between scalable and fixed types");
274       return knownBitsLE(VT);
275     }
276 
277     /// Return the SimpleValueType held in the specified simple EVT.
278     MVT getSimpleVT() const {
279       assert(isSimple() && "Expected a SimpleValueType!");
280       return V;
281     }
282 
283     /// If this is a vector type, return the element type, otherwise return
284     /// this.
285     EVT getScalarType() const {
286       return isVector() ? getVectorElementType() : *this;
287     }
288 
289     /// Given a vector type, return the type of each element.
290     EVT getVectorElementType() const {
291       assert(isVector() && "Invalid vector type!");
292       if (isSimple())
293         return V.getVectorElementType();
294       return getExtendedVectorElementType();
295     }
296 
297     /// Given a vector type, return the number of elements it contains.
298     unsigned getVectorNumElements() const {
299 #ifdef STRICT_FIXED_SIZE_VECTORS
300       assert(isFixedLengthVector() && "Invalid vector type!");
301 #else
302       assert(isVector() && "Invalid vector type!");
303       if (isScalableVector())
304         WithColor::warning()
305             << "Possible incorrect use of EVT::getVectorNumElements() for "
306                "scalable vector. Scalable flag may be dropped, use "
307                "EVT::getVectorElementCount() instead\n";
308 #endif
309       if (isSimple())
310         return V.getVectorNumElements();
311       return getExtendedVectorNumElements();
312     }
313 
314     // Given a (possibly scalable) vector type, return the ElementCount
315     ElementCount getVectorElementCount() const {
316       assert((isVector()) && "Invalid vector type!");
317       if (isSimple())
318         return V.getVectorElementCount();
319 
320       return getExtendedVectorElementCount();
321     }
322 
323     /// Given a vector type, return the minimum number of elements it contains.
324     unsigned getVectorMinNumElements() const {
325       return getVectorElementCount().getKnownMinValue();
326     }
327 
328     /// Return the size of the specified value type in bits.
329     ///
330     /// If the value type is a scalable vector type, the scalable property will
331     /// be set and the runtime size will be a positive integer multiple of the
332     /// base size.
333     TypeSize getSizeInBits() const {
334       if (isSimple())
335         return V.getSizeInBits();
336       return getExtendedSizeInBits();
337     }
338 
339     /// Return the size of the specified fixed width value type in bits. The
340     /// function will assert if the type is scalable.
341     uint64_t getFixedSizeInBits() const {
342       return getSizeInBits().getFixedSize();
343     }
344 
345     uint64_t getScalarSizeInBits() const {
346       return getScalarType().getSizeInBits().getFixedSize();
347     }
348 
349     /// Return the number of bytes overwritten by a store of the specified value
350     /// type.
351     ///
352     /// If the value type is a scalable vector type, the scalable property will
353     /// be set and the runtime size will be a positive integer multiple of the
354     /// base size.
355     TypeSize getStoreSize() const {
356       TypeSize BaseSize = getSizeInBits();
357       return {(BaseSize.getKnownMinSize() + 7) / 8, BaseSize.isScalable()};
358     }
359 
360     /// Return the number of bits overwritten by a store of the specified value
361     /// type.
362     ///
363     /// If the value type is a scalable vector type, the scalable property will
364     /// be set and the runtime size will be a positive integer multiple of the
365     /// base size.
366     TypeSize getStoreSizeInBits() const {
367       return getStoreSize() * 8;
368     }
369 
370     /// Rounds the bit-width of the given integer EVT up to the nearest power of
371     /// two (and at least to eight), and returns the integer EVT with that
372     /// number of bits.
373     EVT getRoundIntegerType(LLVMContext &Context) const {
374       assert(isInteger() && !isVector() && "Invalid integer type!");
375       unsigned BitWidth = getSizeInBits();
376       if (BitWidth <= 8)
377         return EVT(MVT::i8);
378       return getIntegerVT(Context, 1 << Log2_32_Ceil(BitWidth));
379     }
380 
381     /// Finds the smallest simple value type that is greater than or equal to
382     /// half the width of this EVT. If no simple value type can be found, an
383     /// extended integer value type of half the size (rounded up) is returned.
384     EVT getHalfSizedIntegerVT(LLVMContext &Context) const {
385       assert(isInteger() && !isVector() && "Invalid integer type!");
386       unsigned EVTSize = getSizeInBits();
387       for (unsigned IntVT = MVT::FIRST_INTEGER_VALUETYPE;
388           IntVT <= MVT::LAST_INTEGER_VALUETYPE; ++IntVT) {
389         EVT HalfVT = EVT((MVT::SimpleValueType)IntVT);
390         if (HalfVT.getSizeInBits() * 2 >= EVTSize)
391           return HalfVT;
392       }
393       return getIntegerVT(Context, (EVTSize + 1) / 2);
394     }
395 
396     /// Return a VT for an integer vector type with the size of the
397     /// elements doubled. The typed returned may be an extended type.
398     EVT widenIntegerVectorElementType(LLVMContext &Context) const {
399       EVT EltVT = getVectorElementType();
400       EltVT = EVT::getIntegerVT(Context, 2 * EltVT.getSizeInBits());
401       return EVT::getVectorVT(Context, EltVT, getVectorElementCount());
402     }
403 
404     // Return a VT for a vector type with the same element type but
405     // half the number of elements. The type returned may be an
406     // extended type.
407     EVT getHalfNumVectorElementsVT(LLVMContext &Context) const {
408       EVT EltVT = getVectorElementType();
409       auto EltCnt = getVectorElementCount();
410       assert(EltCnt.isKnownEven() && "Splitting vector, but not in half!");
411       return EVT::getVectorVT(Context, EltVT, EltCnt.divideCoefficientBy(2));
412     }
413 
414     // Return a VT for a vector type with the same element type but
415     // double the number of elements. The type returned may be an
416     // extended type.
417     EVT getDoubleNumVectorElementsVT(LLVMContext &Context) const {
418       EVT EltVT = getVectorElementType();
419       auto EltCnt = getVectorElementCount();
420       return EVT::getVectorVT(Context, EltVT, EltCnt * 2);
421     }
422 
423     /// Returns true if the given vector is a power of 2.
424     bool isPow2VectorType() const {
425       unsigned NElts = getVectorMinNumElements();
426       return !(NElts & (NElts - 1));
427     }
428 
429     /// Widens the length of the given vector EVT up to the nearest power of 2
430     /// and returns that type.
431     EVT getPow2VectorType(LLVMContext &Context) const {
432       if (!isPow2VectorType()) {
433         ElementCount NElts = getVectorElementCount();
434         unsigned NewMinCount = 1 << Log2_32_Ceil(NElts.getKnownMinValue());
435         NElts = ElementCount::get(NewMinCount, NElts.isScalable());
436         return EVT::getVectorVT(Context, getVectorElementType(), NElts);
437       }
438       else {
439         return *this;
440       }
441     }
442 
443     /// This function returns value type as a string, e.g. "i32".
444     std::string getEVTString() const;
445 
446     /// This method returns an LLVM type corresponding to the specified EVT.
447     /// For integer types, this returns an unsigned type. Note that this will
448     /// abort for types that cannot be represented.
449     Type *getTypeForEVT(LLVMContext &Context) const;
450 
451     /// Return the value type corresponding to the specified type.
452     /// This returns all pointers as iPTR.  If HandleUnknown is true, unknown
453     /// types are returned as Other, otherwise they are invalid.
454     static EVT getEVT(Type *Ty, bool HandleUnknown = false);
455 
456     intptr_t getRawBits() const {
457       if (isSimple())
458         return V.SimpleTy;
459       else
460         return (intptr_t)(LLVMTy);
461     }
462 
463     /// A meaningless but well-behaved order, useful for constructing
464     /// containers.
465     struct compareRawBits {
466       bool operator()(EVT L, EVT R) const {
467         if (L.V.SimpleTy == R.V.SimpleTy)
468           return L.LLVMTy < R.LLVMTy;
469         else
470           return L.V.SimpleTy < R.V.SimpleTy;
471       }
472     };
473 
474   private:
475     // Methods for handling the Extended-type case in functions above.
476     // These are all out-of-line to prevent users of this header file
477     // from having a dependency on Type.h.
478     EVT changeExtendedTypeToInteger() const;
479     EVT changeExtendedVectorElementType(EVT EltVT) const;
480     EVT changeExtendedVectorElementTypeToInteger() const;
481     static EVT getExtendedIntegerVT(LLVMContext &C, unsigned BitWidth);
482     static EVT getExtendedVectorVT(LLVMContext &C, EVT VT, unsigned NumElements,
483                                    bool IsScalable);
484     static EVT getExtendedVectorVT(LLVMContext &Context, EVT VT,
485                                    ElementCount EC);
486     bool isExtendedFloatingPoint() const LLVM_READONLY;
487     bool isExtendedInteger() const LLVM_READONLY;
488     bool isExtendedScalarInteger() const LLVM_READONLY;
489     bool isExtendedVector() const LLVM_READONLY;
490     bool isExtended16BitVector() const LLVM_READONLY;
491     bool isExtended32BitVector() const LLVM_READONLY;
492     bool isExtended64BitVector() const LLVM_READONLY;
493     bool isExtended128BitVector() const LLVM_READONLY;
494     bool isExtended256BitVector() const LLVM_READONLY;
495     bool isExtended512BitVector() const LLVM_READONLY;
496     bool isExtended1024BitVector() const LLVM_READONLY;
497     bool isExtended2048BitVector() const LLVM_READONLY;
498     bool isExtendedFixedLengthVector() const LLVM_READONLY;
499     bool isExtendedScalableVector() const LLVM_READONLY;
500     EVT getExtendedVectorElementType() const;
501     unsigned getExtendedVectorNumElements() const LLVM_READONLY;
502     ElementCount getExtendedVectorElementCount() const LLVM_READONLY;
503     TypeSize getExtendedSizeInBits() const LLVM_READONLY;
504   };
505 
506 } // end namespace llvm
507 
508 #endif // LLVM_CODEGEN_VALUETYPES_H
509