1 //===- CodeGen/MachineValueType.h - Machine-Level 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 machine-level target independent types which
10 // legal values in the code generator use.
11 //
12 // Constants and properties are defined in ValueTypes.td.
13 //
14 //===----------------------------------------------------------------------===//
15 
16 #ifndef LLVM_CODEGEN_MACHINEVALUETYPE_H
17 #define LLVM_CODEGEN_MACHINEVALUETYPE_H
18 
19 #include "llvm/ADT/Sequence.h"
20 #include "llvm/Support/ErrorHandling.h"
21 #include "llvm/Support/MathExtras.h"
22 #include "llvm/Support/TypeSize.h"
23 #include <cassert>
24 #include <cstdint>
25 
26 namespace llvm {
27 
28   class Type;
29   class raw_ostream;
30 
31   /// Machine Value Type. Every type that is supported natively by some
32   /// processor targeted by LLVM occurs here. This means that any legal value
33   /// type can be represented by an MVT.
34   class MVT {
35   public:
36     enum SimpleValueType : uint8_t {
37       // Simple value types that aren't explicitly part of this enumeration
38       // are considered extended value types.
39       INVALID_SIMPLE_VALUE_TYPE = 0,
40 
41 #define GET_VT_ATTR(Ty, n, sz, Any, Int, FP, Vec, Sc) Ty = n,
42 #define GET_VT_RANGES
43 #include "llvm/CodeGen/GenVT.inc"
44 #undef GET_VT_ATTR
45 #undef GET_VT_RANGES
46 
47       VALUETYPE_SIZE = LAST_VALUETYPE + 1,
48 
49       // This is the current maximum for LAST_VALUETYPE.
50       // MVT::MAX_ALLOWED_VALUETYPE is used for asserts and to size bit vectors
51       // This value must be a multiple of 32.
52       MAX_ALLOWED_VALUETYPE = 224,
53     };
54 
55     static_assert(FIRST_VALUETYPE > 0);
56     static_assert(LAST_VALUETYPE < MAX_ALLOWED_VALUETYPE);
57 
58     SimpleValueType SimpleTy = INVALID_SIMPLE_VALUE_TYPE;
59 
60     constexpr MVT() = default;
61     constexpr MVT(SimpleValueType SVT) : SimpleTy(SVT) {}
62 
63     bool operator>(const MVT& S)  const { return SimpleTy >  S.SimpleTy; }
64     bool operator<(const MVT& S)  const { return SimpleTy <  S.SimpleTy; }
65     bool operator==(const MVT& S) const { return SimpleTy == S.SimpleTy; }
66     bool operator!=(const MVT& S) const { return SimpleTy != S.SimpleTy; }
67     bool operator>=(const MVT& S) const { return SimpleTy >= S.SimpleTy; }
68     bool operator<=(const MVT& S) const { return SimpleTy <= S.SimpleTy; }
69 
70     /// Support for debugging, callable in GDB: VT.dump()
71     void dump() const;
72 
73     /// Implement operator<<.
74     void print(raw_ostream &OS) const;
75 
76     /// Return true if this is a valid simple valuetype.
77     bool isValid() const {
78       return (SimpleTy >= MVT::FIRST_VALUETYPE &&
79               SimpleTy <= MVT::LAST_VALUETYPE);
80     }
81 
82     /// Return true if this is a FP or a vector FP type.
83     bool isFloatingPoint() const {
84       return ((SimpleTy >= MVT::FIRST_FP_VALUETYPE &&
85                SimpleTy <= MVT::LAST_FP_VALUETYPE) ||
86               (SimpleTy >= MVT::FIRST_FP_FIXEDLEN_VECTOR_VALUETYPE &&
87                SimpleTy <= MVT::LAST_FP_FIXEDLEN_VECTOR_VALUETYPE) ||
88               (SimpleTy >= MVT::FIRST_FP_SCALABLE_VECTOR_VALUETYPE &&
89                SimpleTy <= MVT::LAST_FP_SCALABLE_VECTOR_VALUETYPE));
90     }
91 
92     /// Return true if this is an integer or a vector integer type.
93     bool isInteger() const {
94       return ((SimpleTy >= MVT::FIRST_INTEGER_VALUETYPE &&
95                SimpleTy <= MVT::LAST_INTEGER_VALUETYPE) ||
96               (SimpleTy >= MVT::FIRST_INTEGER_FIXEDLEN_VECTOR_VALUETYPE &&
97                SimpleTy <= MVT::LAST_INTEGER_FIXEDLEN_VECTOR_VALUETYPE) ||
98               (SimpleTy >= MVT::FIRST_INTEGER_SCALABLE_VECTOR_VALUETYPE &&
99                SimpleTy <= MVT::LAST_INTEGER_SCALABLE_VECTOR_VALUETYPE));
100     }
101 
102     /// Return true if this is an integer, not including vectors.
103     bool isScalarInteger() const {
104       return (SimpleTy >= MVT::FIRST_INTEGER_VALUETYPE &&
105               SimpleTy <= MVT::LAST_INTEGER_VALUETYPE);
106     }
107 
108     /// Return true if this is a vector value type.
109     bool isVector() const {
110       return (SimpleTy >= MVT::FIRST_VECTOR_VALUETYPE &&
111               SimpleTy <= MVT::LAST_VECTOR_VALUETYPE);
112     }
113 
114     /// Return true if this is a vector value type where the
115     /// runtime length is machine dependent
116     bool isScalableVector() const {
117       return (SimpleTy >= MVT::FIRST_SCALABLE_VECTOR_VALUETYPE &&
118               SimpleTy <= MVT::LAST_SCALABLE_VECTOR_VALUETYPE);
119     }
120 
121     /// Return true if this is a custom target type that has a scalable size.
122     bool isScalableTargetExtVT() const {
123       return SimpleTy == MVT::aarch64svcount;
124     }
125 
126     /// Return true if the type is a scalable type.
127     bool isScalableVT() const {
128       return isScalableVector() || isScalableTargetExtVT();
129     }
130 
131     bool isFixedLengthVector() const {
132       return (SimpleTy >= MVT::FIRST_FIXEDLEN_VECTOR_VALUETYPE &&
133               SimpleTy <= MVT::LAST_FIXEDLEN_VECTOR_VALUETYPE);
134     }
135 
136     /// Return true if this is a 16-bit vector type.
137     bool is16BitVector() const {
138       return (isFixedLengthVector() && getFixedSizeInBits() == 16);
139     }
140 
141     /// Return true if this is a 32-bit vector type.
142     bool is32BitVector() const {
143       return (isFixedLengthVector() && getFixedSizeInBits() == 32);
144     }
145 
146     /// Return true if this is a 64-bit vector type.
147     bool is64BitVector() const {
148       return (isFixedLengthVector() && getFixedSizeInBits() == 64);
149     }
150 
151     /// Return true if this is a 128-bit vector type.
152     bool is128BitVector() const {
153       return (isFixedLengthVector() && getFixedSizeInBits() == 128);
154     }
155 
156     /// Return true if this is a 256-bit vector type.
157     bool is256BitVector() const {
158       return (isFixedLengthVector() && getFixedSizeInBits() == 256);
159     }
160 
161     /// Return true if this is a 512-bit vector type.
162     bool is512BitVector() const {
163       return (isFixedLengthVector() && getFixedSizeInBits() == 512);
164     }
165 
166     /// Return true if this is a 1024-bit vector type.
167     bool is1024BitVector() const {
168       return (isFixedLengthVector() && getFixedSizeInBits() == 1024);
169     }
170 
171     /// Return true if this is a 2048-bit vector type.
172     bool is2048BitVector() const {
173       return (isFixedLengthVector() && getFixedSizeInBits() == 2048);
174     }
175 
176     /// Return true if this is an overloaded type for TableGen.
177     bool isOverloaded() const {
178       switch (SimpleTy) {
179 #define GET_VT_ATTR(Ty, n, sz, Any, Int, FP, Vec, Sc)                          \
180   case Ty:                                                                     \
181     return Any;
182 #include "llvm/CodeGen/GenVT.inc"
183 #undef GET_VT_ATTR
184       default:
185         return false;
186       }
187     }
188 
189     /// Return a vector with the same number of elements as this vector, but
190     /// with the element type converted to an integer type with the same
191     /// bitwidth.
192     MVT changeVectorElementTypeToInteger() const {
193       MVT EltTy = getVectorElementType();
194       MVT IntTy = MVT::getIntegerVT(EltTy.getSizeInBits());
195       MVT VecTy = MVT::getVectorVT(IntTy, getVectorElementCount());
196       assert(VecTy.SimpleTy != MVT::INVALID_SIMPLE_VALUE_TYPE &&
197              "Simple vector VT not representable by simple integer vector VT!");
198       return VecTy;
199     }
200 
201     /// Return a VT for a vector type whose attributes match ourselves
202     /// with the exception of the element type that is chosen by the caller.
203     MVT changeVectorElementType(MVT EltVT) const {
204       MVT VecTy = MVT::getVectorVT(EltVT, getVectorElementCount());
205       assert(VecTy.SimpleTy != MVT::INVALID_SIMPLE_VALUE_TYPE &&
206              "Simple vector VT not representable by simple integer vector VT!");
207       return VecTy;
208     }
209 
210     /// Return the type converted to an equivalently sized integer or vector
211     /// with integer element type. Similar to changeVectorElementTypeToInteger,
212     /// but also handles scalars.
213     MVT changeTypeToInteger() {
214       if (isVector())
215         return changeVectorElementTypeToInteger();
216       return MVT::getIntegerVT(getSizeInBits());
217     }
218 
219     /// Return a VT for a vector type with the same element type but
220     /// half the number of elements.
221     MVT getHalfNumVectorElementsVT() const {
222       MVT EltVT = getVectorElementType();
223       auto EltCnt = getVectorElementCount();
224       assert(EltCnt.isKnownEven() && "Splitting vector, but not in half!");
225       return getVectorVT(EltVT, EltCnt.divideCoefficientBy(2));
226     }
227 
228     // Return a VT for a vector type with the same element type but
229     // double the number of elements.
230     MVT getDoubleNumVectorElementsVT() const {
231       MVT EltVT = getVectorElementType();
232       auto EltCnt = getVectorElementCount();
233       return MVT::getVectorVT(EltVT, EltCnt * 2);
234     }
235 
236     /// Returns true if the given vector is a power of 2.
237     bool isPow2VectorType() const {
238       unsigned NElts = getVectorMinNumElements();
239       return !(NElts & (NElts - 1));
240     }
241 
242     /// Widens the length of the given vector MVT up to the nearest power of 2
243     /// and returns that type.
244     MVT getPow2VectorType() const {
245       if (isPow2VectorType())
246         return *this;
247 
248       ElementCount NElts = getVectorElementCount();
249       unsigned NewMinCount = 1 << Log2_32_Ceil(NElts.getKnownMinValue());
250       NElts = ElementCount::get(NewMinCount, NElts.isScalable());
251       return MVT::getVectorVT(getVectorElementType(), NElts);
252     }
253 
254     /// If this is a vector, return the element type, otherwise return this.
255     MVT getScalarType() const {
256       return isVector() ? getVectorElementType() : *this;
257     }
258 
259     MVT getVectorElementType() const {
260       switch (SimpleTy) {
261       default:
262         llvm_unreachable("Not a vector MVT!");
263 
264 #define GET_VT_VECATTR(Ty, Sc, nElem, ElTy, ElSz)                              \
265   case Ty:                                                                     \
266     return ElTy;
267 #include "llvm/CodeGen/GenVT.inc"
268 #undef GET_VT_VECATTR
269       }
270     }
271 
272     /// Given a vector type, return the minimum number of elements it contains.
273     unsigned getVectorMinNumElements() const {
274       switch (SimpleTy) {
275       default:
276         llvm_unreachable("Not a vector MVT!");
277 
278 #define GET_VT_VECATTR(Ty, Sc, nElem, ElTy, ElSz)                              \
279   case Ty:                                                                     \
280     return nElem;
281 #include "llvm/CodeGen/GenVT.inc"
282 #undef GET_VT_VECATTR
283       }
284     }
285 
286     ElementCount getVectorElementCount() const {
287       return ElementCount::get(getVectorMinNumElements(), isScalableVector());
288     }
289 
290     unsigned getVectorNumElements() const {
291       if (isScalableVector())
292         llvm::reportInvalidSizeRequest(
293             "Possible incorrect use of MVT::getVectorNumElements() for "
294             "scalable vector. Scalable flag may be dropped, use "
295             "MVT::getVectorElementCount() instead");
296       return getVectorMinNumElements();
297     }
298 
299     /// Returns the size of the specified MVT in bits.
300     ///
301     /// If the value type is a scalable vector type, the scalable property will
302     /// be set and the runtime size will be a positive integer multiple of the
303     /// base size.
304     TypeSize getSizeInBits() const {
305       switch (SimpleTy) {
306       default:
307         switch (SimpleTy) {
308         default:
309           llvm_unreachable("getSizeInBits called on extended MVT.");
310 
311 #define GET_VT_ATTR(Ty, N, Sz, Any, Int, FP, Vec, Sc)                          \
312   case Ty:                                                                     \
313     return (Sc ? TypeSize::Scalable(Sz) : TypeSize::Fixed(Sz));
314 #include "llvm/CodeGen/GenVT.inc"
315 #undef GET_VT_ATTR
316         }
317       case Other:
318         llvm_unreachable("Value type is non-standard value, Other.");
319       case iPTR:
320         llvm_unreachable("Value type size is target-dependent. Ask TLI.");
321       case iPTRAny:
322       case iAny:
323       case fAny:
324       case vAny:
325       case Any:
326         llvm_unreachable("Value type is overloaded.");
327       case token:
328         llvm_unreachable("Token type is a sentinel that cannot be used "
329                          "in codegen and has no size");
330       case Metadata:
331         llvm_unreachable("Value type is metadata.");
332       case aarch64svcount: // FIXME: Not in the td.
333         return TypeSize::Scalable(16);
334       }
335     }
336 
337     /// Return the size of the specified fixed width value type in bits. The
338     /// function will assert if the type is scalable.
339     uint64_t getFixedSizeInBits() const {
340       return getSizeInBits().getFixedValue();
341     }
342 
343     uint64_t getScalarSizeInBits() const {
344       return getScalarType().getSizeInBits().getFixedValue();
345     }
346 
347     /// Return the number of bytes overwritten by a store of the specified value
348     /// type.
349     ///
350     /// If the value type is a scalable vector type, the scalable property will
351     /// be set and the runtime size will be a positive integer multiple of the
352     /// base size.
353     TypeSize getStoreSize() const {
354       TypeSize BaseSize = getSizeInBits();
355       return {(BaseSize.getKnownMinValue() + 7) / 8, BaseSize.isScalable()};
356     }
357 
358     // Return the number of bytes overwritten by a store of this value type or
359     // this value type's element type in the case of a vector.
360     uint64_t getScalarStoreSize() const {
361       return getScalarType().getStoreSize().getFixedValue();
362     }
363 
364     /// Return the number of bits overwritten by a store of the specified value
365     /// type.
366     ///
367     /// If the value type is a scalable vector type, the scalable property will
368     /// be set and the runtime size will be a positive integer multiple of the
369     /// base size.
370     TypeSize getStoreSizeInBits() const {
371       return getStoreSize() * 8;
372     }
373 
374     /// Returns true if the number of bits for the type is a multiple of an
375     /// 8-bit byte.
376     bool isByteSized() const { return getSizeInBits().isKnownMultipleOf(8); }
377 
378     /// Return true if we know at compile time this has more bits than VT.
379     bool knownBitsGT(MVT VT) const {
380       return TypeSize::isKnownGT(getSizeInBits(), VT.getSizeInBits());
381     }
382 
383     /// Return true if we know at compile time this has more than or the same
384     /// bits as VT.
385     bool knownBitsGE(MVT VT) const {
386       return TypeSize::isKnownGE(getSizeInBits(), VT.getSizeInBits());
387     }
388 
389     /// Return true if we know at compile time this has fewer bits than VT.
390     bool knownBitsLT(MVT VT) const {
391       return TypeSize::isKnownLT(getSizeInBits(), VT.getSizeInBits());
392     }
393 
394     /// Return true if we know at compile time this has fewer than or the same
395     /// bits as VT.
396     bool knownBitsLE(MVT VT) const {
397       return TypeSize::isKnownLE(getSizeInBits(), VT.getSizeInBits());
398     }
399 
400     /// Return true if this has more bits than VT.
401     bool bitsGT(MVT VT) const {
402       assert(isScalableVector() == VT.isScalableVector() &&
403              "Comparison between scalable and fixed types");
404       return knownBitsGT(VT);
405     }
406 
407     /// Return true if this has no less bits than VT.
408     bool bitsGE(MVT VT) const {
409       assert(isScalableVector() == VT.isScalableVector() &&
410              "Comparison between scalable and fixed types");
411       return knownBitsGE(VT);
412     }
413 
414     /// Return true if this has less bits than VT.
415     bool bitsLT(MVT VT) const {
416       assert(isScalableVector() == VT.isScalableVector() &&
417              "Comparison between scalable and fixed types");
418       return knownBitsLT(VT);
419     }
420 
421     /// Return true if this has no more bits than VT.
422     bool bitsLE(MVT VT) const {
423       assert(isScalableVector() == VT.isScalableVector() &&
424              "Comparison between scalable and fixed types");
425       return knownBitsLE(VT);
426     }
427 
428     static MVT getFloatingPointVT(unsigned BitWidth) {
429 #define GET_VT_ATTR(Ty, n, sz, Any, Int, FP, Vec, Sc)                          \
430   if (FP == 3 && sz == BitWidth)                                               \
431     return Ty;
432 #include "llvm/CodeGen/GenVT.inc"
433 #undef GET_VT_ATTR
434 
435       llvm_unreachable("Bad bit width!");
436     }
437 
438     static MVT getIntegerVT(unsigned BitWidth) {
439 #define GET_VT_ATTR(Ty, n, sz, Any, Int, FP, Vec, Sc)                          \
440   if (Int == 3 && sz == BitWidth)                                              \
441     return Ty;
442 #include "llvm/CodeGen/GenVT.inc"
443 #undef GET_VT_ATTR
444 
445       return (MVT::SimpleValueType)(MVT::INVALID_SIMPLE_VALUE_TYPE);
446     }
447 
448     static MVT getVectorVT(MVT VT, unsigned NumElements) {
449 #define GET_VT_VECATTR(Ty, Sc, nElem, ElTy, ElSz)                              \
450   if (!Sc && VT.SimpleTy == ElTy && NumElements == nElem)                      \
451     return Ty;
452 #include "llvm/CodeGen/GenVT.inc"
453 #undef GET_VT_VECATTR
454 
455       return (MVT::SimpleValueType)(MVT::INVALID_SIMPLE_VALUE_TYPE);
456     }
457 
458     static MVT getScalableVectorVT(MVT VT, unsigned NumElements) {
459 #define GET_VT_VECATTR(Ty, Sc, nElem, ElTy, ElSz)                              \
460   if (Sc && VT.SimpleTy == ElTy && NumElements == nElem)                       \
461     return Ty;
462 #include "llvm/CodeGen/GenVT.inc"
463 #undef GET_VT_VECATTR
464 
465       return (MVT::SimpleValueType)(MVT::INVALID_SIMPLE_VALUE_TYPE);
466     }
467 
468     static MVT getVectorVT(MVT VT, unsigned NumElements, bool IsScalable) {
469       if (IsScalable)
470         return getScalableVectorVT(VT, NumElements);
471       return getVectorVT(VT, NumElements);
472     }
473 
474     static MVT getVectorVT(MVT VT, ElementCount EC) {
475       if (EC.isScalable())
476         return getScalableVectorVT(VT, EC.getKnownMinValue());
477       return getVectorVT(VT, EC.getKnownMinValue());
478     }
479 
480     /// Return the value type corresponding to the specified type.  This returns
481     /// all pointers as iPTR.  If HandleUnknown is true, unknown types are
482     /// returned as Other, otherwise they are invalid.
483     static MVT getVT(Type *Ty, bool HandleUnknown = false);
484 
485   public:
486     /// SimpleValueType Iteration
487     /// @{
488     static auto all_valuetypes() {
489       return enum_seq_inclusive(MVT::FIRST_VALUETYPE, MVT::LAST_VALUETYPE,
490                                 force_iteration_on_noniterable_enum);
491     }
492 
493     static auto integer_valuetypes() {
494       return enum_seq_inclusive(MVT::FIRST_INTEGER_VALUETYPE,
495                                 MVT::LAST_INTEGER_VALUETYPE,
496                                 force_iteration_on_noniterable_enum);
497     }
498 
499     static auto fp_valuetypes() {
500       return enum_seq_inclusive(MVT::FIRST_FP_VALUETYPE, MVT::LAST_FP_VALUETYPE,
501                                 force_iteration_on_noniterable_enum);
502     }
503 
504     static auto vector_valuetypes() {
505       return enum_seq_inclusive(MVT::FIRST_VECTOR_VALUETYPE,
506                                 MVT::LAST_VECTOR_VALUETYPE,
507                                 force_iteration_on_noniterable_enum);
508     }
509 
510     static auto fixedlen_vector_valuetypes() {
511       return enum_seq_inclusive(MVT::FIRST_FIXEDLEN_VECTOR_VALUETYPE,
512                                 MVT::LAST_FIXEDLEN_VECTOR_VALUETYPE,
513                                 force_iteration_on_noniterable_enum);
514     }
515 
516     static auto scalable_vector_valuetypes() {
517       return enum_seq_inclusive(MVT::FIRST_SCALABLE_VECTOR_VALUETYPE,
518                                 MVT::LAST_SCALABLE_VECTOR_VALUETYPE,
519                                 force_iteration_on_noniterable_enum);
520     }
521 
522     static auto integer_fixedlen_vector_valuetypes() {
523       return enum_seq_inclusive(MVT::FIRST_INTEGER_FIXEDLEN_VECTOR_VALUETYPE,
524                                 MVT::LAST_INTEGER_FIXEDLEN_VECTOR_VALUETYPE,
525                                 force_iteration_on_noniterable_enum);
526     }
527 
528     static auto fp_fixedlen_vector_valuetypes() {
529       return enum_seq_inclusive(MVT::FIRST_FP_FIXEDLEN_VECTOR_VALUETYPE,
530                                 MVT::LAST_FP_FIXEDLEN_VECTOR_VALUETYPE,
531                                 force_iteration_on_noniterable_enum);
532     }
533 
534     static auto integer_scalable_vector_valuetypes() {
535       return enum_seq_inclusive(MVT::FIRST_INTEGER_SCALABLE_VECTOR_VALUETYPE,
536                                 MVT::LAST_INTEGER_SCALABLE_VECTOR_VALUETYPE,
537                                 force_iteration_on_noniterable_enum);
538     }
539 
540     static auto fp_scalable_vector_valuetypes() {
541       return enum_seq_inclusive(MVT::FIRST_FP_SCALABLE_VECTOR_VALUETYPE,
542                                 MVT::LAST_FP_SCALABLE_VECTOR_VALUETYPE,
543                                 force_iteration_on_noniterable_enum);
544     }
545     /// @}
546   };
547 
548   inline raw_ostream &operator<<(raw_ostream &OS, const MVT &VT) {
549     VT.print(OS);
550     return OS;
551   }
552 
553 } // end namespace llvm
554 
555 #endif // LLVM_CODEGEN_MACHINEVALUETYPE_H
556