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/SmallString.h"
17 #include "llvm/ADT/StringMap.h"
18 #include "llvm/ADT/StringRef.h"
19 #include "llvm/IR/Constant.h"
20 #include "llvm/IR/Constants.h"
21 #include "llvm/IR/DerivedTypes.h"
22 #include "llvm/IR/LLVMContext.h"
23 #include "llvm/IR/Value.h"
24 #include "llvm/Support/Casting.h"
25 #include "llvm/Support/TypeSize.h"
26 #include "llvm/Support/raw_ostream.h"
27 #include <cassert>
28 #include <utility>
29 
30 using namespace llvm;
31 
32 //===----------------------------------------------------------------------===//
33 //                         Type Class Implementation
34 //===----------------------------------------------------------------------===//
35 
36 Type *Type::getPrimitiveType(LLVMContext &C, TypeID IDNumber) {
37   switch (IDNumber) {
38   case VoidTyID      : return getVoidTy(C);
39   case HalfTyID      : return getHalfTy(C);
40   case BFloatTyID    : return getBFloatTy(C);
41   case FloatTyID     : return getFloatTy(C);
42   case DoubleTyID    : return getDoubleTy(C);
43   case X86_FP80TyID  : return getX86_FP80Ty(C);
44   case FP128TyID     : return getFP128Ty(C);
45   case PPC_FP128TyID : return getPPC_FP128Ty(C);
46   case LabelTyID     : return getLabelTy(C);
47   case MetadataTyID  : return getMetadataTy(C);
48   case X86_MMXTyID   : return getX86_MMXTy(C);
49   case X86_AMXTyID   : return getX86_AMXTy(C);
50   case TokenTyID     : return getTokenTy(C);
51   default:
52     return nullptr;
53   }
54 }
55 
56 bool Type::isIntegerTy(unsigned Bitwidth) const {
57   return isIntegerTy() && cast<IntegerType>(this)->getBitWidth() == Bitwidth;
58 }
59 
60 bool Type::isScalableTy() const {
61   if (const auto *STy = dyn_cast<StructType>(this)) {
62     SmallPtrSet<Type *, 4> Visited;
63     return STy->containsScalableVectorType(&Visited);
64   }
65   return getTypeID() == ScalableVectorTyID || isScalableTargetExtTy();
66 }
67 
68 const fltSemantics &Type::getFltSemantics() const {
69   switch (getTypeID()) {
70   case HalfTyID: return APFloat::IEEEhalf();
71   case BFloatTyID: return APFloat::BFloat();
72   case FloatTyID: return APFloat::IEEEsingle();
73   case DoubleTyID: return APFloat::IEEEdouble();
74   case X86_FP80TyID: return APFloat::x87DoubleExtended();
75   case FP128TyID: return APFloat::IEEEquad();
76   case PPC_FP128TyID: return APFloat::PPCDoubleDouble();
77   default: llvm_unreachable("Invalid floating type");
78   }
79 }
80 
81 bool Type::isIEEE() const {
82   return APFloat::getZero(getFltSemantics()).isIEEE();
83 }
84 
85 bool Type::isScalableTargetExtTy() const {
86   if (auto *TT = dyn_cast<TargetExtType>(this))
87     return isa<ScalableVectorType>(TT->getLayoutType());
88   return false;
89 }
90 
91 Type *Type::getFloatingPointTy(LLVMContext &C, const fltSemantics &S) {
92   Type *Ty;
93   if (&S == &APFloat::IEEEhalf())
94     Ty = Type::getHalfTy(C);
95   else if (&S == &APFloat::BFloat())
96     Ty = Type::getBFloatTy(C);
97   else if (&S == &APFloat::IEEEsingle())
98     Ty = Type::getFloatTy(C);
99   else if (&S == &APFloat::IEEEdouble())
100     Ty = Type::getDoubleTy(C);
101   else if (&S == &APFloat::x87DoubleExtended())
102     Ty = Type::getX86_FP80Ty(C);
103   else if (&S == &APFloat::IEEEquad())
104     Ty = Type::getFP128Ty(C);
105   else {
106     assert(&S == &APFloat::PPCDoubleDouble() && "Unknown FP format");
107     Ty = Type::getPPC_FP128Ty(C);
108   }
109   return Ty;
110 }
111 
112 bool Type::canLosslesslyBitCastTo(Type *Ty) const {
113   // Identity cast means no change so return true
114   if (this == Ty)
115     return true;
116 
117   // They are not convertible unless they are at least first class types
118   if (!this->isFirstClassType() || !Ty->isFirstClassType())
119     return false;
120 
121   // Vector -> Vector conversions are always lossless if the two vector types
122   // have the same size, otherwise not.
123   if (isa<VectorType>(this) && isa<VectorType>(Ty))
124     return getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits();
125 
126   //  64-bit fixed width vector types can be losslessly converted to x86mmx.
127   if (((isa<FixedVectorType>(this)) && Ty->isX86_MMXTy()) &&
128       getPrimitiveSizeInBits().getFixedValue() == 64)
129     return true;
130   if ((isX86_MMXTy() && isa<FixedVectorType>(Ty)) &&
131       Ty->getPrimitiveSizeInBits().getFixedValue() == 64)
132     return true;
133 
134   //  8192-bit fixed width vector types can be losslessly converted to x86amx.
135   if (((isa<FixedVectorType>(this)) && Ty->isX86_AMXTy()) &&
136       getPrimitiveSizeInBits().getFixedValue() == 8192)
137     return true;
138   if ((isX86_AMXTy() && isa<FixedVectorType>(Ty)) &&
139       Ty->getPrimitiveSizeInBits().getFixedValue() == 8192)
140     return true;
141 
142   // At this point we have only various mismatches of the first class types
143   // remaining and ptr->ptr. Just select the lossless conversions. Everything
144   // else is not lossless. Conservatively assume we can't losslessly convert
145   // between pointers with different address spaces.
146   if (auto *PTy = dyn_cast<PointerType>(this)) {
147     if (auto *OtherPTy = dyn_cast<PointerType>(Ty))
148       return PTy->getAddressSpace() == OtherPTy->getAddressSpace();
149     return false;
150   }
151   return false;  // Other types have no identity values
152 }
153 
154 bool Type::isEmptyTy() const {
155   if (auto *ATy = dyn_cast<ArrayType>(this)) {
156     unsigned NumElements = ATy->getNumElements();
157     return NumElements == 0 || ATy->getElementType()->isEmptyTy();
158   }
159 
160   if (auto *STy = dyn_cast<StructType>(this)) {
161     unsigned NumElements = STy->getNumElements();
162     for (unsigned i = 0; i < NumElements; ++i)
163       if (!STy->getElementType(i)->isEmptyTy())
164         return false;
165     return true;
166   }
167 
168   return false;
169 }
170 
171 TypeSize Type::getPrimitiveSizeInBits() const {
172   switch (getTypeID()) {
173   case Type::HalfTyID: return TypeSize::Fixed(16);
174   case Type::BFloatTyID: return TypeSize::Fixed(16);
175   case Type::FloatTyID: return TypeSize::Fixed(32);
176   case Type::DoubleTyID: return TypeSize::Fixed(64);
177   case Type::X86_FP80TyID: return TypeSize::Fixed(80);
178   case Type::FP128TyID: return TypeSize::Fixed(128);
179   case Type::PPC_FP128TyID: return TypeSize::Fixed(128);
180   case Type::X86_MMXTyID: return TypeSize::Fixed(64);
181   case Type::X86_AMXTyID: return TypeSize::Fixed(8192);
182   case Type::IntegerTyID:
183     return TypeSize::Fixed(cast<IntegerType>(this)->getBitWidth());
184   case Type::FixedVectorTyID:
185   case Type::ScalableVectorTyID: {
186     const VectorType *VTy = cast<VectorType>(this);
187     ElementCount EC = VTy->getElementCount();
188     TypeSize ETS = VTy->getElementType()->getPrimitiveSizeInBits();
189     assert(!ETS.isScalable() && "Vector type should have fixed-width elements");
190     return {ETS.getFixedValue() * EC.getKnownMinValue(), EC.isScalable()};
191   }
192   default: return TypeSize::Fixed(0);
193   }
194 }
195 
196 unsigned Type::getScalarSizeInBits() const {
197   // It is safe to assume that the scalar types have a fixed size.
198   return getScalarType()->getPrimitiveSizeInBits().getFixedValue();
199 }
200 
201 int Type::getFPMantissaWidth() const {
202   if (auto *VTy = dyn_cast<VectorType>(this))
203     return VTy->getElementType()->getFPMantissaWidth();
204   assert(isFloatingPointTy() && "Not a floating point type!");
205   if (getTypeID() == HalfTyID) return 11;
206   if (getTypeID() == BFloatTyID) return 8;
207   if (getTypeID() == FloatTyID) return 24;
208   if (getTypeID() == DoubleTyID) return 53;
209   if (getTypeID() == X86_FP80TyID) return 64;
210   if (getTypeID() == FP128TyID) return 113;
211   assert(getTypeID() == PPC_FP128TyID && "unknown fp type");
212   return -1;
213 }
214 
215 bool Type::isSizedDerivedType(SmallPtrSetImpl<Type*> *Visited) const {
216   if (auto *ATy = dyn_cast<ArrayType>(this))
217     return ATy->getElementType()->isSized(Visited);
218 
219   if (auto *VTy = dyn_cast<VectorType>(this))
220     return VTy->getElementType()->isSized(Visited);
221 
222   if (auto *TTy = dyn_cast<TargetExtType>(this))
223     return TTy->getLayoutType()->isSized(Visited);
224 
225   return cast<StructType>(this)->isSized(Visited);
226 }
227 
228 //===----------------------------------------------------------------------===//
229 //                          Primitive 'Type' data
230 //===----------------------------------------------------------------------===//
231 
232 Type *Type::getVoidTy(LLVMContext &C) { return &C.pImpl->VoidTy; }
233 Type *Type::getLabelTy(LLVMContext &C) { return &C.pImpl->LabelTy; }
234 Type *Type::getHalfTy(LLVMContext &C) { return &C.pImpl->HalfTy; }
235 Type *Type::getBFloatTy(LLVMContext &C) { return &C.pImpl->BFloatTy; }
236 Type *Type::getFloatTy(LLVMContext &C) { return &C.pImpl->FloatTy; }
237 Type *Type::getDoubleTy(LLVMContext &C) { return &C.pImpl->DoubleTy; }
238 Type *Type::getMetadataTy(LLVMContext &C) { return &C.pImpl->MetadataTy; }
239 Type *Type::getTokenTy(LLVMContext &C) { return &C.pImpl->TokenTy; }
240 Type *Type::getX86_FP80Ty(LLVMContext &C) { return &C.pImpl->X86_FP80Ty; }
241 Type *Type::getFP128Ty(LLVMContext &C) { return &C.pImpl->FP128Ty; }
242 Type *Type::getPPC_FP128Ty(LLVMContext &C) { return &C.pImpl->PPC_FP128Ty; }
243 Type *Type::getX86_MMXTy(LLVMContext &C) { return &C.pImpl->X86_MMXTy; }
244 Type *Type::getX86_AMXTy(LLVMContext &C) { return &C.pImpl->X86_AMXTy; }
245 
246 IntegerType *Type::getInt1Ty(LLVMContext &C) { return &C.pImpl->Int1Ty; }
247 IntegerType *Type::getInt8Ty(LLVMContext &C) { return &C.pImpl->Int8Ty; }
248 IntegerType *Type::getInt16Ty(LLVMContext &C) { return &C.pImpl->Int16Ty; }
249 IntegerType *Type::getInt32Ty(LLVMContext &C) { return &C.pImpl->Int32Ty; }
250 IntegerType *Type::getInt64Ty(LLVMContext &C) { return &C.pImpl->Int64Ty; }
251 IntegerType *Type::getInt128Ty(LLVMContext &C) { return &C.pImpl->Int128Ty; }
252 
253 IntegerType *Type::getIntNTy(LLVMContext &C, unsigned N) {
254   return IntegerType::get(C, N);
255 }
256 
257 PointerType *Type::getHalfPtrTy(LLVMContext &C, unsigned AS) {
258   return getHalfTy(C)->getPointerTo(AS);
259 }
260 
261 PointerType *Type::getBFloatPtrTy(LLVMContext &C, unsigned AS) {
262   return getBFloatTy(C)->getPointerTo(AS);
263 }
264 
265 PointerType *Type::getFloatPtrTy(LLVMContext &C, unsigned AS) {
266   return getFloatTy(C)->getPointerTo(AS);
267 }
268 
269 PointerType *Type::getDoublePtrTy(LLVMContext &C, unsigned AS) {
270   return getDoubleTy(C)->getPointerTo(AS);
271 }
272 
273 PointerType *Type::getX86_FP80PtrTy(LLVMContext &C, unsigned AS) {
274   return getX86_FP80Ty(C)->getPointerTo(AS);
275 }
276 
277 PointerType *Type::getFP128PtrTy(LLVMContext &C, unsigned AS) {
278   return getFP128Ty(C)->getPointerTo(AS);
279 }
280 
281 PointerType *Type::getPPC_FP128PtrTy(LLVMContext &C, unsigned AS) {
282   return getPPC_FP128Ty(C)->getPointerTo(AS);
283 }
284 
285 PointerType *Type::getX86_MMXPtrTy(LLVMContext &C, unsigned AS) {
286   return getX86_MMXTy(C)->getPointerTo(AS);
287 }
288 
289 PointerType *Type::getX86_AMXPtrTy(LLVMContext &C, unsigned AS) {
290   return getX86_AMXTy(C)->getPointerTo(AS);
291 }
292 
293 PointerType *Type::getIntNPtrTy(LLVMContext &C, unsigned N, unsigned AS) {
294   return getIntNTy(C, N)->getPointerTo(AS);
295 }
296 
297 PointerType *Type::getInt1PtrTy(LLVMContext &C, unsigned AS) {
298   return getInt1Ty(C)->getPointerTo(AS);
299 }
300 
301 PointerType *Type::getInt8PtrTy(LLVMContext &C, unsigned AS) {
302   return getInt8Ty(C)->getPointerTo(AS);
303 }
304 
305 PointerType *Type::getInt16PtrTy(LLVMContext &C, unsigned AS) {
306   return getInt16Ty(C)->getPointerTo(AS);
307 }
308 
309 PointerType *Type::getInt32PtrTy(LLVMContext &C, unsigned AS) {
310   return getInt32Ty(C)->getPointerTo(AS);
311 }
312 
313 PointerType *Type::getInt64PtrTy(LLVMContext &C, unsigned AS) {
314   return getInt64Ty(C)->getPointerTo(AS);
315 }
316 
317 Type *Type::getWasm_ExternrefTy(LLVMContext &C) {
318   // opaque pointer in addrspace(10)
319   static PointerType *Ty = PointerType::get(C, 10);
320   return Ty;
321 }
322 
323 Type *Type::getWasm_FuncrefTy(LLVMContext &C) {
324   // opaque pointer in addrspace(20)
325   static PointerType *Ty = PointerType::get(C, 20);
326   return Ty;
327 }
328 
329 //===----------------------------------------------------------------------===//
330 //                       IntegerType Implementation
331 //===----------------------------------------------------------------------===//
332 
333 IntegerType *IntegerType::get(LLVMContext &C, unsigned NumBits) {
334   assert(NumBits >= MIN_INT_BITS && "bitwidth too small");
335   assert(NumBits <= MAX_INT_BITS && "bitwidth too large");
336 
337   // Check for the built-in integer types
338   switch (NumBits) {
339   case   1: return cast<IntegerType>(Type::getInt1Ty(C));
340   case   8: return cast<IntegerType>(Type::getInt8Ty(C));
341   case  16: return cast<IntegerType>(Type::getInt16Ty(C));
342   case  32: return cast<IntegerType>(Type::getInt32Ty(C));
343   case  64: return cast<IntegerType>(Type::getInt64Ty(C));
344   case 128: return cast<IntegerType>(Type::getInt128Ty(C));
345   default:
346     break;
347   }
348 
349   IntegerType *&Entry = C.pImpl->IntegerTypes[NumBits];
350 
351   if (!Entry)
352     Entry = new (C.pImpl->Alloc) IntegerType(C, NumBits);
353 
354   return Entry;
355 }
356 
357 APInt IntegerType::getMask() const { return APInt::getAllOnes(getBitWidth()); }
358 
359 //===----------------------------------------------------------------------===//
360 //                       FunctionType Implementation
361 //===----------------------------------------------------------------------===//
362 
363 FunctionType::FunctionType(Type *Result, ArrayRef<Type*> Params,
364                            bool IsVarArgs)
365   : Type(Result->getContext(), FunctionTyID) {
366   Type **SubTys = reinterpret_cast<Type**>(this+1);
367   assert(isValidReturnType(Result) && "invalid return type for function");
368   setSubclassData(IsVarArgs);
369 
370   SubTys[0] = Result;
371 
372   for (unsigned i = 0, e = Params.size(); i != e; ++i) {
373     assert(isValidArgumentType(Params[i]) &&
374            "Not a valid type for function argument!");
375     SubTys[i+1] = Params[i];
376   }
377 
378   ContainedTys = SubTys;
379   NumContainedTys = Params.size() + 1; // + 1 for result type
380 }
381 
382 // This is the factory function for the FunctionType class.
383 FunctionType *FunctionType::get(Type *ReturnType,
384                                 ArrayRef<Type*> Params, bool isVarArg) {
385   LLVMContextImpl *pImpl = ReturnType->getContext().pImpl;
386   const FunctionTypeKeyInfo::KeyTy Key(ReturnType, Params, isVarArg);
387   FunctionType *FT;
388   // Since we only want to allocate a fresh function type in case none is found
389   // and we don't want to perform two lookups (one for checking if existent and
390   // one for inserting the newly allocated one), here we instead lookup based on
391   // Key and update the reference to the function type in-place to a newly
392   // allocated one if not found.
393   auto Insertion = pImpl->FunctionTypes.insert_as(nullptr, Key);
394   if (Insertion.second) {
395     // The function type was not found. Allocate one and update FunctionTypes
396     // in-place.
397     FT = (FunctionType *)pImpl->Alloc.Allocate(
398         sizeof(FunctionType) + sizeof(Type *) * (Params.size() + 1),
399         alignof(FunctionType));
400     new (FT) FunctionType(ReturnType, Params, isVarArg);
401     *Insertion.first = FT;
402   } else {
403     // The function type was found. Just return it.
404     FT = *Insertion.first;
405   }
406   return FT;
407 }
408 
409 FunctionType *FunctionType::get(Type *Result, bool isVarArg) {
410   return get(Result, std::nullopt, isVarArg);
411 }
412 
413 bool FunctionType::isValidReturnType(Type *RetTy) {
414   return !RetTy->isFunctionTy() && !RetTy->isLabelTy() &&
415   !RetTy->isMetadataTy();
416 }
417 
418 bool FunctionType::isValidArgumentType(Type *ArgTy) {
419   return ArgTy->isFirstClassType();
420 }
421 
422 //===----------------------------------------------------------------------===//
423 //                       StructType Implementation
424 //===----------------------------------------------------------------------===//
425 
426 // Primitive Constructors.
427 
428 StructType *StructType::get(LLVMContext &Context, ArrayRef<Type*> ETypes,
429                             bool isPacked) {
430   LLVMContextImpl *pImpl = Context.pImpl;
431   const AnonStructTypeKeyInfo::KeyTy Key(ETypes, isPacked);
432 
433   StructType *ST;
434   // Since we only want to allocate a fresh struct type in case none is found
435   // and we don't want to perform two lookups (one for checking if existent and
436   // one for inserting the newly allocated one), here we instead lookup based on
437   // Key and update the reference to the struct type in-place to a newly
438   // allocated one if not found.
439   auto Insertion = pImpl->AnonStructTypes.insert_as(nullptr, Key);
440   if (Insertion.second) {
441     // The struct type was not found. Allocate one and update AnonStructTypes
442     // in-place.
443     ST = new (Context.pImpl->Alloc) StructType(Context);
444     ST->setSubclassData(SCDB_IsLiteral);  // Literal struct.
445     ST->setBody(ETypes, isPacked);
446     *Insertion.first = ST;
447   } else {
448     // The struct type was found. Just return it.
449     ST = *Insertion.first;
450   }
451 
452   return ST;
453 }
454 
455 bool StructType::containsScalableVectorType(
456     SmallPtrSetImpl<Type *> *Visited) const {
457   if ((getSubclassData() & SCDB_ContainsScalableVector) != 0)
458     return true;
459 
460   if ((getSubclassData() & SCDB_NotContainsScalableVector) != 0)
461     return false;
462 
463   if (Visited && !Visited->insert(const_cast<StructType *>(this)).second)
464     return false;
465 
466   for (Type *Ty : elements()) {
467     if (isa<ScalableVectorType>(Ty)) {
468       const_cast<StructType *>(this)->setSubclassData(
469           getSubclassData() | SCDB_ContainsScalableVector);
470       return true;
471     }
472     if (auto *STy = dyn_cast<StructType>(Ty)) {
473       if (STy->containsScalableVectorType(Visited)) {
474         const_cast<StructType *>(this)->setSubclassData(
475             getSubclassData() | SCDB_ContainsScalableVector);
476         return true;
477       }
478     }
479   }
480 
481   // For structures that are opaque, return false but do not set the
482   // SCDB_NotContainsScalableVector flag since it may gain scalable vector type
483   // when it becomes non-opaque.
484   if (!isOpaque())
485     const_cast<StructType *>(this)->setSubclassData(
486         getSubclassData() | SCDB_NotContainsScalableVector);
487   return false;
488 }
489 
490 bool StructType::containsHomogeneousScalableVectorTypes() const {
491   Type *FirstTy = getNumElements() > 0 ? elements()[0] : nullptr;
492   if (!FirstTy || !isa<ScalableVectorType>(FirstTy))
493     return false;
494   for (Type *Ty : elements())
495     if (Ty != FirstTy)
496       return false;
497   return true;
498 }
499 
500 void StructType::setBody(ArrayRef<Type*> Elements, bool isPacked) {
501   assert(isOpaque() && "Struct body already set!");
502 
503   setSubclassData(getSubclassData() | SCDB_HasBody);
504   if (isPacked)
505     setSubclassData(getSubclassData() | SCDB_Packed);
506 
507   NumContainedTys = Elements.size();
508 
509   if (Elements.empty()) {
510     ContainedTys = nullptr;
511     return;
512   }
513 
514   ContainedTys = Elements.copy(getContext().pImpl->Alloc).data();
515 }
516 
517 void StructType::setName(StringRef Name) {
518   if (Name == getName()) return;
519 
520   StringMap<StructType *> &SymbolTable = getContext().pImpl->NamedStructTypes;
521 
522   using EntryTy = StringMap<StructType *>::MapEntryTy;
523 
524   // If this struct already had a name, remove its symbol table entry. Don't
525   // delete the data yet because it may be part of the new name.
526   if (SymbolTableEntry)
527     SymbolTable.remove((EntryTy *)SymbolTableEntry);
528 
529   // If this is just removing the name, we're done.
530   if (Name.empty()) {
531     if (SymbolTableEntry) {
532       // Delete the old string data.
533       ((EntryTy *)SymbolTableEntry)->Destroy(SymbolTable.getAllocator());
534       SymbolTableEntry = nullptr;
535     }
536     return;
537   }
538 
539   // Look up the entry for the name.
540   auto IterBool =
541       getContext().pImpl->NamedStructTypes.insert(std::make_pair(Name, this));
542 
543   // While we have a name collision, try a random rename.
544   if (!IterBool.second) {
545     SmallString<64> TempStr(Name);
546     TempStr.push_back('.');
547     raw_svector_ostream TmpStream(TempStr);
548     unsigned NameSize = Name.size();
549 
550     do {
551       TempStr.resize(NameSize + 1);
552       TmpStream << getContext().pImpl->NamedStructTypesUniqueID++;
553 
554       IterBool = getContext().pImpl->NamedStructTypes.insert(
555           std::make_pair(TmpStream.str(), this));
556     } while (!IterBool.second);
557   }
558 
559   // Delete the old string data.
560   if (SymbolTableEntry)
561     ((EntryTy *)SymbolTableEntry)->Destroy(SymbolTable.getAllocator());
562   SymbolTableEntry = &*IterBool.first;
563 }
564 
565 //===----------------------------------------------------------------------===//
566 // StructType Helper functions.
567 
568 StructType *StructType::create(LLVMContext &Context, StringRef Name) {
569   StructType *ST = new (Context.pImpl->Alloc) StructType(Context);
570   if (!Name.empty())
571     ST->setName(Name);
572   return ST;
573 }
574 
575 StructType *StructType::get(LLVMContext &Context, bool isPacked) {
576   return get(Context, std::nullopt, isPacked);
577 }
578 
579 StructType *StructType::create(LLVMContext &Context, ArrayRef<Type*> Elements,
580                                StringRef Name, bool isPacked) {
581   StructType *ST = create(Context, Name);
582   ST->setBody(Elements, isPacked);
583   return ST;
584 }
585 
586 StructType *StructType::create(LLVMContext &Context, ArrayRef<Type*> Elements) {
587   return create(Context, Elements, StringRef());
588 }
589 
590 StructType *StructType::create(LLVMContext &Context) {
591   return create(Context, StringRef());
592 }
593 
594 StructType *StructType::create(ArrayRef<Type*> Elements, StringRef Name,
595                                bool isPacked) {
596   assert(!Elements.empty() &&
597          "This method may not be invoked with an empty list");
598   return create(Elements[0]->getContext(), Elements, Name, isPacked);
599 }
600 
601 StructType *StructType::create(ArrayRef<Type*> Elements) {
602   assert(!Elements.empty() &&
603          "This method may not be invoked with an empty list");
604   return create(Elements[0]->getContext(), Elements, StringRef());
605 }
606 
607 bool StructType::isSized(SmallPtrSetImpl<Type*> *Visited) const {
608   if ((getSubclassData() & SCDB_IsSized) != 0)
609     return true;
610   if (isOpaque())
611     return false;
612 
613   if (Visited && !Visited->insert(const_cast<StructType*>(this)).second)
614     return false;
615 
616   // Okay, our struct is sized if all of the elements are, but if one of the
617   // elements is opaque, the struct isn't sized *yet*, but may become sized in
618   // the future, so just bail out without caching.
619   // The ONLY special case inside a struct that is considered sized is when the
620   // elements are homogeneous of a scalable vector type.
621   if (containsHomogeneousScalableVectorTypes()) {
622     const_cast<StructType *>(this)->setSubclassData(getSubclassData() |
623                                                     SCDB_IsSized);
624     return true;
625   }
626   for (Type *Ty : elements()) {
627     // If the struct contains a scalable vector type, don't consider it sized.
628     // This prevents it from being used in loads/stores/allocas/GEPs. The ONLY
629     // special case right now is a structure of homogenous scalable vector
630     // types and is handled by the if-statement before this for-loop.
631     if (Ty->isScalableTy())
632       return false;
633     if (!Ty->isSized(Visited))
634       return false;
635   }
636 
637   // Here we cheat a bit and cast away const-ness. The goal is to memoize when
638   // we find a sized type, as types can only move from opaque to sized, not the
639   // other way.
640   const_cast<StructType*>(this)->setSubclassData(
641     getSubclassData() | SCDB_IsSized);
642   return true;
643 }
644 
645 StringRef StructType::getName() const {
646   assert(!isLiteral() && "Literal structs never have names");
647   if (!SymbolTableEntry) return StringRef();
648 
649   return ((StringMapEntry<StructType*> *)SymbolTableEntry)->getKey();
650 }
651 
652 bool StructType::isValidElementType(Type *ElemTy) {
653   return !ElemTy->isVoidTy() && !ElemTy->isLabelTy() &&
654          !ElemTy->isMetadataTy() && !ElemTy->isFunctionTy() &&
655          !ElemTy->isTokenTy();
656 }
657 
658 bool StructType::isLayoutIdentical(StructType *Other) const {
659   if (this == Other) return true;
660 
661   if (isPacked() != Other->isPacked())
662     return false;
663 
664   return elements() == Other->elements();
665 }
666 
667 Type *StructType::getTypeAtIndex(const Value *V) const {
668   unsigned Idx = (unsigned)cast<Constant>(V)->getUniqueInteger().getZExtValue();
669   assert(indexValid(Idx) && "Invalid structure index!");
670   return getElementType(Idx);
671 }
672 
673 bool StructType::indexValid(const Value *V) const {
674   // Structure indexes require (vectors of) 32-bit integer constants.  In the
675   // vector case all of the indices must be equal.
676   if (!V->getType()->isIntOrIntVectorTy(32))
677     return false;
678   if (isa<ScalableVectorType>(V->getType()))
679     return false;
680   const Constant *C = dyn_cast<Constant>(V);
681   if (C && V->getType()->isVectorTy())
682     C = C->getSplatValue();
683   const ConstantInt *CU = dyn_cast_or_null<ConstantInt>(C);
684   return CU && CU->getZExtValue() < getNumElements();
685 }
686 
687 StructType *StructType::getTypeByName(LLVMContext &C, StringRef Name) {
688   return C.pImpl->NamedStructTypes.lookup(Name);
689 }
690 
691 //===----------------------------------------------------------------------===//
692 //                           ArrayType Implementation
693 //===----------------------------------------------------------------------===//
694 
695 ArrayType::ArrayType(Type *ElType, uint64_t NumEl)
696     : Type(ElType->getContext(), ArrayTyID), ContainedType(ElType),
697       NumElements(NumEl) {
698   ContainedTys = &ContainedType;
699   NumContainedTys = 1;
700 }
701 
702 ArrayType *ArrayType::get(Type *ElementType, uint64_t NumElements) {
703   assert(isValidElementType(ElementType) && "Invalid type for array element!");
704 
705   LLVMContextImpl *pImpl = ElementType->getContext().pImpl;
706   ArrayType *&Entry =
707     pImpl->ArrayTypes[std::make_pair(ElementType, NumElements)];
708 
709   if (!Entry)
710     Entry = new (pImpl->Alloc) ArrayType(ElementType, NumElements);
711   return Entry;
712 }
713 
714 bool ArrayType::isValidElementType(Type *ElemTy) {
715   return !ElemTy->isVoidTy() && !ElemTy->isLabelTy() &&
716          !ElemTy->isMetadataTy() && !ElemTy->isFunctionTy() &&
717          !ElemTy->isTokenTy() && !ElemTy->isX86_AMXTy() &&
718          !isa<ScalableVectorType>(ElemTy);
719 }
720 
721 //===----------------------------------------------------------------------===//
722 //                          VectorType Implementation
723 //===----------------------------------------------------------------------===//
724 
725 VectorType::VectorType(Type *ElType, unsigned EQ, Type::TypeID TID)
726     : Type(ElType->getContext(), TID), ContainedType(ElType),
727       ElementQuantity(EQ) {
728   ContainedTys = &ContainedType;
729   NumContainedTys = 1;
730 }
731 
732 VectorType *VectorType::get(Type *ElementType, ElementCount EC) {
733   if (EC.isScalable())
734     return ScalableVectorType::get(ElementType, EC.getKnownMinValue());
735   else
736     return FixedVectorType::get(ElementType, EC.getKnownMinValue());
737 }
738 
739 bool VectorType::isValidElementType(Type *ElemTy) {
740   return ElemTy->isIntegerTy() || ElemTy->isFloatingPointTy() ||
741          ElemTy->isPointerTy() || ElemTy->getTypeID() == TypedPointerTyID;
742 }
743 
744 //===----------------------------------------------------------------------===//
745 //                        FixedVectorType Implementation
746 //===----------------------------------------------------------------------===//
747 
748 FixedVectorType *FixedVectorType::get(Type *ElementType, unsigned NumElts) {
749   assert(NumElts > 0 && "#Elements of a VectorType must be greater than 0");
750   assert(isValidElementType(ElementType) && "Element type of a VectorType must "
751                                             "be an integer, floating point, or "
752                                             "pointer type.");
753 
754   auto EC = ElementCount::getFixed(NumElts);
755 
756   LLVMContextImpl *pImpl = ElementType->getContext().pImpl;
757   VectorType *&Entry = ElementType->getContext()
758                            .pImpl->VectorTypes[std::make_pair(ElementType, EC)];
759 
760   if (!Entry)
761     Entry = new (pImpl->Alloc) FixedVectorType(ElementType, NumElts);
762   return cast<FixedVectorType>(Entry);
763 }
764 
765 //===----------------------------------------------------------------------===//
766 //                       ScalableVectorType Implementation
767 //===----------------------------------------------------------------------===//
768 
769 ScalableVectorType *ScalableVectorType::get(Type *ElementType,
770                                             unsigned MinNumElts) {
771   assert(MinNumElts > 0 && "#Elements of a VectorType must be greater than 0");
772   assert(isValidElementType(ElementType) && "Element type of a VectorType must "
773                                             "be an integer, floating point, or "
774                                             "pointer type.");
775 
776   auto EC = ElementCount::getScalable(MinNumElts);
777 
778   LLVMContextImpl *pImpl = ElementType->getContext().pImpl;
779   VectorType *&Entry = ElementType->getContext()
780                            .pImpl->VectorTypes[std::make_pair(ElementType, EC)];
781 
782   if (!Entry)
783     Entry = new (pImpl->Alloc) ScalableVectorType(ElementType, MinNumElts);
784   return cast<ScalableVectorType>(Entry);
785 }
786 
787 //===----------------------------------------------------------------------===//
788 //                         PointerType Implementation
789 //===----------------------------------------------------------------------===//
790 
791 PointerType *PointerType::get(Type *EltTy, unsigned AddressSpace) {
792   assert(EltTy && "Can't get a pointer to <null> type!");
793   assert(isValidElementType(EltTy) && "Invalid type for pointer element!");
794 
795   // Automatically convert typed pointers to opaque pointers.
796   return get(EltTy->getContext(), AddressSpace);
797 }
798 
799 PointerType *PointerType::get(LLVMContext &C, unsigned AddressSpace) {
800   LLVMContextImpl *CImpl = C.pImpl;
801 
802   // Since AddressSpace #0 is the common case, we special case it.
803   PointerType *&Entry = AddressSpace == 0 ? CImpl->AS0PointerType
804                                           : CImpl->PointerTypes[AddressSpace];
805 
806   if (!Entry)
807     Entry = new (CImpl->Alloc) PointerType(C, AddressSpace);
808   return Entry;
809 }
810 
811 PointerType::PointerType(LLVMContext &C, unsigned AddrSpace)
812     : Type(C, PointerTyID) {
813   setSubclassData(AddrSpace);
814 }
815 
816 PointerType *Type::getPointerTo(unsigned AddrSpace) const {
817   return PointerType::get(const_cast<Type*>(this), AddrSpace);
818 }
819 
820 bool PointerType::isValidElementType(Type *ElemTy) {
821   return !ElemTy->isVoidTy() && !ElemTy->isLabelTy() &&
822          !ElemTy->isMetadataTy() && !ElemTy->isTokenTy() &&
823          !ElemTy->isX86_AMXTy();
824 }
825 
826 bool PointerType::isLoadableOrStorableType(Type *ElemTy) {
827   return isValidElementType(ElemTy) && !ElemTy->isFunctionTy();
828 }
829 
830 //===----------------------------------------------------------------------===//
831 //                       TargetExtType Implementation
832 //===----------------------------------------------------------------------===//
833 
834 TargetExtType::TargetExtType(LLVMContext &C, StringRef Name,
835                              ArrayRef<Type *> Types, ArrayRef<unsigned> Ints)
836     : Type(C, TargetExtTyID), Name(C.pImpl->Saver.save(Name)) {
837   NumContainedTys = Types.size();
838 
839   // Parameter storage immediately follows the class in allocation.
840   Type **Params = reinterpret_cast<Type **>(this + 1);
841   ContainedTys = Params;
842   for (Type *T : Types)
843     *Params++ = T;
844 
845   setSubclassData(Ints.size());
846   unsigned *IntParamSpace = reinterpret_cast<unsigned *>(Params);
847   IntParams = IntParamSpace;
848   for (unsigned IntParam : Ints)
849     *IntParamSpace++ = IntParam;
850 }
851 
852 TargetExtType *TargetExtType::get(LLVMContext &C, StringRef Name,
853                                   ArrayRef<Type *> Types,
854                                   ArrayRef<unsigned> Ints) {
855   const TargetExtTypeKeyInfo::KeyTy Key(Name, Types, Ints);
856   TargetExtType *TT;
857   // Since we only want to allocate a fresh target type in case none is found
858   // and we don't want to perform two lookups (one for checking if existent and
859   // one for inserting the newly allocated one), here we instead lookup based on
860   // Key and update the reference to the target type in-place to a newly
861   // allocated one if not found.
862   auto Insertion = C.pImpl->TargetExtTypes.insert_as(nullptr, Key);
863   if (Insertion.second) {
864     // The target type was not found. Allocate one and update TargetExtTypes
865     // in-place.
866     TT = (TargetExtType *)C.pImpl->Alloc.Allocate(
867         sizeof(TargetExtType) + sizeof(Type *) * Types.size() +
868             sizeof(unsigned) * Ints.size(),
869         alignof(TargetExtType));
870     new (TT) TargetExtType(C, Name, Types, Ints);
871     *Insertion.first = TT;
872   } else {
873     // The target type was found. Just return it.
874     TT = *Insertion.first;
875   }
876   return TT;
877 }
878 
879 namespace {
880 struct TargetTypeInfo {
881   Type *LayoutType;
882   uint64_t Properties;
883 
884   template <typename... ArgTys>
885   TargetTypeInfo(Type *LayoutType, ArgTys... Properties)
886       : LayoutType(LayoutType), Properties((0 | ... | Properties)) {}
887 };
888 } // anonymous namespace
889 
890 static TargetTypeInfo getTargetTypeInfo(const TargetExtType *Ty) {
891   LLVMContext &C = Ty->getContext();
892   StringRef Name = Ty->getName();
893   if (Name.startswith("spirv."))
894     return TargetTypeInfo(Type::getInt8PtrTy(C, 0), TargetExtType::HasZeroInit,
895                           TargetExtType::CanBeGlobal);
896 
897   // Opaque types in the AArch64 name space.
898   if (Name == "aarch64.svcount")
899     return TargetTypeInfo(ScalableVectorType::get(Type::getInt1Ty(C), 16));
900 
901   return TargetTypeInfo(Type::getVoidTy(C));
902 }
903 
904 Type *TargetExtType::getLayoutType() const {
905   return getTargetTypeInfo(this).LayoutType;
906 }
907 
908 bool TargetExtType::hasProperty(Property Prop) const {
909   uint64_t Properties = getTargetTypeInfo(this).Properties;
910   return (Properties & Prop) == Prop;
911 }
912