1 //===----------- ValueTypes.cpp - Implementation of EVT methods -----------===//
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 #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"
15 using namespace llvm;
16
changeExtendedTypeToInteger() const17 EVT EVT::changeExtendedTypeToInteger() const {
18 LLVMContext &Context = LLVMTy->getContext();
19 return getIntegerVT(Context, getSizeInBits());
20 }
21
changeExtendedVectorElementTypeToInteger() const22 EVT EVT::changeExtendedVectorElementTypeToInteger() const {
23 LLVMContext &Context = LLVMTy->getContext();
24 EVT IntTy = getIntegerVT(Context, getScalarSizeInBits());
25 return getVectorVT(Context, IntTy, getVectorNumElements());
26 }
27
getExtendedIntegerVT(LLVMContext & Context,unsigned BitWidth)28 EVT EVT::getExtendedIntegerVT(LLVMContext &Context, unsigned BitWidth) {
29 EVT VT;
30 VT.LLVMTy = IntegerType::get(Context, BitWidth);
31 assert(VT.isExtended() && "Type is not extended!");
32 return VT;
33 }
34
getExtendedVectorVT(LLVMContext & Context,EVT VT,unsigned NumElements)35 EVT EVT::getExtendedVectorVT(LLVMContext &Context, EVT VT,
36 unsigned NumElements) {
37 EVT ResultVT;
38 ResultVT.LLVMTy = VectorType::get(VT.getTypeForEVT(Context), NumElements);
39 assert(ResultVT.isExtended() && "Type is not extended!");
40 return ResultVT;
41 }
42
isExtendedFloatingPoint() const43 bool EVT::isExtendedFloatingPoint() const {
44 assert(isExtended() && "Type is not extended!");
45 return LLVMTy->isFPOrFPVectorTy();
46 }
47
isExtendedInteger() const48 bool EVT::isExtendedInteger() const {
49 assert(isExtended() && "Type is not extended!");
50 return LLVMTy->isIntOrIntVectorTy();
51 }
52
isExtendedScalarInteger() const53 bool EVT::isExtendedScalarInteger() const {
54 assert(isExtended() && "Type is not extended!");
55 return LLVMTy->isIntegerTy();
56 }
57
isExtendedVector() const58 bool EVT::isExtendedVector() const {
59 assert(isExtended() && "Type is not extended!");
60 return LLVMTy->isVectorTy();
61 }
62
isExtended16BitVector() const63 bool EVT::isExtended16BitVector() const {
64 return isExtendedVector() && getExtendedSizeInBits() == 16;
65 }
66
isExtended32BitVector() const67 bool EVT::isExtended32BitVector() const {
68 return isExtendedVector() && getExtendedSizeInBits() == 32;
69 }
70
isExtended64BitVector() const71 bool EVT::isExtended64BitVector() const {
72 return isExtendedVector() && getExtendedSizeInBits() == 64;
73 }
74
isExtended128BitVector() const75 bool EVT::isExtended128BitVector() const {
76 return isExtendedVector() && getExtendedSizeInBits() == 128;
77 }
78
isExtended256BitVector() const79 bool EVT::isExtended256BitVector() const {
80 return isExtendedVector() && getExtendedSizeInBits() == 256;
81 }
82
isExtended512BitVector() const83 bool EVT::isExtended512BitVector() const {
84 return isExtendedVector() && getExtendedSizeInBits() == 512;
85 }
86
isExtended1024BitVector() const87 bool EVT::isExtended1024BitVector() const {
88 return isExtendedVector() && getExtendedSizeInBits() == 1024;
89 }
90
isExtended2048BitVector() const91 bool EVT::isExtended2048BitVector() const {
92 return isExtendedVector() && getExtendedSizeInBits() == 2048;
93 }
94
getExtendedVectorElementType() const95 EVT EVT::getExtendedVectorElementType() const {
96 assert(isExtended() && "Type is not extended!");
97 return EVT::getEVT(cast<VectorType>(LLVMTy)->getElementType());
98 }
99
getExtendedVectorNumElements() const100 unsigned EVT::getExtendedVectorNumElements() const {
101 assert(isExtended() && "Type is not extended!");
102 return cast<VectorType>(LLVMTy)->getNumElements();
103 }
104
getExtendedSizeInBits() const105 TypeSize EVT::getExtendedSizeInBits() const {
106 assert(isExtended() && "Type is not extended!");
107 if (IntegerType *ITy = dyn_cast<IntegerType>(LLVMTy))
108 return TypeSize::Fixed(ITy->getBitWidth());
109 if (VectorType *VTy = dyn_cast<VectorType>(LLVMTy))
110 return VTy->getPrimitiveSizeInBits();
111 llvm_unreachable("Unrecognized extended type!");
112 }
113
114 /// getEVTString - This function returns value type as a string, e.g. "i32".
getEVTString() const115 std::string EVT::getEVTString() const {
116 switch (V.SimpleTy) {
117 default:
118 if (isVector())
119 return (isScalableVector() ? "nxv" : "v") + utostr(getVectorNumElements())
120 + getVectorElementType().getEVTString();
121 if (isInteger())
122 return "i" + utostr(getSizeInBits());
123 if (isFloatingPoint())
124 return "f" + utostr(getSizeInBits());
125 llvm_unreachable("Invalid EVT!");
126 case MVT::ppcf128: return "ppcf128";
127 case MVT::isVoid: return "isVoid";
128 case MVT::Other: return "ch";
129 case MVT::Glue: return "glue";
130 case MVT::x86mmx: return "x86mmx";
131 case MVT::Metadata:return "Metadata";
132 case MVT::Untyped: return "Untyped";
133 case MVT::exnref : return "exnref";
134 }
135 }
136
137 /// getTypeForEVT - This method returns an LLVM type corresponding to the
138 /// specified EVT. For integer types, this returns an unsigned type. Note
139 /// that this will abort for types that cannot be represented.
getTypeForEVT(LLVMContext & Context) const140 Type *EVT::getTypeForEVT(LLVMContext &Context) const {
141 switch (V.SimpleTy) {
142 default:
143 assert(isExtended() && "Type is not extended!");
144 return LLVMTy;
145 case MVT::isVoid: return Type::getVoidTy(Context);
146 case MVT::i1: return Type::getInt1Ty(Context);
147 case MVT::i8: return Type::getInt8Ty(Context);
148 case MVT::i16: return Type::getInt16Ty(Context);
149 case MVT::i32: return Type::getInt32Ty(Context);
150 case MVT::i64: return Type::getInt64Ty(Context);
151 case MVT::i128: return IntegerType::get(Context, 128);
152 case MVT::f16: return Type::getHalfTy(Context);
153 case MVT::f32: return Type::getFloatTy(Context);
154 case MVT::f64: return Type::getDoubleTy(Context);
155 case MVT::f80: return Type::getX86_FP80Ty(Context);
156 case MVT::f128: return Type::getFP128Ty(Context);
157 case MVT::ppcf128: return Type::getPPC_FP128Ty(Context);
158 case MVT::x86mmx: return Type::getX86_MMXTy(Context);
159 case MVT::v1i1: return VectorType::get(Type::getInt1Ty(Context), 1);
160 case MVT::v2i1: return VectorType::get(Type::getInt1Ty(Context), 2);
161 case MVT::v4i1: return VectorType::get(Type::getInt1Ty(Context), 4);
162 case MVT::v8i1: return VectorType::get(Type::getInt1Ty(Context), 8);
163 case MVT::v16i1: return VectorType::get(Type::getInt1Ty(Context), 16);
164 case MVT::v32i1: return VectorType::get(Type::getInt1Ty(Context), 32);
165 case MVT::v64i1: return VectorType::get(Type::getInt1Ty(Context), 64);
166 case MVT::v128i1: return VectorType::get(Type::getInt1Ty(Context), 128);
167 case MVT::v256i1: return VectorType::get(Type::getInt1Ty(Context), 256);
168 case MVT::v512i1: return VectorType::get(Type::getInt1Ty(Context), 512);
169 case MVT::v1024i1: return VectorType::get(Type::getInt1Ty(Context), 1024);
170 case MVT::v1i8: return VectorType::get(Type::getInt8Ty(Context), 1);
171 case MVT::v2i8: return VectorType::get(Type::getInt8Ty(Context), 2);
172 case MVT::v4i8: return VectorType::get(Type::getInt8Ty(Context), 4);
173 case MVT::v8i8: return VectorType::get(Type::getInt8Ty(Context), 8);
174 case MVT::v16i8: return VectorType::get(Type::getInt8Ty(Context), 16);
175 case MVT::v32i8: return VectorType::get(Type::getInt8Ty(Context), 32);
176 case MVT::v64i8: return VectorType::get(Type::getInt8Ty(Context), 64);
177 case MVT::v128i8: return VectorType::get(Type::getInt8Ty(Context), 128);
178 case MVT::v256i8: return VectorType::get(Type::getInt8Ty(Context), 256);
179 case MVT::v1i16: return VectorType::get(Type::getInt16Ty(Context), 1);
180 case MVT::v2i16: return VectorType::get(Type::getInt16Ty(Context), 2);
181 case MVT::v3i16: return VectorType::get(Type::getInt16Ty(Context), 3);
182 case MVT::v4i16: return VectorType::get(Type::getInt16Ty(Context), 4);
183 case MVT::v8i16: return VectorType::get(Type::getInt16Ty(Context), 8);
184 case MVT::v16i16: return VectorType::get(Type::getInt16Ty(Context), 16);
185 case MVT::v32i16: return VectorType::get(Type::getInt16Ty(Context), 32);
186 case MVT::v64i16: return VectorType::get(Type::getInt16Ty(Context), 64);
187 case MVT::v128i16: return VectorType::get(Type::getInt16Ty(Context), 128);
188 case MVT::v1i32: return VectorType::get(Type::getInt32Ty(Context), 1);
189 case MVT::v2i32: return VectorType::get(Type::getInt32Ty(Context), 2);
190 case MVT::v3i32: return VectorType::get(Type::getInt32Ty(Context), 3);
191 case MVT::v4i32: return VectorType::get(Type::getInt32Ty(Context), 4);
192 case MVT::v5i32: return VectorType::get(Type::getInt32Ty(Context), 5);
193 case MVT::v8i32: return VectorType::get(Type::getInt32Ty(Context), 8);
194 case MVT::v16i32: return VectorType::get(Type::getInt32Ty(Context), 16);
195 case MVT::v32i32: return VectorType::get(Type::getInt32Ty(Context), 32);
196 case MVT::v64i32: return VectorType::get(Type::getInt32Ty(Context), 64);
197 case MVT::v128i32: return VectorType::get(Type::getInt32Ty(Context), 128);
198 case MVT::v256i32: return VectorType::get(Type::getInt32Ty(Context), 256);
199 case MVT::v512i32: return VectorType::get(Type::getInt32Ty(Context), 512);
200 case MVT::v1024i32:return VectorType::get(Type::getInt32Ty(Context), 1024);
201 case MVT::v2048i32:return VectorType::get(Type::getInt32Ty(Context), 2048);
202 case MVT::v1i64: return VectorType::get(Type::getInt64Ty(Context), 1);
203 case MVT::v2i64: return VectorType::get(Type::getInt64Ty(Context), 2);
204 case MVT::v4i64: return VectorType::get(Type::getInt64Ty(Context), 4);
205 case MVT::v8i64: return VectorType::get(Type::getInt64Ty(Context), 8);
206 case MVT::v16i64: return VectorType::get(Type::getInt64Ty(Context), 16);
207 case MVT::v32i64: return VectorType::get(Type::getInt64Ty(Context), 32);
208 case MVT::v1i128: return VectorType::get(Type::getInt128Ty(Context), 1);
209 case MVT::v2f16: return VectorType::get(Type::getHalfTy(Context), 2);
210 case MVT::v3f16: return VectorType::get(Type::getHalfTy(Context), 3);
211 case MVT::v4f16: return VectorType::get(Type::getHalfTy(Context), 4);
212 case MVT::v8f16: return VectorType::get(Type::getHalfTy(Context), 8);
213 case MVT::v16f16: return VectorType::get(Type::getHalfTy(Context), 16);
214 case MVT::v32f16: return VectorType::get(Type::getHalfTy(Context), 32);
215 case MVT::v1f32: return VectorType::get(Type::getFloatTy(Context), 1);
216 case MVT::v2f32: return VectorType::get(Type::getFloatTy(Context), 2);
217 case MVT::v3f32: return VectorType::get(Type::getFloatTy(Context), 3);
218 case MVT::v4f32: return VectorType::get(Type::getFloatTy(Context), 4);
219 case MVT::v5f32: return VectorType::get(Type::getFloatTy(Context), 5);
220 case MVT::v8f32: return VectorType::get(Type::getFloatTy(Context), 8);
221 case MVT::v16f32: return VectorType::get(Type::getFloatTy(Context), 16);
222 case MVT::v32f32: return VectorType::get(Type::getFloatTy(Context), 32);
223 case MVT::v64f32: return VectorType::get(Type::getFloatTy(Context), 64);
224 case MVT::v128f32: return VectorType::get(Type::getFloatTy(Context), 128);
225 case MVT::v256f32: return VectorType::get(Type::getFloatTy(Context), 256);
226 case MVT::v512f32: return VectorType::get(Type::getFloatTy(Context), 512);
227 case MVT::v1024f32:return VectorType::get(Type::getFloatTy(Context), 1024);
228 case MVT::v2048f32:return VectorType::get(Type::getFloatTy(Context), 2048);
229 case MVT::v1f64: return VectorType::get(Type::getDoubleTy(Context), 1);
230 case MVT::v2f64: return VectorType::get(Type::getDoubleTy(Context), 2);
231 case MVT::v4f64: return VectorType::get(Type::getDoubleTy(Context), 4);
232 case MVT::v8f64: return VectorType::get(Type::getDoubleTy(Context), 8);
233 case MVT::nxv1i1:
234 return VectorType::get(Type::getInt1Ty(Context), 1, /*Scalable=*/ true);
235 case MVT::nxv2i1:
236 return VectorType::get(Type::getInt1Ty(Context), 2, /*Scalable=*/ true);
237 case MVT::nxv4i1:
238 return VectorType::get(Type::getInt1Ty(Context), 4, /*Scalable=*/ true);
239 case MVT::nxv8i1:
240 return VectorType::get(Type::getInt1Ty(Context), 8, /*Scalable=*/ true);
241 case MVT::nxv16i1:
242 return VectorType::get(Type::getInt1Ty(Context), 16, /*Scalable=*/ true);
243 case MVT::nxv32i1:
244 return VectorType::get(Type::getInt1Ty(Context), 32, /*Scalable=*/ true);
245 case MVT::nxv1i8:
246 return VectorType::get(Type::getInt8Ty(Context), 1, /*Scalable=*/ true);
247 case MVT::nxv2i8:
248 return VectorType::get(Type::getInt8Ty(Context), 2, /*Scalable=*/ true);
249 case MVT::nxv4i8:
250 return VectorType::get(Type::getInt8Ty(Context), 4, /*Scalable=*/ true);
251 case MVT::nxv8i8:
252 return VectorType::get(Type::getInt8Ty(Context), 8, /*Scalable=*/ true);
253 case MVT::nxv16i8:
254 return VectorType::get(Type::getInt8Ty(Context), 16, /*Scalable=*/ true);
255 case MVT::nxv32i8:
256 return VectorType::get(Type::getInt8Ty(Context), 32, /*Scalable=*/ true);
257 case MVT::nxv1i16:
258 return VectorType::get(Type::getInt16Ty(Context), 1, /*Scalable=*/ true);
259 case MVT::nxv2i16:
260 return VectorType::get(Type::getInt16Ty(Context), 2, /*Scalable=*/ true);
261 case MVT::nxv4i16:
262 return VectorType::get(Type::getInt16Ty(Context), 4, /*Scalable=*/ true);
263 case MVT::nxv8i16:
264 return VectorType::get(Type::getInt16Ty(Context), 8, /*Scalable=*/ true);
265 case MVT::nxv16i16:
266 return VectorType::get(Type::getInt16Ty(Context), 16, /*Scalable=*/ true);
267 case MVT::nxv32i16:
268 return VectorType::get(Type::getInt16Ty(Context), 32, /*Scalable=*/ true);
269 case MVT::nxv1i32:
270 return VectorType::get(Type::getInt32Ty(Context), 1, /*Scalable=*/ true);
271 case MVT::nxv2i32:
272 return VectorType::get(Type::getInt32Ty(Context), 2, /*Scalable=*/ true);
273 case MVT::nxv4i32:
274 return VectorType::get(Type::getInt32Ty(Context), 4, /*Scalable=*/ true);
275 case MVT::nxv8i32:
276 return VectorType::get(Type::getInt32Ty(Context), 8, /*Scalable=*/ true);
277 case MVT::nxv16i32:
278 return VectorType::get(Type::getInt32Ty(Context), 16,/*Scalable=*/ true);
279 case MVT::nxv32i32:
280 return VectorType::get(Type::getInt32Ty(Context), 32,/*Scalable=*/ true);
281 case MVT::nxv1i64:
282 return VectorType::get(Type::getInt64Ty(Context), 1, /*Scalable=*/ true);
283 case MVT::nxv2i64:
284 return VectorType::get(Type::getInt64Ty(Context), 2, /*Scalable=*/ true);
285 case MVT::nxv4i64:
286 return VectorType::get(Type::getInt64Ty(Context), 4, /*Scalable=*/ true);
287 case MVT::nxv8i64:
288 return VectorType::get(Type::getInt64Ty(Context), 8, /*Scalable=*/ true);
289 case MVT::nxv16i64:
290 return VectorType::get(Type::getInt64Ty(Context), 16, /*Scalable=*/ true);
291 case MVT::nxv32i64:
292 return VectorType::get(Type::getInt64Ty(Context), 32, /*Scalable=*/ true);
293 case MVT::nxv2f16:
294 return VectorType::get(Type::getHalfTy(Context), 2, /*Scalable=*/ true);
295 case MVT::nxv4f16:
296 return VectorType::get(Type::getHalfTy(Context), 4, /*Scalable=*/ true);
297 case MVT::nxv8f16:
298 return VectorType::get(Type::getHalfTy(Context), 8, /*Scalable=*/ true);
299 case MVT::nxv1f32:
300 return VectorType::get(Type::getFloatTy(Context), 1, /*Scalable=*/ true);
301 case MVT::nxv2f32:
302 return VectorType::get(Type::getFloatTy(Context), 2, /*Scalable=*/ true);
303 case MVT::nxv4f32:
304 return VectorType::get(Type::getFloatTy(Context), 4, /*Scalable=*/ true);
305 case MVT::nxv8f32:
306 return VectorType::get(Type::getFloatTy(Context), 8, /*Scalable=*/ true);
307 case MVT::nxv16f32:
308 return VectorType::get(Type::getFloatTy(Context), 16, /*Scalable=*/ true);
309 case MVT::nxv1f64:
310 return VectorType::get(Type::getDoubleTy(Context), 1, /*Scalable=*/ true);
311 case MVT::nxv2f64:
312 return VectorType::get(Type::getDoubleTy(Context), 2, /*Scalable=*/ true);
313 case MVT::nxv4f64:
314 return VectorType::get(Type::getDoubleTy(Context), 4, /*Scalable=*/ true);
315 case MVT::nxv8f64:
316 return VectorType::get(Type::getDoubleTy(Context), 8, /*Scalable=*/ true);
317 case MVT::Metadata: return Type::getMetadataTy(Context);
318 }
319 }
320
321 /// Return the value type corresponding to the specified type. This returns all
322 /// pointers as MVT::iPTR. If HandleUnknown is true, unknown types are returned
323 /// as Other, otherwise they are invalid.
getVT(Type * Ty,bool HandleUnknown)324 MVT MVT::getVT(Type *Ty, bool HandleUnknown){
325 switch (Ty->getTypeID()) {
326 default:
327 if (HandleUnknown) return MVT(MVT::Other);
328 llvm_unreachable("Unknown type!");
329 case Type::VoidTyID:
330 return MVT::isVoid;
331 case Type::IntegerTyID:
332 return getIntegerVT(cast<IntegerType>(Ty)->getBitWidth());
333 case Type::HalfTyID: return MVT(MVT::f16);
334 case Type::FloatTyID: return MVT(MVT::f32);
335 case Type::DoubleTyID: return MVT(MVT::f64);
336 case Type::X86_FP80TyID: return MVT(MVT::f80);
337 case Type::X86_MMXTyID: return MVT(MVT::x86mmx);
338 case Type::FP128TyID: return MVT(MVT::f128);
339 case Type::PPC_FP128TyID: return MVT(MVT::ppcf128);
340 case Type::PointerTyID: return MVT(MVT::iPTR);
341 case Type::VectorTyID: {
342 VectorType *VTy = cast<VectorType>(Ty);
343 return getVectorVT(
344 getVT(VTy->getElementType(), /*HandleUnknown=*/ false),
345 VTy->getElementCount());
346 }
347 }
348 }
349
350 /// getEVT - Return the value type corresponding to the specified type. This
351 /// returns all pointers as MVT::iPTR. If HandleUnknown is true, unknown types
352 /// are returned as Other, otherwise they are invalid.
getEVT(Type * Ty,bool HandleUnknown)353 EVT EVT::getEVT(Type *Ty, bool HandleUnknown){
354 switch (Ty->getTypeID()) {
355 default:
356 return MVT::getVT(Ty, HandleUnknown);
357 case Type::IntegerTyID:
358 return getIntegerVT(Ty->getContext(), cast<IntegerType>(Ty)->getBitWidth());
359 case Type::VectorTyID: {
360 VectorType *VTy = cast<VectorType>(Ty);
361 return getVectorVT(Ty->getContext(),
362 getEVT(VTy->getElementType(), /*HandleUnknown=*/ false),
363 VTy->getElementCount());
364 }
365 }
366 }
367