1 //===----------- ValueTypes.cpp - Implementation of EVT methods -----------===//
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
7 //===----------------------------------------------------------------------===//
9 #include "llvm/CodeGen/ValueTypes.h"
10 #include "llvm/ADT/StringExtras.h"
11 #include "llvm/IR/DerivedTypes.h"
12 #include "llvm/IR/Type.h"
13 #include "llvm/Support/ErrorHandling.h"
14 #include "llvm/Support/TypeSize.h"
17 EVT
EVT::changeExtendedTypeToInteger() const {
18 assert(isExtended() && "Type is not extended!");
19 LLVMContext
&Context
= LLVMTy
->getContext();
20 return getIntegerVT(Context
, getSizeInBits());
23 EVT
EVT::changeExtendedVectorElementTypeToInteger() const {
24 assert(isExtended() && "Type is not extended!");
25 LLVMContext
&Context
= LLVMTy
->getContext();
26 EVT IntTy
= getIntegerVT(Context
, getScalarSizeInBits());
27 return getVectorVT(Context
, IntTy
, getVectorElementCount());
30 EVT
EVT::changeExtendedVectorElementType(EVT EltVT
) const {
31 assert(isExtended() && "Type is not extended!");
32 LLVMContext
&Context
= LLVMTy
->getContext();
33 return getVectorVT(Context
, EltVT
, getVectorElementCount());
36 EVT
EVT::getExtendedIntegerVT(LLVMContext
&Context
, unsigned BitWidth
) {
38 VT
.LLVMTy
= IntegerType::get(Context
, BitWidth
);
39 assert(VT
.isExtended() && "Type is not extended!");
43 EVT
EVT::getExtendedVectorVT(LLVMContext
&Context
, EVT VT
, unsigned NumElements
,
47 VectorType::get(VT
.getTypeForEVT(Context
), NumElements
, IsScalable
);
48 assert(ResultVT
.isExtended() && "Type is not extended!");
52 EVT
EVT::getExtendedVectorVT(LLVMContext
&Context
, EVT VT
, ElementCount EC
) {
54 ResultVT
.LLVMTy
= VectorType::get(VT
.getTypeForEVT(Context
), EC
);
55 assert(ResultVT
.isExtended() && "Type is not extended!");
59 bool EVT::isExtendedFloatingPoint() const {
60 assert(isExtended() && "Type is not extended!");
61 return LLVMTy
->isFPOrFPVectorTy();
64 bool EVT::isExtendedInteger() const {
65 assert(isExtended() && "Type is not extended!");
66 return LLVMTy
->isIntOrIntVectorTy();
69 bool EVT::isExtendedScalarInteger() const {
70 assert(isExtended() && "Type is not extended!");
71 return LLVMTy
->isIntegerTy();
74 bool EVT::isExtendedVector() const {
75 assert(isExtended() && "Type is not extended!");
76 return LLVMTy
->isVectorTy();
79 bool EVT::isExtended16BitVector() const {
80 return isExtendedVector() && getExtendedSizeInBits() == 16;
83 bool EVT::isExtended32BitVector() const {
84 return isExtendedVector() && getExtendedSizeInBits() == 32;
87 bool EVT::isExtended64BitVector() const {
88 return isExtendedVector() && getExtendedSizeInBits() == 64;
91 bool EVT::isExtended128BitVector() const {
92 return isExtendedVector() && getExtendedSizeInBits() == 128;
95 bool EVT::isExtended256BitVector() const {
96 return isExtendedVector() && getExtendedSizeInBits() == 256;
99 bool EVT::isExtended512BitVector() const {
100 return isExtendedVector() && getExtendedSizeInBits() == 512;
103 bool EVT::isExtended1024BitVector() const {
104 return isExtendedVector() && getExtendedSizeInBits() == 1024;
107 bool EVT::isExtended2048BitVector() const {
108 return isExtendedVector() && getExtendedSizeInBits() == 2048;
111 bool EVT::isExtendedFixedLengthVector() const {
112 return isExtendedVector() && isa
<FixedVectorType
>(LLVMTy
);
115 bool EVT::isExtendedScalableVector() const {
116 return isExtendedVector() && isa
<ScalableVectorType
>(LLVMTy
);
119 EVT
EVT::getExtendedVectorElementType() const {
120 assert(isExtended() && "Type is not extended!");
121 return EVT::getEVT(cast
<VectorType
>(LLVMTy
)->getElementType());
124 unsigned EVT::getExtendedVectorNumElements() const {
125 assert(isExtended() && "Type is not extended!");
126 ElementCount EC
= cast
<VectorType
>(LLVMTy
)->getElementCount();
127 if (EC
.isScalable()) {
129 << "The code that requested the fixed number of elements has made the "
130 "assumption that this vector is not scalable. This assumption was "
131 "not correct, and this may lead to broken code\n";
133 return EC
.getKnownMinValue();
136 ElementCount
EVT::getExtendedVectorElementCount() const {
137 assert(isExtended() && "Type is not extended!");
138 return cast
<VectorType
>(LLVMTy
)->getElementCount();
141 TypeSize
EVT::getExtendedSizeInBits() const {
142 assert(isExtended() && "Type is not extended!");
143 if (IntegerType
*ITy
= dyn_cast
<IntegerType
>(LLVMTy
))
144 return TypeSize::Fixed(ITy
->getBitWidth());
145 if (VectorType
*VTy
= dyn_cast
<VectorType
>(LLVMTy
))
146 return VTy
->getPrimitiveSizeInBits();
147 llvm_unreachable("Unrecognized extended type!");
150 /// getEVTString - This function returns value type as a string, e.g. "i32".
151 std::string
EVT::getEVTString() const {
152 switch (V
.SimpleTy
) {
155 return (isScalableVector() ? "nxv" : "v") +
156 utostr(getVectorElementCount().getKnownMinValue()) +
157 getVectorElementType().getEVTString();
159 return "i" + utostr(getSizeInBits());
160 if (isFloatingPoint())
161 return "f" + utostr(getSizeInBits());
162 llvm_unreachable("Invalid EVT!");
163 case MVT::bf16
: return "bf16";
164 case MVT::ppcf128
: return "ppcf128";
165 case MVT::isVoid
: return "isVoid";
166 case MVT::Other
: return "ch";
167 case MVT::Glue
: return "glue";
168 case MVT::x86mmx
: return "x86mmx";
169 case MVT::x86amx
: return "x86amx";
170 case MVT::i64x8
: return "i64x8";
171 case MVT::Metadata
: return "Metadata";
172 case MVT::Untyped
: return "Untyped";
173 case MVT::funcref
: return "funcref";
174 case MVT::externref
: return "externref";
178 /// getTypeForEVT - This method returns an LLVM type corresponding to the
179 /// specified EVT. For integer types, this returns an unsigned type. Note
180 /// that this will abort for types that cannot be represented.
181 Type
*EVT::getTypeForEVT(LLVMContext
&Context
) const {
182 switch (V
.SimpleTy
) {
184 assert(isExtended() && "Type is not extended!");
186 case MVT::isVoid
: return Type::getVoidTy(Context
);
187 case MVT::i1
: return Type::getInt1Ty(Context
);
188 case MVT::i8
: return Type::getInt8Ty(Context
);
189 case MVT::i16
: return Type::getInt16Ty(Context
);
190 case MVT::i32
: return Type::getInt32Ty(Context
);
191 case MVT::i64
: return Type::getInt64Ty(Context
);
192 case MVT::i128
: return IntegerType::get(Context
, 128);
193 case MVT::f16
: return Type::getHalfTy(Context
);
194 case MVT::bf16
: return Type::getBFloatTy(Context
);
195 case MVT::f32
: return Type::getFloatTy(Context
);
196 case MVT::f64
: return Type::getDoubleTy(Context
);
197 case MVT::f80
: return Type::getX86_FP80Ty(Context
);
198 case MVT::f128
: return Type::getFP128Ty(Context
);
199 case MVT::ppcf128
: return Type::getPPC_FP128Ty(Context
);
200 case MVT::x86mmx
: return Type::getX86_MMXTy(Context
);
201 case MVT::x86amx
: return Type::getX86_AMXTy(Context
);
202 case MVT::i64x8
: return IntegerType::get(Context
, 512);
204 // pointer to opaque struct in addrspace(10)
205 return PointerType::get(StructType::create(Context
), 10);
207 // pointer to i8 addrspace(20)
208 return PointerType::get(Type::getInt8Ty(Context
), 20);
210 return FixedVectorType::get(Type::getInt1Ty(Context
), 1);
212 return FixedVectorType::get(Type::getInt1Ty(Context
), 2);
214 return FixedVectorType::get(Type::getInt1Ty(Context
), 4);
216 return FixedVectorType::get(Type::getInt1Ty(Context
), 8);
218 return FixedVectorType::get(Type::getInt1Ty(Context
), 16);
220 return FixedVectorType::get(Type::getInt1Ty(Context
), 32);
222 return FixedVectorType::get(Type::getInt1Ty(Context
), 64);
224 return FixedVectorType::get(Type::getInt1Ty(Context
), 128);
226 return FixedVectorType::get(Type::getInt1Ty(Context
), 256);
228 return FixedVectorType::get(Type::getInt1Ty(Context
), 512);
230 return FixedVectorType::get(Type::getInt1Ty(Context
), 1024);
232 return FixedVectorType::get(Type::getInt8Ty(Context
), 1);
234 return FixedVectorType::get(Type::getInt8Ty(Context
), 2);
236 return FixedVectorType::get(Type::getInt8Ty(Context
), 4);
238 return FixedVectorType::get(Type::getInt8Ty(Context
), 8);
240 return FixedVectorType::get(Type::getInt8Ty(Context
), 16);
242 return FixedVectorType::get(Type::getInt8Ty(Context
), 32);
244 return FixedVectorType::get(Type::getInt8Ty(Context
), 64);
246 return FixedVectorType::get(Type::getInt8Ty(Context
), 128);
248 return FixedVectorType::get(Type::getInt8Ty(Context
), 256);
250 return FixedVectorType::get(Type::getInt8Ty(Context
), 512);
252 return FixedVectorType::get(Type::getInt8Ty(Context
), 1024);
254 return FixedVectorType::get(Type::getInt16Ty(Context
), 1);
256 return FixedVectorType::get(Type::getInt16Ty(Context
), 2);
258 return FixedVectorType::get(Type::getInt16Ty(Context
), 3);
260 return FixedVectorType::get(Type::getInt16Ty(Context
), 4);
262 return FixedVectorType::get(Type::getInt16Ty(Context
), 8);
264 return FixedVectorType::get(Type::getInt16Ty(Context
), 16);
266 return FixedVectorType::get(Type::getInt16Ty(Context
), 32);
268 return FixedVectorType::get(Type::getInt16Ty(Context
), 64);
270 return FixedVectorType::get(Type::getInt16Ty(Context
), 128);
272 return FixedVectorType::get(Type::getInt16Ty(Context
), 256);
274 return FixedVectorType::get(Type::getInt16Ty(Context
), 512);
276 return FixedVectorType::get(Type::getInt32Ty(Context
), 1);
278 return FixedVectorType::get(Type::getInt32Ty(Context
), 2);
280 return FixedVectorType::get(Type::getInt32Ty(Context
), 3);
282 return FixedVectorType::get(Type::getInt32Ty(Context
), 4);
284 return FixedVectorType::get(Type::getInt32Ty(Context
), 5);
286 return FixedVectorType::get(Type::getInt32Ty(Context
), 6);
288 return FixedVectorType::get(Type::getInt32Ty(Context
), 7);
290 return FixedVectorType::get(Type::getInt32Ty(Context
), 8);
292 return FixedVectorType::get(Type::getInt32Ty(Context
), 16);
294 return FixedVectorType::get(Type::getInt32Ty(Context
), 32);
296 return FixedVectorType::get(Type::getInt32Ty(Context
), 64);
298 return FixedVectorType::get(Type::getInt32Ty(Context
), 128);
300 return FixedVectorType::get(Type::getInt32Ty(Context
), 256);
302 return FixedVectorType::get(Type::getInt32Ty(Context
), 512);
304 return FixedVectorType::get(Type::getInt32Ty(Context
), 1024);
306 return FixedVectorType::get(Type::getInt32Ty(Context
), 2048);
308 return FixedVectorType::get(Type::getInt64Ty(Context
), 1);
310 return FixedVectorType::get(Type::getInt64Ty(Context
), 2);
312 return FixedVectorType::get(Type::getInt64Ty(Context
), 3);
314 return FixedVectorType::get(Type::getInt64Ty(Context
), 4);
316 return FixedVectorType::get(Type::getInt64Ty(Context
), 8);
318 return FixedVectorType::get(Type::getInt64Ty(Context
), 16);
320 return FixedVectorType::get(Type::getInt64Ty(Context
), 32);
322 return FixedVectorType::get(Type::getInt64Ty(Context
), 64);
324 return FixedVectorType::get(Type::getInt64Ty(Context
), 128);
326 return FixedVectorType::get(Type::getInt64Ty(Context
), 256);
328 return FixedVectorType::get(Type::getInt128Ty(Context
), 1);
330 return FixedVectorType::get(Type::getHalfTy(Context
), 1);
332 return FixedVectorType::get(Type::getHalfTy(Context
), 2);
334 return FixedVectorType::get(Type::getHalfTy(Context
), 3);
336 return FixedVectorType::get(Type::getHalfTy(Context
), 4);
338 return FixedVectorType::get(Type::getHalfTy(Context
), 8);
340 return FixedVectorType::get(Type::getHalfTy(Context
), 16);
342 return FixedVectorType::get(Type::getHalfTy(Context
), 32);
344 return FixedVectorType::get(Type::getHalfTy(Context
), 64);
346 return FixedVectorType::get(Type::getHalfTy(Context
), 128);
348 return FixedVectorType::get(Type::getHalfTy(Context
), 256);
350 return FixedVectorType::get(Type::getHalfTy(Context
), 512);
352 return FixedVectorType::get(Type::getBFloatTy(Context
), 2);
354 return FixedVectorType::get(Type::getBFloatTy(Context
), 3);
356 return FixedVectorType::get(Type::getBFloatTy(Context
), 4);
358 return FixedVectorType::get(Type::getBFloatTy(Context
), 8);
360 return FixedVectorType::get(Type::getBFloatTy(Context
), 16);
362 return FixedVectorType::get(Type::getBFloatTy(Context
), 32);
364 return FixedVectorType::get(Type::getBFloatTy(Context
), 64);
366 return FixedVectorType::get(Type::getBFloatTy(Context
), 128);
368 return FixedVectorType::get(Type::getFloatTy(Context
), 1);
370 return FixedVectorType::get(Type::getFloatTy(Context
), 2);
372 return FixedVectorType::get(Type::getFloatTy(Context
), 3);
374 return FixedVectorType::get(Type::getFloatTy(Context
), 4);
376 return FixedVectorType::get(Type::getFloatTy(Context
), 5);
378 return FixedVectorType::get(Type::getFloatTy(Context
), 6);
380 return FixedVectorType::get(Type::getFloatTy(Context
), 7);
382 return FixedVectorType::get(Type::getFloatTy(Context
), 8);
384 return FixedVectorType::get(Type::getFloatTy(Context
), 16);
386 return FixedVectorType::get(Type::getFloatTy(Context
), 32);
388 return FixedVectorType::get(Type::getFloatTy(Context
), 64);
390 return FixedVectorType::get(Type::getFloatTy(Context
), 128);
392 return FixedVectorType::get(Type::getFloatTy(Context
), 256);
394 return FixedVectorType::get(Type::getFloatTy(Context
), 512);
396 return FixedVectorType::get(Type::getFloatTy(Context
), 1024);
398 return FixedVectorType::get(Type::getFloatTy(Context
), 2048);
400 return FixedVectorType::get(Type::getDoubleTy(Context
), 1);
402 return FixedVectorType::get(Type::getDoubleTy(Context
), 2);
404 return FixedVectorType::get(Type::getDoubleTy(Context
), 3);
406 return FixedVectorType::get(Type::getDoubleTy(Context
), 4);
408 return FixedVectorType::get(Type::getDoubleTy(Context
), 8);
410 return FixedVectorType::get(Type::getDoubleTy(Context
), 16);
412 return FixedVectorType::get(Type::getDoubleTy(Context
), 32);
414 return FixedVectorType::get(Type::getDoubleTy(Context
), 64);
416 return FixedVectorType::get(Type::getDoubleTy(Context
), 128);
418 return FixedVectorType::get(Type::getDoubleTy(Context
), 256);
420 return ScalableVectorType::get(Type::getInt1Ty(Context
), 1);
422 return ScalableVectorType::get(Type::getInt1Ty(Context
), 2);
424 return ScalableVectorType::get(Type::getInt1Ty(Context
), 4);
426 return ScalableVectorType::get(Type::getInt1Ty(Context
), 8);
428 return ScalableVectorType::get(Type::getInt1Ty(Context
), 16);
430 return ScalableVectorType::get(Type::getInt1Ty(Context
), 32);
432 return ScalableVectorType::get(Type::getInt1Ty(Context
), 64);
434 return ScalableVectorType::get(Type::getInt8Ty(Context
), 1);
436 return ScalableVectorType::get(Type::getInt8Ty(Context
), 2);
438 return ScalableVectorType::get(Type::getInt8Ty(Context
), 4);
440 return ScalableVectorType::get(Type::getInt8Ty(Context
), 8);
442 return ScalableVectorType::get(Type::getInt8Ty(Context
), 16);
444 return ScalableVectorType::get(Type::getInt8Ty(Context
), 32);
446 return ScalableVectorType::get(Type::getInt8Ty(Context
), 64);
448 return ScalableVectorType::get(Type::getInt16Ty(Context
), 1);
450 return ScalableVectorType::get(Type::getInt16Ty(Context
), 2);
452 return ScalableVectorType::get(Type::getInt16Ty(Context
), 4);
454 return ScalableVectorType::get(Type::getInt16Ty(Context
), 8);
456 return ScalableVectorType::get(Type::getInt16Ty(Context
), 16);
458 return ScalableVectorType::get(Type::getInt16Ty(Context
), 32);
460 return ScalableVectorType::get(Type::getInt32Ty(Context
), 1);
462 return ScalableVectorType::get(Type::getInt32Ty(Context
), 2);
464 return ScalableVectorType::get(Type::getInt32Ty(Context
), 4);
466 return ScalableVectorType::get(Type::getInt32Ty(Context
), 8);
468 return ScalableVectorType::get(Type::getInt32Ty(Context
), 16);
470 return ScalableVectorType::get(Type::getInt32Ty(Context
), 32);
472 return ScalableVectorType::get(Type::getInt64Ty(Context
), 1);
474 return ScalableVectorType::get(Type::getInt64Ty(Context
), 2);
476 return ScalableVectorType::get(Type::getInt64Ty(Context
), 4);
478 return ScalableVectorType::get(Type::getInt64Ty(Context
), 8);
480 return ScalableVectorType::get(Type::getInt64Ty(Context
), 16);
482 return ScalableVectorType::get(Type::getInt64Ty(Context
), 32);
484 return ScalableVectorType::get(Type::getHalfTy(Context
), 1);
486 return ScalableVectorType::get(Type::getHalfTy(Context
), 2);
488 return ScalableVectorType::get(Type::getHalfTy(Context
), 4);
490 return ScalableVectorType::get(Type::getHalfTy(Context
), 8);
492 return ScalableVectorType::get(Type::getHalfTy(Context
), 16);
494 return ScalableVectorType::get(Type::getHalfTy(Context
), 32);
496 return ScalableVectorType::get(Type::getBFloatTy(Context
), 1);
498 return ScalableVectorType::get(Type::getBFloatTy(Context
), 2);
500 return ScalableVectorType::get(Type::getBFloatTy(Context
), 4);
502 return ScalableVectorType::get(Type::getBFloatTy(Context
), 8);
504 return ScalableVectorType::get(Type::getFloatTy(Context
), 1);
506 return ScalableVectorType::get(Type::getFloatTy(Context
), 2);
508 return ScalableVectorType::get(Type::getFloatTy(Context
), 4);
510 return ScalableVectorType::get(Type::getFloatTy(Context
), 8);
512 return ScalableVectorType::get(Type::getFloatTy(Context
), 16);
514 return ScalableVectorType::get(Type::getDoubleTy(Context
), 1);
516 return ScalableVectorType::get(Type::getDoubleTy(Context
), 2);
518 return ScalableVectorType::get(Type::getDoubleTy(Context
), 4);
520 return ScalableVectorType::get(Type::getDoubleTy(Context
), 8);
521 case MVT::Metadata
: return Type::getMetadataTy(Context
);
525 /// Return the value type corresponding to the specified type. This returns all
526 /// pointers as MVT::iPTR. If HandleUnknown is true, unknown types are returned
527 /// as Other, otherwise they are invalid.
528 MVT
MVT::getVT(Type
*Ty
, bool HandleUnknown
){
529 switch (Ty
->getTypeID()) {
531 if (HandleUnknown
) return MVT(MVT::Other
);
532 llvm_unreachable("Unknown type!");
535 case Type::IntegerTyID
:
536 return getIntegerVT(cast
<IntegerType
>(Ty
)->getBitWidth());
537 case Type::HalfTyID
: return MVT(MVT::f16
);
538 case Type::BFloatTyID
: return MVT(MVT::bf16
);
539 case Type::FloatTyID
: return MVT(MVT::f32
);
540 case Type::DoubleTyID
: return MVT(MVT::f64
);
541 case Type::X86_FP80TyID
: return MVT(MVT::f80
);
542 case Type::X86_MMXTyID
: return MVT(MVT::x86mmx
);
543 case Type::X86_AMXTyID
: return MVT(MVT::x86amx
);
544 case Type::FP128TyID
: return MVT(MVT::f128
);
545 case Type::PPC_FP128TyID
: return MVT(MVT::ppcf128
);
546 case Type::PointerTyID
: return MVT(MVT::iPTR
);
547 case Type::FixedVectorTyID
:
548 case Type::ScalableVectorTyID
: {
549 VectorType
*VTy
= cast
<VectorType
>(Ty
);
551 getVT(VTy
->getElementType(), /*HandleUnknown=*/ false),
552 VTy
->getElementCount());
557 /// getEVT - Return the value type corresponding to the specified type. This
558 /// returns all pointers as MVT::iPTR. If HandleUnknown is true, unknown types
559 /// are returned as Other, otherwise they are invalid.
560 EVT
EVT::getEVT(Type
*Ty
, bool HandleUnknown
){
561 switch (Ty
->getTypeID()) {
563 return MVT::getVT(Ty
, HandleUnknown
);
564 case Type::IntegerTyID
:
565 return getIntegerVT(Ty
->getContext(), cast
<IntegerType
>(Ty
)->getBitWidth());
566 case Type::FixedVectorTyID
:
567 case Type::ScalableVectorTyID
: {
568 VectorType
*VTy
= cast
<VectorType
>(Ty
);
569 return getVectorVT(Ty
->getContext(),
570 getEVT(VTy
->getElementType(), /*HandleUnknown=*/ false),
571 VTy
->getElementCount());