1//===- Type.cpp - Implement the Type class --------------------------------===//
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 implements the Type class for the IR library.
10//
11//===----------------------------------------------------------------------===//
12
13#include "llvm/IR/Type.h"
14#include "LLVMContextImpl.h"
15#include "llvm/ADT/APInt.h"
16#include "llvm/ADT/SetVector.h"
17#include "llvm/ADT/SmallString.h"
18#include "llvm/ADT/StringMap.h"
19#include "llvm/ADT/StringRef.h"
20#include "llvm/IR/Constant.h"
21#include "llvm/IR/Constants.h"
22#include "llvm/IR/DerivedTypes.h"
23#include "llvm/IR/LLVMContext.h"
24#include "llvm/IR/Value.h"
25#include "llvm/Support/Casting.h"
26#include "llvm/Support/Error.h"
27#include "llvm/Support/TypeSize.h"
28#include "llvm/Support/raw_ostream.h"
29#include "llvm/TargetParser/RISCVTargetParser.h"
30#include <cassert>
31
32using namespace llvm;
33
34//===----------------------------------------------------------------------===//
35// Type Class Implementation
36//===----------------------------------------------------------------------===//
37
38Type *Type::getPrimitiveType(LLVMContext &C, TypeID IDNumber) {
39 switch (IDNumber) {
40 case VoidTyID : return getVoidTy(C);
41 case HalfTyID : return getHalfTy(C);
42 case BFloatTyID : return getBFloatTy(C);
43 case FloatTyID : return getFloatTy(C);
44 case DoubleTyID : return getDoubleTy(C);
45 case X86_FP80TyID : return getX86_FP80Ty(C);
46 case FP128TyID : return getFP128Ty(C);
47 case PPC_FP128TyID : return getPPC_FP128Ty(C);
48 case LabelTyID : return getLabelTy(C);
49 case MetadataTyID : return getMetadataTy(C);
50 case X86_AMXTyID : return getX86_AMXTy(C);
51 case TokenTyID : return getTokenTy(C);
52 default:
53 return nullptr;
54 }
55}
56
57bool Type::isByteTy(unsigned BitWidth) const {
58 return isByteTy() && cast<ByteType>(Val: this)->getBitWidth() == BitWidth;
59}
60
61bool Type::isScalableTy() const {
62 switch (getTypeID()) {
63 case ScalableVectorTyID:
64 return true;
65 case TargetExtTyID:
66 return isScalableTargetExtTy();
67 case ArrayTyID:
68 return cast<ArrayType>(Val: this)->getElementType()->isScalableTy();
69 case StructTyID:
70 return cast<StructType>(Val: this)->isScalableTy();
71 default:
72 return false;
73 }
74}
75
76bool Type::containsNonGlobalTargetExtType() const {
77 if (const auto *ATy = dyn_cast<ArrayType>(Val: this))
78 return ATy->getElementType()->containsNonGlobalTargetExtType();
79 if (const auto *STy = dyn_cast<StructType>(Val: this))
80 return STy->containsNonGlobalTargetExtType();
81 if (auto *TT = dyn_cast<TargetExtType>(Val: this))
82 return !TT->hasProperty(Prop: TargetExtType::CanBeGlobal);
83 return false;
84}
85
86bool Type::containsNonLocalTargetExtType() const {
87 if (const auto *ATy = dyn_cast<ArrayType>(Val: this))
88 return ATy->getElementType()->containsNonLocalTargetExtType();
89 if (const auto *STy = dyn_cast<StructType>(Val: this))
90 return STy->containsNonLocalTargetExtType();
91 if (auto *TT = dyn_cast<TargetExtType>(Val: this))
92 return !TT->hasProperty(Prop: TargetExtType::CanBeLocal);
93 return false;
94}
95
96const fltSemantics &Type::getFltSemantics() const {
97 switch (getTypeID()) {
98 case HalfTyID: return APFloat::IEEEhalf();
99 case BFloatTyID: return APFloat::BFloat();
100 case FloatTyID: return APFloat::IEEEsingle();
101 case DoubleTyID: return APFloat::IEEEdouble();
102 case X86_FP80TyID: return APFloat::x87DoubleExtended();
103 case FP128TyID: return APFloat::IEEEquad();
104 case PPC_FP128TyID: return APFloat::PPCDoubleDouble();
105 default: llvm_unreachable("Invalid floating type");
106 }
107}
108
109bool Type::isScalableTargetExtTy() const {
110 if (auto *TT = dyn_cast<TargetExtType>(Val: this))
111 return isa<ScalableVectorType>(Val: TT->getLayoutType());
112 return false;
113}
114
115Type *Type::getFloatingPointTy(LLVMContext &C, const fltSemantics &S) {
116 switch (llvm::APFloat::SemanticsToEnum(Sem: S)) {
117 case llvm::APFloat::S_IEEEhalf:
118 return llvm::Type::getHalfTy(C);
119 case llvm::APFloat::S_BFloat:
120 return llvm::Type::getBFloatTy(C);
121 case llvm::APFloat::S_IEEEsingle:
122 return llvm::Type::getFloatTy(C);
123 case llvm::APFloat::S_IEEEdouble:
124 return llvm::Type::getDoubleTy(C);
125 case llvm::APFloat::S_IEEEquad:
126 return llvm::Type::getFP128Ty(C);
127 case llvm::APFloat::S_PPCDoubleDouble:
128 return llvm::Type::getPPC_FP128Ty(C);
129 case llvm::APFloat::S_x87DoubleExtended:
130 return llvm::Type::getX86_FP80Ty(C);
131 default:
132 llvm_unreachable("unhandled float format");
133 }
134}
135
136bool Type::isRISCVVectorTupleTy() const {
137 if (!isTargetExtTy())
138 return false;
139
140 return cast<TargetExtType>(Val: this)->getName() == "riscv.vector.tuple";
141}
142
143bool Type::canLosslesslyBitCastTo(Type *Ty) const {
144 // Identity cast means no change so return true
145 if (this == Ty)
146 return true;
147
148 // They are not convertible unless they are at least first class types
149 if (!this->isFirstClassType() || !Ty->isFirstClassType())
150 return false;
151
152 // Vector -> Vector conversions are always lossless if the two vector types
153 // have the same size, otherwise not.
154 if (isa<VectorType>(Val: this) && isa<VectorType>(Val: Ty))
155 return getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits();
156
157 // 8192-bit fixed width vector types can be losslessly converted to x86amx.
158 if (((isa<FixedVectorType>(Val: this)) && Ty->isX86_AMXTy()) &&
159 getPrimitiveSizeInBits().getFixedValue() == 8192)
160 return true;
161 if ((isX86_AMXTy() && isa<FixedVectorType>(Val: Ty)) &&
162 Ty->getPrimitiveSizeInBits().getFixedValue() == 8192)
163 return true;
164
165 // Conservatively assume we can't losslessly convert between pointers with
166 // different address spaces.
167 return false;
168}
169
170bool Type::isEmptyTy() const {
171 if (auto *ATy = dyn_cast<ArrayType>(Val: this)) {
172 unsigned NumElements = ATy->getNumElements();
173 return NumElements == 0 || ATy->getElementType()->isEmptyTy();
174 }
175
176 if (auto *STy = dyn_cast<StructType>(Val: this)) {
177 unsigned NumElements = STy->getNumElements();
178 for (unsigned i = 0; i < NumElements; ++i)
179 if (!STy->getElementType(N: i)->isEmptyTy())
180 return false;
181 return true;
182 }
183
184 return false;
185}
186
187TypeSize Type::getPrimitiveSizeInBits() const {
188 switch (getTypeID()) {
189 case Type::HalfTyID:
190 return TypeSize::getFixed(ExactSize: 16);
191 case Type::BFloatTyID:
192 return TypeSize::getFixed(ExactSize: 16);
193 case Type::FloatTyID:
194 return TypeSize::getFixed(ExactSize: 32);
195 case Type::DoubleTyID:
196 return TypeSize::getFixed(ExactSize: 64);
197 case Type::X86_FP80TyID:
198 return TypeSize::getFixed(ExactSize: 80);
199 case Type::FP128TyID:
200 return TypeSize::getFixed(ExactSize: 128);
201 case Type::PPC_FP128TyID:
202 return TypeSize::getFixed(ExactSize: 128);
203 case Type::X86_AMXTyID:
204 return TypeSize::getFixed(ExactSize: 8192);
205 case Type::ByteTyID:
206 return TypeSize::getFixed(ExactSize: cast<ByteType>(Val: this)->getBitWidth());
207 case Type::IntegerTyID:
208 return TypeSize::getFixed(ExactSize: cast<IntegerType>(Val: this)->getBitWidth());
209 case Type::FixedVectorTyID:
210 case Type::ScalableVectorTyID: {
211 const VectorType *VTy = cast<VectorType>(Val: this);
212 ElementCount EC = VTy->getElementCount();
213 TypeSize ETS = VTy->getElementType()->getPrimitiveSizeInBits();
214 assert(!ETS.isScalable() && "Vector type should have fixed-width elements");
215 return {ETS.getFixedValue() * EC.getKnownMinValue(), EC.isScalable()};
216 }
217 default:
218 return TypeSize::getFixed(ExactSize: 0);
219 }
220}
221
222unsigned Type::getScalarSizeInBits() const {
223 // It is safe to assume that the scalar types have a fixed size.
224 return getScalarType()->getPrimitiveSizeInBits().getFixedValue();
225}
226
227int Type::getFPMantissaWidth() const {
228 if (auto *VTy = dyn_cast<VectorType>(Val: this))
229 return VTy->getElementType()->getFPMantissaWidth();
230 assert(isFloatingPointTy() && "Not a floating point type!");
231 if (getTypeID() == HalfTyID) return 11;
232 if (getTypeID() == BFloatTyID) return 8;
233 if (getTypeID() == FloatTyID) return 24;
234 if (getTypeID() == DoubleTyID) return 53;
235 if (getTypeID() == X86_FP80TyID) return 64;
236 if (getTypeID() == FP128TyID) return 113;
237 assert(getTypeID() == PPC_FP128TyID && "unknown fp type");
238 return -1;
239}
240
241bool Type::isFirstClassType() const {
242 switch (getTypeID()) {
243 default:
244 return true;
245 case FunctionTyID:
246 case VoidTyID:
247 return false;
248 case StructTyID: {
249 auto *ST = cast<StructType>(Val: this);
250 return !ST->isOpaque();
251 }
252 }
253}
254
255bool Type::isSizedDerivedType() const {
256 if (auto *ATy = dyn_cast<ArrayType>(Val: this))
257 return ATy->getElementType()->isSized();
258
259 if (auto *VTy = dyn_cast<VectorType>(Val: this))
260 return VTy->getElementType()->isSized();
261
262 if (auto *TTy = dyn_cast<TargetExtType>(Val: this))
263 return TTy->getLayoutType()->isSized();
264
265 return cast<StructType>(Val: this)->isSized();
266}
267
268//===----------------------------------------------------------------------===//
269// Primitive 'Type' data
270//===----------------------------------------------------------------------===//
271
272Type *Type::getVoidTy(LLVMContext &C) { return &C.pImpl->VoidTy; }
273Type *Type::getLabelTy(LLVMContext &C) { return &C.pImpl->LabelTy; }
274Type *Type::getHalfTy(LLVMContext &C) { return &C.pImpl->HalfTy; }
275Type *Type::getBFloatTy(LLVMContext &C) { return &C.pImpl->BFloatTy; }
276Type *Type::getFloatTy(LLVMContext &C) { return &C.pImpl->FloatTy; }
277Type *Type::getDoubleTy(LLVMContext &C) { return &C.pImpl->DoubleTy; }
278Type *Type::getMetadataTy(LLVMContext &C) { return &C.pImpl->MetadataTy; }
279Type *Type::getTokenTy(LLVMContext &C) { return &C.pImpl->TokenTy; }
280Type *Type::getX86_FP80Ty(LLVMContext &C) { return &C.pImpl->X86_FP80Ty; }
281Type *Type::getFP128Ty(LLVMContext &C) { return &C.pImpl->FP128Ty; }
282Type *Type::getPPC_FP128Ty(LLVMContext &C) { return &C.pImpl->PPC_FP128Ty; }
283Type *Type::getX86_AMXTy(LLVMContext &C) { return &C.pImpl->X86_AMXTy; }
284
285ByteType *Type::getByte1Ty(LLVMContext &C) { return &C.pImpl->Byte1Ty; }
286ByteType *Type::getByte8Ty(LLVMContext &C) { return &C.pImpl->Byte8Ty; }
287ByteType *Type::getByte16Ty(LLVMContext &C) { return &C.pImpl->Byte16Ty; }
288ByteType *Type::getByte32Ty(LLVMContext &C) { return &C.pImpl->Byte32Ty; }
289ByteType *Type::getByte64Ty(LLVMContext &C) { return &C.pImpl->Byte64Ty; }
290ByteType *Type::getByte128Ty(LLVMContext &C) { return &C.pImpl->Byte128Ty; }
291
292ByteType *Type::getByteNTy(LLVMContext &C, unsigned N) {
293 return ByteType::get(C, NumBits: N);
294}
295
296IntegerType *Type::getInt1Ty(LLVMContext &C) { return &C.pImpl->Int1Ty; }
297IntegerType *Type::getInt8Ty(LLVMContext &C) { return &C.pImpl->Int8Ty; }
298IntegerType *Type::getInt16Ty(LLVMContext &C) { return &C.pImpl->Int16Ty; }
299IntegerType *Type::getInt32Ty(LLVMContext &C) { return &C.pImpl->Int32Ty; }
300IntegerType *Type::getInt64Ty(LLVMContext &C) { return &C.pImpl->Int64Ty; }
301IntegerType *Type::getInt128Ty(LLVMContext &C) { return &C.pImpl->Int128Ty; }
302
303IntegerType *Type::getIntNTy(LLVMContext &C, unsigned N) {
304 return IntegerType::get(C, NumBits: N);
305}
306
307Type *Type::getIntFromByteType(Type *Ty) {
308 assert(Ty->isByteOrByteVectorTy() && "Expected a byte or byte vector type.");
309 unsigned NumBits = Ty->getScalarSizeInBits();
310 IntegerType *IntTy = IntegerType::get(C&: Ty->getContext(), NumBits);
311 if (VectorType *VecTy = dyn_cast<VectorType>(Val: Ty))
312 return VectorType::get(ElementType: IntTy, Other: VecTy);
313 return IntTy;
314}
315
316Type *Type::getByteFromIntType(Type *Ty) {
317 assert(!Ty->isPtrOrPtrVectorTy() &&
318 "Expected a non-pointer or non-pointer vector type.");
319 unsigned NumBits = Ty->getScalarSizeInBits();
320 ByteType *ByteTy = ByteType::get(C&: Ty->getContext(), NumBits);
321 if (VectorType *VecTy = dyn_cast<VectorType>(Val: Ty))
322 return VectorType::get(ElementType: ByteTy, Other: VecTy);
323 return ByteTy;
324}
325
326Type *Type::getWasm_ExternrefTy(LLVMContext &C) {
327 return TargetExtType::get(Context&: C, Name: "wasm.externref", Types: {}, Ints: {});
328}
329
330Type *Type::getWasm_FuncrefTy(LLVMContext &C) {
331 return TargetExtType::get(Context&: C, Name: "wasm.funcref", Types: {}, Ints: {});
332}
333
334//===----------------------------------------------------------------------===//
335// IntegerType Implementation
336//===----------------------------------------------------------------------===//
337
338IntegerType *IntegerType::get(LLVMContext &C, unsigned NumBits) {
339 assert(NumBits >= MIN_INT_BITS && "bitwidth too small");
340 assert(NumBits <= MAX_INT_BITS && "bitwidth too large");
341
342 // Check for the built-in integer types
343 switch (NumBits) {
344 case 1: return Type::getInt1Ty(C);
345 case 8: return Type::getInt8Ty(C);
346 case 16: return Type::getInt16Ty(C);
347 case 32: return Type::getInt32Ty(C);
348 case 64: return Type::getInt64Ty(C);
349 case 128: return Type::getInt128Ty(C);
350 default:
351 break;
352 }
353
354 IntegerType *&Entry = C.pImpl->IntegerTypes[NumBits];
355
356 if (!Entry)
357 Entry = new (C.pImpl->Alloc) IntegerType(C, NumBits);
358
359 return Entry;
360}
361
362APInt IntegerType::getMask() const { return APInt::getAllOnes(numBits: getBitWidth()); }
363
364//===----------------------------------------------------------------------===//
365// ByteType Implementation
366//===----------------------------------------------------------------------===//
367
368ByteType *ByteType::get(LLVMContext &C, unsigned NumBits) {
369 assert(NumBits >= MIN_BYTE_BITS && "bitwidth too small");
370 assert(NumBits <= MAX_BYTE_BITS && "bitwidth too large");
371
372 // Check for the built-in byte types
373 switch (NumBits) {
374 case 8:
375 return Type::getByte8Ty(C);
376 case 16:
377 return Type::getByte16Ty(C);
378 case 32:
379 return Type::getByte32Ty(C);
380 case 64:
381 return Type::getByte64Ty(C);
382 case 128:
383 return Type::getByte128Ty(C);
384 default:
385 break;
386 }
387
388 ByteType *&Entry = C.pImpl->ByteTypes[NumBits];
389
390 if (!Entry)
391 Entry = new (C.pImpl->Alloc) ByteType(C, NumBits);
392
393 return Entry;
394}
395
396APInt ByteType::getMask() const { return APInt::getAllOnes(numBits: getBitWidth()); }
397
398//===----------------------------------------------------------------------===//
399// FunctionType Implementation
400//===----------------------------------------------------------------------===//
401
402FunctionType::FunctionType(Type *Result, ArrayRef<Type*> Params,
403 bool IsVarArgs)
404 : Type(Result->getContext(), FunctionTyID) {
405 Type **SubTys = reinterpret_cast<Type**>(this+1);
406 assert(isValidReturnType(Result) && "invalid return type for function");
407 setSubclassData(IsVarArgs);
408
409 SubTys[0] = Result;
410
411 for (unsigned i = 0, e = Params.size(); i != e; ++i) {
412 assert(isValidArgumentType(Params[i]) &&
413 "Not a valid type for function argument!");
414 SubTys[i+1] = Params[i];
415 }
416
417 ContainedTys = SubTys;
418 NumContainedTys = Params.size() + 1; // + 1 for result type
419}
420
421// This is the factory function for the FunctionType class.
422FunctionType *FunctionType::get(Type *ReturnType,
423 ArrayRef<Type*> Params, bool isVarArg) {
424 LLVMContextImpl *pImpl = ReturnType->getContext().pImpl;
425 const FunctionTypeKeyInfo::KeyTy Key(ReturnType, Params, isVarArg);
426 FunctionType *FT;
427 // Since we only want to allocate a fresh function type in case none is found
428 // and we don't want to perform two lookups (one for checking if existent and
429 // one for inserting the newly allocated one), here we instead lookup based on
430 // Key and update the reference to the function type in-place to a newly
431 // allocated one if not found.
432 auto Insertion = pImpl->FunctionTypes.insert_as(V: nullptr, LookupKey: Key);
433 if (Insertion.second) {
434 // The function type was not found. Allocate one and update FunctionTypes
435 // in-place.
436 FT = (FunctionType *)pImpl->Alloc.Allocate(
437 Size: sizeof(FunctionType) + sizeof(Type *) * (Params.size() + 1),
438 Alignment: alignof(FunctionType));
439 new (FT) FunctionType(ReturnType, Params, isVarArg);
440 *Insertion.first = FT;
441 } else {
442 // The function type was found. Just return it.
443 FT = *Insertion.first;
444 }
445 return FT;
446}
447
448FunctionType *FunctionType::get(Type *Result, bool isVarArg) {
449 return get(ReturnType: Result, Params: {}, isVarArg);
450}
451
452bool FunctionType::isValidReturnType(Type *RetTy) {
453 return !RetTy->isFunctionTy() && !RetTy->isLabelTy() &&
454 !RetTy->isMetadataTy();
455}
456
457bool FunctionType::isValidArgumentType(Type *ArgTy) {
458 return ArgTy->isFirstClassType() && !ArgTy->isLabelTy();
459}
460
461//===----------------------------------------------------------------------===//
462// StructType Implementation
463//===----------------------------------------------------------------------===//
464
465// Primitive Constructors.
466
467StructType *StructType::get(LLVMContext &Context, ArrayRef<Type*> ETypes,
468 bool isPacked) {
469 LLVMContextImpl *pImpl = Context.pImpl;
470 const AnonStructTypeKeyInfo::KeyTy Key(ETypes, isPacked);
471
472 StructType *ST;
473 // Since we only want to allocate a fresh struct type in case none is found
474 // and we don't want to perform two lookups (one for checking if existent and
475 // one for inserting the newly allocated one), here we instead lookup based on
476 // Key and update the reference to the struct type in-place to a newly
477 // allocated one if not found.
478 auto Insertion = pImpl->AnonStructTypes.insert_as(V: nullptr, LookupKey: Key);
479 if (Insertion.second) {
480 // The struct type was not found. Allocate one and update AnonStructTypes
481 // in-place.
482 ST = new (Context.pImpl->Alloc) StructType(Context);
483 ST->setSubclassData(SCDB_IsLiteral); // Literal struct.
484 ST->setBody(Elements: ETypes, isPacked);
485 *Insertion.first = ST;
486 } else {
487 // The struct type was found. Just return it.
488 ST = *Insertion.first;
489 }
490
491 return ST;
492}
493
494bool StructType::isScalableTy() const {
495 if ((getSubclassData() & SCDB_ContainsScalableVector) != 0)
496 return true;
497
498 if ((getSubclassData() & SCDB_NotContainsScalableVector) != 0)
499 return false;
500
501 for (Type *Ty : elements()) {
502 if (Ty->isScalableTy()) {
503 const_cast<StructType *>(this)->setSubclassData(
504 getSubclassData() | SCDB_ContainsScalableVector);
505 return true;
506 }
507 }
508
509 // For structures that are opaque, return false but do not set the
510 // SCDB_NotContainsScalableVector flag since it may gain scalable vector type
511 // when it becomes non-opaque.
512 if (!isOpaque())
513 const_cast<StructType *>(this)->setSubclassData(
514 getSubclassData() | SCDB_NotContainsScalableVector);
515 return false;
516}
517
518bool StructType::containsNonGlobalTargetExtType() const {
519 if ((getSubclassData() & SCDB_ContainsNonGlobalTargetExtType) != 0)
520 return true;
521
522 if ((getSubclassData() & SCDB_NotContainsNonGlobalTargetExtType) != 0)
523 return false;
524
525 for (Type *Ty : elements()) {
526 if (Ty->containsNonGlobalTargetExtType()) {
527 const_cast<StructType *>(this)->setSubclassData(
528 getSubclassData() | SCDB_ContainsNonGlobalTargetExtType);
529 return true;
530 }
531 }
532
533 // For structures that are opaque, return false but do not set the
534 // SCDB_NotContainsNonGlobalTargetExtType flag since it may gain non-global
535 // target extension types when it becomes non-opaque.
536 if (!isOpaque())
537 const_cast<StructType *>(this)->setSubclassData(
538 getSubclassData() | SCDB_NotContainsNonGlobalTargetExtType);
539 return false;
540}
541
542bool StructType::containsNonLocalTargetExtType() const {
543 if ((getSubclassData() & SCDB_ContainsNonLocalTargetExtType) != 0)
544 return true;
545
546 if ((getSubclassData() & SCDB_NotContainsNonLocalTargetExtType) != 0)
547 return false;
548
549 for (Type *Ty : elements()) {
550 if (Ty->containsNonLocalTargetExtType()) {
551 const_cast<StructType *>(this)->setSubclassData(
552 getSubclassData() | SCDB_ContainsNonLocalTargetExtType);
553 return true;
554 }
555 }
556
557 // For structures that are opaque, return false but do not set the
558 // SCDB_NotContainsNonLocalTargetExtType flag since it may gain non-local
559 // target extension types when it becomes non-opaque.
560 if (!isOpaque())
561 const_cast<StructType *>(this)->setSubclassData(
562 getSubclassData() | SCDB_NotContainsNonLocalTargetExtType);
563 return false;
564}
565
566bool StructType::containsHomogeneousScalableVectorTypes() const {
567 if (getNumElements() <= 0 || !isa<ScalableVectorType>(Val: elements().front()))
568 return false;
569 return containsHomogeneousTypes();
570}
571
572bool StructType::containsHomogeneousTypes() const {
573 ArrayRef<Type *> ElementTys = elements();
574 return !ElementTys.empty() && all_equal(Range&: ElementTys);
575}
576
577void StructType::setBody(ArrayRef<Type*> Elements, bool isPacked) {
578 cantFail(Err: setBodyOrError(Elements, isPacked));
579}
580
581Error StructType::setBodyOrError(ArrayRef<Type *> Elements, bool isPacked) {
582 assert(isOpaque() && "Struct body already set!");
583
584 if (auto E = checkBody(Elements))
585 return E;
586
587 setSubclassData(getSubclassData() | SCDB_HasBody);
588 if (isPacked)
589 setSubclassData(getSubclassData() | SCDB_Packed);
590
591 NumContainedTys = Elements.size();
592 ContainedTys = Elements.empty()
593 ? nullptr
594 : Elements.copy(A&: getContext().pImpl->Alloc).data();
595
596 return Error::success();
597}
598
599Error StructType::checkBody(ArrayRef<Type *> Elements) {
600 SmallSetVector<Type *, 4> Worklist(Elements.begin(), Elements.end());
601 for (unsigned I = 0; I < Worklist.size(); ++I) {
602 Type *Ty = Worklist[I];
603 if (Ty == this)
604 return createStringError(S: Twine("identified structure type '") +
605 getName() + "' is recursive");
606 Worklist.insert_range(R: Ty->subtypes());
607 }
608 return Error::success();
609}
610
611void StructType::setName(StringRef Name) {
612 if (Name == getName()) return;
613
614 StringMap<StructType *> &SymbolTable = getContext().pImpl->NamedStructTypes;
615
616 using EntryTy = StringMap<StructType *>::MapEntryTy;
617
618 // If this struct already had a name, remove its symbol table entry. Don't
619 // delete the data yet because it may be part of the new name.
620 if (SymbolTableEntry)
621 SymbolTable.remove(KeyValue: (EntryTy *)SymbolTableEntry);
622
623 // If this is just removing the name, we're done.
624 if (Name.empty()) {
625 if (SymbolTableEntry) {
626 // Delete the old string data.
627 ((EntryTy *)SymbolTableEntry)->Destroy(allocator&: SymbolTable.getAllocator());
628 SymbolTableEntry = nullptr;
629 }
630 return;
631 }
632
633 // Look up the entry for the name.
634 auto IterBool =
635 getContext().pImpl->NamedStructTypes.insert(KV: std::make_pair(x&: Name, y: this));
636
637 // While we have a name collision, try a random rename.
638 if (!IterBool.second) {
639 SmallString<64> TempStr(Name);
640 TempStr.push_back(Elt: '.');
641 raw_svector_ostream TmpStream(TempStr);
642 unsigned NameSize = Name.size();
643
644 do {
645 TempStr.resize(N: NameSize + 1);
646 TmpStream << getContext().pImpl->NamedStructTypesUniqueID++;
647
648 IterBool = getContext().pImpl->NamedStructTypes.insert(
649 KV: std::make_pair(x: TmpStream.str(), y: this));
650 } while (!IterBool.second);
651 }
652
653 // Delete the old string data.
654 if (SymbolTableEntry)
655 ((EntryTy *)SymbolTableEntry)->Destroy(allocator&: SymbolTable.getAllocator());
656 SymbolTableEntry = &*IterBool.first;
657}
658
659//===----------------------------------------------------------------------===//
660// StructType Helper functions.
661
662StructType *StructType::create(LLVMContext &Context, StringRef Name) {
663 StructType *ST = new (Context.pImpl->Alloc) StructType(Context);
664 if (!Name.empty())
665 ST->setName(Name);
666 return ST;
667}
668
669StructType *StructType::get(LLVMContext &Context, bool isPacked) {
670 return get(Context, ETypes: {}, isPacked);
671}
672
673StructType *StructType::create(LLVMContext &Context, ArrayRef<Type*> Elements,
674 StringRef Name, bool isPacked) {
675 StructType *ST = create(Context, Name);
676 ST->setBody(Elements, isPacked);
677 return ST;
678}
679
680StructType *StructType::create(LLVMContext &Context, ArrayRef<Type*> Elements) {
681 return create(Context, Elements, Name: StringRef());
682}
683
684StructType *StructType::create(LLVMContext &Context) {
685 return create(Context, Name: StringRef());
686}
687
688StructType *StructType::create(ArrayRef<Type*> Elements, StringRef Name,
689 bool isPacked) {
690 assert(!Elements.empty() &&
691 "This method may not be invoked with an empty list");
692 return create(Context&: Elements[0]->getContext(), Elements, Name, isPacked);
693}
694
695StructType *StructType::create(ArrayRef<Type*> Elements) {
696 assert(!Elements.empty() &&
697 "This method may not be invoked with an empty list");
698 return create(Context&: Elements[0]->getContext(), Elements, Name: StringRef());
699}
700
701bool StructType::isSized() const {
702 if ((getSubclassData() & SCDB_IsSized) != 0)
703 return true;
704 if (isOpaque())
705 return false;
706
707 // Okay, our struct is sized if all of the elements are, but if one of the
708 // elements is opaque, the struct isn't sized *yet*, but may become sized in
709 // the future, so just bail out without caching.
710 // The ONLY special case inside a struct that is considered sized is when the
711 // elements are homogeneous of a scalable vector type.
712 if (containsHomogeneousScalableVectorTypes()) {
713 const_cast<StructType *>(this)->setSubclassData(getSubclassData() |
714 SCDB_IsSized);
715 return true;
716 }
717 for (Type *Ty : elements()) {
718 // If the struct contains a scalable vector type, don't consider it sized.
719 // This prevents it from being used in loads/stores/allocas/GEPs. The ONLY
720 // special case right now is a structure of homogenous scalable vector
721 // types and is handled by the if-statement before this for-loop.
722 if (Ty->isScalableTy())
723 return false;
724 if (!Ty->isSized())
725 return false;
726 }
727
728 // Here we cheat a bit and cast away const-ness. The goal is to memoize when
729 // we find a sized type, as types can only move from opaque to sized, not the
730 // other way.
731 const_cast<StructType*>(this)->setSubclassData(
732 getSubclassData() | SCDB_IsSized);
733 return true;
734}
735
736StringRef StructType::getName() const {
737 assert(!isLiteral() && "Literal structs never have names");
738 if (!SymbolTableEntry) return StringRef();
739
740 return ((StringMapEntry<StructType*> *)SymbolTableEntry)->getKey();
741}
742
743bool StructType::isValidElementType(Type *ElemTy) {
744 return !ElemTy->isVoidTy() && !ElemTy->isLabelTy() &&
745 !ElemTy->isMetadataTy() && !ElemTy->isFunctionTy() &&
746 !ElemTy->isTokenTy();
747}
748
749bool StructType::isLayoutIdentical(StructType *Other) const {
750 if (this == Other) return true;
751
752 if (isPacked() != Other->isPacked())
753 return false;
754
755 return elements() == Other->elements();
756}
757
758Type *StructType::getTypeAtIndex(const Value *V) const {
759 unsigned Idx = (unsigned)cast<Constant>(Val: V)->getUniqueInteger().getZExtValue();
760 assert(indexValid(Idx) && "Invalid structure index!");
761 return getElementType(N: Idx);
762}
763
764bool StructType::indexValid(const Value *V) const {
765 // Structure indexes require (vectors of) 32-bit integer constants. In the
766 // vector case all of the indices must be equal.
767 if (!V->getType()->isIntOrIntVectorTy(BitWidth: 32))
768 return false;
769 if (isa<ScalableVectorType>(Val: V->getType()))
770 return false;
771 const Constant *C = dyn_cast<Constant>(Val: V);
772 if (C && V->getType()->isVectorTy())
773 C = C->getSplatValue();
774 const ConstantInt *CU = dyn_cast_or_null<ConstantInt>(Val: C);
775 return CU && CU->getZExtValue() < getNumElements();
776}
777
778StructType *StructType::getTypeByName(LLVMContext &C, StringRef Name) {
779 return C.pImpl->NamedStructTypes.lookup(Key: Name);
780}
781
782//===----------------------------------------------------------------------===//
783// ArrayType Implementation
784//===----------------------------------------------------------------------===//
785
786ArrayType::ArrayType(Type *ElType, uint64_t NumEl)
787 : Type(ElType->getContext(), ArrayTyID), ContainedType(ElType),
788 NumElements(NumEl) {
789 ContainedTys = &ContainedType;
790 NumContainedTys = 1;
791}
792
793ArrayType *ArrayType::get(Type *ElementType, uint64_t NumElements) {
794 assert(isValidElementType(ElementType) && "Invalid type for array element!");
795
796 LLVMContextImpl *pImpl = ElementType->getContext().pImpl;
797 ArrayType *&Entry =
798 pImpl->ArrayTypes[std::make_pair(x&: ElementType, y&: NumElements)];
799
800 if (!Entry)
801 Entry = new (pImpl->Alloc) ArrayType(ElementType, NumElements);
802 return Entry;
803}
804
805bool ArrayType::isValidElementType(Type *ElemTy) {
806 return !ElemTy->isVoidTy() && !ElemTy->isLabelTy() &&
807 !ElemTy->isMetadataTy() && !ElemTy->isFunctionTy() &&
808 !ElemTy->isTokenTy() && !ElemTy->isX86_AMXTy();
809}
810
811//===----------------------------------------------------------------------===//
812// VectorType Implementation
813//===----------------------------------------------------------------------===//
814
815VectorType::VectorType(Type *ElType, unsigned EQ, Type::TypeID TID)
816 : Type(ElType->getContext(), TID), ContainedType(ElType),
817 ElementQuantity(EQ) {
818 ContainedTys = &ContainedType;
819 NumContainedTys = 1;
820}
821
822VectorType *VectorType::get(Type *ElementType, ElementCount EC) {
823 if (EC.isScalable())
824 return ScalableVectorType::get(ElementType, MinNumElts: EC.getKnownMinValue());
825 else
826 return FixedVectorType::get(ElementType, NumElts: EC.getKnownMinValue());
827}
828
829bool VectorType::isValidElementType(Type *ElemTy) {
830 if (ElemTy->isIntegerTy() || ElemTy->isFloatingPointTy() ||
831 ElemTy->isPointerTy() || ElemTy->getTypeID() == TypedPointerTyID ||
832 ElemTy->isByteTy())
833 return true;
834 if (auto *TTy = dyn_cast<TargetExtType>(Val: ElemTy))
835 return TTy->hasProperty(Prop: TargetExtType::CanBeVectorElement);
836 return false;
837}
838
839//===----------------------------------------------------------------------===//
840// FixedVectorType Implementation
841//===----------------------------------------------------------------------===//
842
843FixedVectorType *FixedVectorType::get(Type *ElementType, unsigned NumElts) {
844 assert(NumElts > 0 && "#Elements of a VectorType must be greater than 0");
845 assert(isValidElementType(ElementType) && "Element type of a VectorType must "
846 "be an integer, floating point, "
847 "pointer type, or a valid target "
848 "extension type.");
849
850 auto EC = ElementCount::getFixed(MinVal: NumElts);
851
852 LLVMContextImpl *pImpl = ElementType->getContext().pImpl;
853 VectorType *&Entry = ElementType->getContext()
854 .pImpl->VectorTypes[std::make_pair(x&: ElementType, y&: EC)];
855
856 if (!Entry)
857 Entry = new (pImpl->Alloc) FixedVectorType(ElementType, NumElts);
858 return cast<FixedVectorType>(Val: Entry);
859}
860
861//===----------------------------------------------------------------------===//
862// ScalableVectorType Implementation
863//===----------------------------------------------------------------------===//
864
865ScalableVectorType *ScalableVectorType::get(Type *ElementType,
866 unsigned MinNumElts) {
867 assert(MinNumElts > 0 && "#Elements of a VectorType must be greater than 0");
868 assert(isValidElementType(ElementType) && "Element type of a VectorType must "
869 "be an integer, floating point, or "
870 "pointer type.");
871
872 auto EC = ElementCount::getScalable(MinVal: MinNumElts);
873
874 LLVMContextImpl *pImpl = ElementType->getContext().pImpl;
875 VectorType *&Entry = ElementType->getContext()
876 .pImpl->VectorTypes[std::make_pair(x&: ElementType, y&: EC)];
877
878 if (!Entry)
879 Entry = new (pImpl->Alloc) ScalableVectorType(ElementType, MinNumElts);
880 return cast<ScalableVectorType>(Val: Entry);
881}
882
883//===----------------------------------------------------------------------===//
884// PointerType Implementation
885//===----------------------------------------------------------------------===//
886
887PointerType *PointerType::get(LLVMContext &C, unsigned AddressSpace) {
888 LLVMContextImpl *CImpl = C.pImpl;
889
890 // Since AddressSpace #0 is the common case, we special case it.
891 PointerType *&Entry = AddressSpace == 0 ? CImpl->AS0PointerType
892 : CImpl->PointerTypes[AddressSpace];
893
894 if (!Entry)
895 Entry = new (CImpl->Alloc) PointerType(C, AddressSpace);
896 return Entry;
897}
898
899PointerType::PointerType(LLVMContext &C, unsigned AddrSpace)
900 : Type(C, PointerTyID) {
901 setSubclassData(AddrSpace);
902}
903
904bool PointerType::isValidElementType(Type *ElemTy) {
905 return !ElemTy->isVoidTy() && !ElemTy->isLabelTy() &&
906 !ElemTy->isMetadataTy() && !ElemTy->isTokenTy() &&
907 !ElemTy->isX86_AMXTy();
908}
909
910bool PointerType::isLoadableOrStorableType(Type *ElemTy) {
911 return isValidElementType(ElemTy) && !ElemTy->isFunctionTy();
912}
913
914//===----------------------------------------------------------------------===//
915// TargetExtType Implementation
916//===----------------------------------------------------------------------===//
917
918TargetExtType::TargetExtType(LLVMContext &C, StringRef Name,
919 ArrayRef<Type *> Types, ArrayRef<unsigned> Ints)
920 : Type(C, TargetExtTyID), Name(C.pImpl->Saver.save(S: Name)) {
921 NumContainedTys = Types.size();
922
923 // Parameter storage immediately follows the class in allocation.
924 Type **Params = reinterpret_cast<Type **>(this + 1);
925 ContainedTys = Params;
926 for (Type *T : Types)
927 *Params++ = T;
928
929 setSubclassData(Ints.size());
930 unsigned *IntParamSpace = reinterpret_cast<unsigned *>(Params);
931 IntParams = IntParamSpace;
932 for (unsigned IntParam : Ints)
933 *IntParamSpace++ = IntParam;
934}
935
936TargetExtType *TargetExtType::get(LLVMContext &C, StringRef Name,
937 ArrayRef<Type *> Types,
938 ArrayRef<unsigned> Ints) {
939 return cantFail(ValOrErr: getOrError(Context&: C, Name, Types, Ints));
940}
941
942Expected<TargetExtType *> TargetExtType::getOrError(LLVMContext &C,
943 StringRef Name,
944 ArrayRef<Type *> Types,
945 ArrayRef<unsigned> Ints) {
946 const TargetExtTypeKeyInfo::KeyTy Key(Name, Types, Ints);
947 TargetExtType *TT;
948 // Since we only want to allocate a fresh target type in case none is found
949 // and we don't want to perform two lookups (one for checking if existent and
950 // one for inserting the newly allocated one), here we instead lookup based on
951 // Key and update the reference to the target type in-place to a newly
952 // allocated one if not found.
953 auto [Iter, Inserted] = C.pImpl->TargetExtTypes.insert_as(V: nullptr, LookupKey: Key);
954 if (Inserted) {
955 // The target type was not found. Allocate one and update TargetExtTypes
956 // in-place.
957 TT = (TargetExtType *)C.pImpl->Alloc.Allocate(
958 Size: sizeof(TargetExtType) + sizeof(Type *) * Types.size() +
959 sizeof(unsigned) * Ints.size(),
960 Alignment: alignof(TargetExtType));
961 new (TT) TargetExtType(C, Name, Types, Ints);
962 *Iter = TT;
963 return checkParams(TTy: TT);
964 }
965
966 // The target type was found. Just return it.
967 return *Iter;
968}
969
970Expected<TargetExtType *> TargetExtType::checkParams(TargetExtType *TTy) {
971 // Opaque types in the AArch64 name space.
972 if (TTy->Name == "aarch64.svcount" &&
973 (TTy->getNumTypeParameters() != 0 || TTy->getNumIntParameters() != 0))
974 return createStringError(
975 Fmt: "target extension type aarch64.svcount should have no parameters");
976
977 // Opaque types in the RISC-V name space.
978 if (TTy->Name == "riscv.vector.tuple" &&
979 (TTy->getNumTypeParameters() != 1 || TTy->getNumIntParameters() != 1))
980 return createStringError(
981 Fmt: "target extension type riscv.vector.tuple should have one "
982 "type parameter and one integer parameter");
983
984 // Opaque types in the AMDGPU name space.
985 if (TTy->Name == "amdgcn.named.barrier" &&
986 (TTy->getNumTypeParameters() != 0 || TTy->getNumIntParameters() != 1)) {
987 return createStringError(Fmt: "target extension type amdgcn.named.barrier "
988 "should have no type parameters "
989 "and one integer parameter");
990 }
991 if (TTy->Name == "amdgpu.stridemark" &&
992 (TTy->getNumTypeParameters() != 0 || TTy->getNumIntParameters() > 1)) {
993 return createStringError(Fmt: "target extension type amdgpu.stridemark "
994 "should have no type parameters "
995 "and at most one integer parameter");
996 }
997
998 return TTy;
999}
1000
1001namespace {
1002struct TargetTypeInfo {
1003 Type *LayoutType;
1004 uint64_t Properties;
1005
1006 template <typename... ArgTys>
1007 TargetTypeInfo(Type *LayoutType, ArgTys... Properties)
1008 : LayoutType(LayoutType), Properties((0 | ... | Properties)) {
1009 assert((!(this->Properties & TargetExtType::CanBeVectorElement) ||
1010 LayoutType->isSized()) &&
1011 "Vector element type must be sized");
1012 }
1013};
1014} // anonymous namespace
1015
1016static TargetTypeInfo getTargetTypeInfo(const TargetExtType *Ty) {
1017 LLVMContext &C = Ty->getContext();
1018 StringRef Name = Ty->getName();
1019 if (Name == "spirv.Image" || Name == "spirv.SignedImage")
1020 return TargetTypeInfo(PointerType::get(C, AddressSpace: 0), TargetExtType::CanBeGlobal,
1021 TargetExtType::CanBeLocal);
1022 if (Name == "spirv.Type") {
1023 assert(Ty->getNumIntParameters() == 3 &&
1024 "Wrong number of parameters for spirv.Type");
1025
1026 auto Size = Ty->getIntParameter(i: 1);
1027 auto Alignment = Ty->getIntParameter(i: 2);
1028
1029 llvm::Type *LayoutType = nullptr;
1030 if (Size > 0 && Alignment > 0) {
1031 LayoutType =
1032 ArrayType::get(ElementType: Type::getIntNTy(C, N: Alignment), NumElements: Size * 8 / Alignment);
1033 } else {
1034 // LLVM expects variables that can be allocated to have an alignment and
1035 // size. Default to using a 32-bit int as the layout type if none are
1036 // present.
1037 LayoutType = Type::getInt32Ty(C);
1038 }
1039
1040 return TargetTypeInfo(LayoutType, TargetExtType::CanBeGlobal,
1041 TargetExtType::CanBeLocal);
1042 }
1043 if (Name == "spirv.IntegralConstant" || Name == "spirv.Literal")
1044 return TargetTypeInfo(Type::getVoidTy(C));
1045 if (Name == "spirv.Padding")
1046 return TargetTypeInfo(
1047 ArrayType::get(ElementType: Type::getInt8Ty(C), NumElements: Ty->getIntParameter(i: 0)),
1048 TargetExtType::CanBeGlobal);
1049 if (Name.starts_with(Prefix: "spirv.")) {
1050 if (Name.ends_with(Suffix: "TypedPointerType"))
1051 return TargetTypeInfo(PointerType::get(C, AddressSpace: 0), TargetExtType::HasZeroInit,
1052 TargetExtType::CanBeGlobal,
1053 TargetExtType::CanBeLocal,
1054 TargetExtType::CanBeVectorElement);
1055 return TargetTypeInfo(PointerType::get(C, AddressSpace: 0), TargetExtType::HasZeroInit,
1056 TargetExtType::CanBeGlobal,
1057 TargetExtType::CanBeLocal);
1058 }
1059
1060 // Opaque types in the AArch64 name space.
1061 if (Name == "aarch64.svcount")
1062 return TargetTypeInfo(ScalableVectorType::get(ElementType: Type::getInt1Ty(C), MinNumElts: 16),
1063 TargetExtType::HasZeroInit,
1064 TargetExtType::CanBeLocal);
1065
1066 // RISC-V vector tuple type. The layout is represented as the type that needs
1067 // the same number of vector registers(VREGS) as this tuple type, represented
1068 // as <vscale x (RVVBitsPerBlock * VREGS / 8) x i8>.
1069 if (Name == "riscv.vector.tuple") {
1070 unsigned TotalNumElts =
1071 std::max(a: cast<ScalableVectorType>(Val: Ty->getTypeParameter(i: 0))
1072 ->getMinNumElements(),
1073 b: RISCV::RVVBytesPerBlock) *
1074 Ty->getIntParameter(i: 0);
1075 return TargetTypeInfo(
1076 ScalableVectorType::get(ElementType: Type::getInt8Ty(C), MinNumElts: TotalNumElts),
1077 TargetExtType::CanBeLocal, TargetExtType::HasZeroInit);
1078 }
1079
1080 // DirectX resources
1081 if (Name == "dx.Padding")
1082 return TargetTypeInfo(
1083 ArrayType::get(ElementType: Type::getInt8Ty(C), NumElements: Ty->getIntParameter(i: 0)),
1084 TargetExtType::CanBeGlobal);
1085 if (Name.starts_with(Prefix: "dx."))
1086 return TargetTypeInfo(PointerType::get(C, AddressSpace: 0), TargetExtType::CanBeGlobal,
1087 TargetExtType::CanBeLocal);
1088
1089 // Opaque types in the AMDGPU name space.
1090 // NOTE: If the size of the type is changed, it must be also updated in
1091 // AMDGPUMemoryUtils.h !
1092 if (Name == "amdgcn.named.barrier") {
1093 return TargetTypeInfo(FixedVectorType::get(ElementType: Type::getInt32Ty(C), NumElts: 4),
1094 TargetExtType::CanBeGlobal);
1095 }
1096 if (Name == "amdgpu.stridemark")
1097 return TargetTypeInfo(Type::getVoidTy(C), TargetExtType::IsTokenLike);
1098
1099 // Type used to test vector element target extension property.
1100 // Can be removed once a public target extension type uses CanBeVectorElement.
1101 if (Name == "llvm.test.vectorelement") {
1102 return TargetTypeInfo(Type::getInt32Ty(C), TargetExtType::CanBeLocal,
1103 TargetExtType::CanBeVectorElement);
1104 }
1105
1106 // Opaque types in the WebAssembly name space.
1107 if (Name == "wasm.funcref" || Name == "wasm.externref")
1108 return TargetTypeInfo(PointerType::getUnqual(C), TargetExtType::HasZeroInit,
1109 TargetExtType::CanBeGlobal,
1110 TargetExtType::CanBeLocal);
1111
1112 return TargetTypeInfo(Type::getVoidTy(C));
1113}
1114
1115bool Type::isTokenLikeTy() const {
1116 if (isTokenTy())
1117 return true;
1118 if (auto *TT = dyn_cast<TargetExtType>(Val: this))
1119 return TT->hasProperty(Prop: TargetExtType::Property::IsTokenLike);
1120 return false;
1121}
1122
1123Type *TargetExtType::getLayoutType() const {
1124 return getTargetTypeInfo(Ty: this).LayoutType;
1125}
1126
1127bool TargetExtType::hasProperty(Property Prop) const {
1128 uint64_t Properties = getTargetTypeInfo(Ty: this).Properties;
1129 return (Properties & Prop) == Prop;
1130}
1131