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