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