xref: /llvm-project-15.0.7/llvm/lib/IR/Type.cpp (revision abb4cd3e)
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/None.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/Module.h"
25 #include "llvm/IR/Value.h"
26 #include "llvm/Support/Casting.h"
27 #include "llvm/Support/MathExtras.h"
28 #include "llvm/Support/raw_ostream.h"
29 #include "llvm/Support/TypeSize.h"
30 #include <cassert>
31 #include <utility>
32 
33 using namespace llvm;
34 
35 //===----------------------------------------------------------------------===//
36 //                         Type Class Implementation
37 //===----------------------------------------------------------------------===//
38 
39 Type *Type::getPrimitiveType(LLVMContext &C, TypeID IDNumber) {
40   switch (IDNumber) {
41   case VoidTyID      : return getVoidTy(C);
42   case HalfTyID      : return getHalfTy(C);
43   case BFloatTyID    : return getBFloatTy(C);
44   case FloatTyID     : return getFloatTy(C);
45   case DoubleTyID    : return getDoubleTy(C);
46   case X86_FP80TyID  : return getX86_FP80Ty(C);
47   case FP128TyID     : return getFP128Ty(C);
48   case PPC_FP128TyID : return getPPC_FP128Ty(C);
49   case LabelTyID     : return getLabelTy(C);
50   case MetadataTyID  : return getMetadataTy(C);
51   case X86_MMXTyID   : return getX86_MMXTy(C);
52   case X86_AMXTyID   : return getX86_AMXTy(C);
53   case TokenTyID     : return getTokenTy(C);
54   default:
55     return nullptr;
56   }
57 }
58 
59 bool Type::isIntegerTy(unsigned Bitwidth) const {
60   return isIntegerTy() && cast<IntegerType>(this)->getBitWidth() == Bitwidth;
61 }
62 
63 bool Type::canLosslesslyBitCastTo(Type *Ty) const {
64   // Identity cast means no change so return true
65   if (this == Ty)
66     return true;
67 
68   // They are not convertible unless they are at least first class types
69   if (!this->isFirstClassType() || !Ty->isFirstClassType())
70     return false;
71 
72   // Vector -> Vector conversions are always lossless if the two vector types
73   // have the same size, otherwise not.
74   if (isa<VectorType>(this) && isa<VectorType>(Ty))
75     return getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits();
76 
77   //  64-bit fixed width vector types can be losslessly converted to x86mmx.
78   if (((isa<FixedVectorType>(this)) && Ty->isX86_MMXTy()) &&
79       getPrimitiveSizeInBits().getFixedSize() == 64)
80     return true;
81   if ((isX86_MMXTy() && isa<FixedVectorType>(Ty)) &&
82       Ty->getPrimitiveSizeInBits().getFixedSize() == 64)
83     return true;
84 
85   //  8192-bit fixed width vector types can be losslessly converted to x86amx.
86   if (((isa<FixedVectorType>(this)) && Ty->isX86_AMXTy()) &&
87       getPrimitiveSizeInBits().getFixedSize() == 8192)
88     return true;
89   if ((isX86_AMXTy() && isa<FixedVectorType>(Ty)) &&
90       Ty->getPrimitiveSizeInBits().getFixedSize() == 8192)
91     return true;
92 
93   // At this point we have only various mismatches of the first class types
94   // remaining and ptr->ptr. Just select the lossless conversions. Everything
95   // else is not lossless. Conservatively assume we can't losslessly convert
96   // between pointers with different address spaces.
97   if (auto *PTy = dyn_cast<PointerType>(this)) {
98     if (auto *OtherPTy = dyn_cast<PointerType>(Ty))
99       return PTy->getAddressSpace() == OtherPTy->getAddressSpace();
100     return false;
101   }
102   return false;  // Other types have no identity values
103 }
104 
105 bool Type::isEmptyTy() const {
106   if (auto *ATy = dyn_cast<ArrayType>(this)) {
107     unsigned NumElements = ATy->getNumElements();
108     return NumElements == 0 || ATy->getElementType()->isEmptyTy();
109   }
110 
111   if (auto *STy = dyn_cast<StructType>(this)) {
112     unsigned NumElements = STy->getNumElements();
113     for (unsigned i = 0; i < NumElements; ++i)
114       if (!STy->getElementType(i)->isEmptyTy())
115         return false;
116     return true;
117   }
118 
119   return false;
120 }
121 
122 TypeSize Type::getPrimitiveSizeInBits() const {
123   switch (getTypeID()) {
124   case Type::HalfTyID: return TypeSize::Fixed(16);
125   case Type::BFloatTyID: return TypeSize::Fixed(16);
126   case Type::FloatTyID: return TypeSize::Fixed(32);
127   case Type::DoubleTyID: return TypeSize::Fixed(64);
128   case Type::X86_FP80TyID: return TypeSize::Fixed(80);
129   case Type::FP128TyID: return TypeSize::Fixed(128);
130   case Type::PPC_FP128TyID: return TypeSize::Fixed(128);
131   case Type::X86_MMXTyID: return TypeSize::Fixed(64);
132   case Type::X86_AMXTyID: return TypeSize::Fixed(8192);
133   case Type::IntegerTyID:
134     return TypeSize::Fixed(cast<IntegerType>(this)->getBitWidth());
135   case Type::FixedVectorTyID:
136   case Type::ScalableVectorTyID: {
137     const VectorType *VTy = cast<VectorType>(this);
138     ElementCount EC = VTy->getElementCount();
139     TypeSize ETS = VTy->getElementType()->getPrimitiveSizeInBits();
140     assert(!ETS.isScalable() && "Vector type should have fixed-width elements");
141     return {ETS.getFixedSize() * EC.getKnownMinValue(), EC.isScalable()};
142   }
143   default: return TypeSize::Fixed(0);
144   }
145 }
146 
147 unsigned Type::getScalarSizeInBits() const {
148   // It is safe to assume that the scalar types have a fixed size.
149   return getScalarType()->getPrimitiveSizeInBits().getFixedSize();
150 }
151 
152 int Type::getFPMantissaWidth() const {
153   if (auto *VTy = dyn_cast<VectorType>(this))
154     return VTy->getElementType()->getFPMantissaWidth();
155   assert(isFloatingPointTy() && "Not a floating point type!");
156   if (getTypeID() == HalfTyID) return 11;
157   if (getTypeID() == BFloatTyID) return 8;
158   if (getTypeID() == FloatTyID) return 24;
159   if (getTypeID() == DoubleTyID) return 53;
160   if (getTypeID() == X86_FP80TyID) return 64;
161   if (getTypeID() == FP128TyID) return 113;
162   assert(getTypeID() == PPC_FP128TyID && "unknown fp type");
163   return -1;
164 }
165 
166 bool Type::isSizedDerivedType(SmallPtrSetImpl<Type*> *Visited) const {
167   if (auto *ATy = dyn_cast<ArrayType>(this))
168     return ATy->getElementType()->isSized(Visited);
169 
170   if (auto *VTy = dyn_cast<VectorType>(this))
171     return VTy->getElementType()->isSized(Visited);
172 
173   return cast<StructType>(this)->isSized(Visited);
174 }
175 
176 //===----------------------------------------------------------------------===//
177 //                          Primitive 'Type' data
178 //===----------------------------------------------------------------------===//
179 
180 Type *Type::getVoidTy(LLVMContext &C) { return &C.pImpl->VoidTy; }
181 Type *Type::getLabelTy(LLVMContext &C) { return &C.pImpl->LabelTy; }
182 Type *Type::getHalfTy(LLVMContext &C) { return &C.pImpl->HalfTy; }
183 Type *Type::getBFloatTy(LLVMContext &C) { return &C.pImpl->BFloatTy; }
184 Type *Type::getFloatTy(LLVMContext &C) { return &C.pImpl->FloatTy; }
185 Type *Type::getDoubleTy(LLVMContext &C) { return &C.pImpl->DoubleTy; }
186 Type *Type::getMetadataTy(LLVMContext &C) { return &C.pImpl->MetadataTy; }
187 Type *Type::getTokenTy(LLVMContext &C) { return &C.pImpl->TokenTy; }
188 Type *Type::getX86_FP80Ty(LLVMContext &C) { return &C.pImpl->X86_FP80Ty; }
189 Type *Type::getFP128Ty(LLVMContext &C) { return &C.pImpl->FP128Ty; }
190 Type *Type::getPPC_FP128Ty(LLVMContext &C) { return &C.pImpl->PPC_FP128Ty; }
191 Type *Type::getX86_MMXTy(LLVMContext &C) { return &C.pImpl->X86_MMXTy; }
192 Type *Type::getX86_AMXTy(LLVMContext &C) { return &C.pImpl->X86_AMXTy; }
193 
194 IntegerType *Type::getInt1Ty(LLVMContext &C) { return &C.pImpl->Int1Ty; }
195 IntegerType *Type::getInt8Ty(LLVMContext &C) { return &C.pImpl->Int8Ty; }
196 IntegerType *Type::getInt16Ty(LLVMContext &C) { return &C.pImpl->Int16Ty; }
197 IntegerType *Type::getInt32Ty(LLVMContext &C) { return &C.pImpl->Int32Ty; }
198 IntegerType *Type::getInt64Ty(LLVMContext &C) { return &C.pImpl->Int64Ty; }
199 IntegerType *Type::getInt128Ty(LLVMContext &C) { return &C.pImpl->Int128Ty; }
200 
201 IntegerType *Type::getIntNTy(LLVMContext &C, unsigned N) {
202   return IntegerType::get(C, N);
203 }
204 
205 PointerType *Type::getHalfPtrTy(LLVMContext &C, unsigned AS) {
206   return getHalfTy(C)->getPointerTo(AS);
207 }
208 
209 PointerType *Type::getBFloatPtrTy(LLVMContext &C, unsigned AS) {
210   return getBFloatTy(C)->getPointerTo(AS);
211 }
212 
213 PointerType *Type::getFloatPtrTy(LLVMContext &C, unsigned AS) {
214   return getFloatTy(C)->getPointerTo(AS);
215 }
216 
217 PointerType *Type::getDoublePtrTy(LLVMContext &C, unsigned AS) {
218   return getDoubleTy(C)->getPointerTo(AS);
219 }
220 
221 PointerType *Type::getX86_FP80PtrTy(LLVMContext &C, unsigned AS) {
222   return getX86_FP80Ty(C)->getPointerTo(AS);
223 }
224 
225 PointerType *Type::getFP128PtrTy(LLVMContext &C, unsigned AS) {
226   return getFP128Ty(C)->getPointerTo(AS);
227 }
228 
229 PointerType *Type::getPPC_FP128PtrTy(LLVMContext &C, unsigned AS) {
230   return getPPC_FP128Ty(C)->getPointerTo(AS);
231 }
232 
233 PointerType *Type::getX86_MMXPtrTy(LLVMContext &C, unsigned AS) {
234   return getX86_MMXTy(C)->getPointerTo(AS);
235 }
236 
237 PointerType *Type::getX86_AMXPtrTy(LLVMContext &C, unsigned AS) {
238   return getX86_AMXTy(C)->getPointerTo(AS);
239 }
240 
241 PointerType *Type::getIntNPtrTy(LLVMContext &C, unsigned N, unsigned AS) {
242   return getIntNTy(C, N)->getPointerTo(AS);
243 }
244 
245 PointerType *Type::getInt1PtrTy(LLVMContext &C, unsigned AS) {
246   return getInt1Ty(C)->getPointerTo(AS);
247 }
248 
249 PointerType *Type::getInt8PtrTy(LLVMContext &C, unsigned AS) {
250   return getInt8Ty(C)->getPointerTo(AS);
251 }
252 
253 PointerType *Type::getInt16PtrTy(LLVMContext &C, unsigned AS) {
254   return getInt16Ty(C)->getPointerTo(AS);
255 }
256 
257 PointerType *Type::getInt32PtrTy(LLVMContext &C, unsigned AS) {
258   return getInt32Ty(C)->getPointerTo(AS);
259 }
260 
261 PointerType *Type::getInt64PtrTy(LLVMContext &C, unsigned AS) {
262   return getInt64Ty(C)->getPointerTo(AS);
263 }
264 
265 //===----------------------------------------------------------------------===//
266 //                       IntegerType Implementation
267 //===----------------------------------------------------------------------===//
268 
269 IntegerType *IntegerType::get(LLVMContext &C, unsigned NumBits) {
270   assert(NumBits >= MIN_INT_BITS && "bitwidth too small");
271   assert(NumBits <= MAX_INT_BITS && "bitwidth too large");
272 
273   // Check for the built-in integer types
274   switch (NumBits) {
275   case   1: return cast<IntegerType>(Type::getInt1Ty(C));
276   case   8: return cast<IntegerType>(Type::getInt8Ty(C));
277   case  16: return cast<IntegerType>(Type::getInt16Ty(C));
278   case  32: return cast<IntegerType>(Type::getInt32Ty(C));
279   case  64: return cast<IntegerType>(Type::getInt64Ty(C));
280   case 128: return cast<IntegerType>(Type::getInt128Ty(C));
281   default:
282     break;
283   }
284 
285   IntegerType *&Entry = C.pImpl->IntegerTypes[NumBits];
286 
287   if (!Entry)
288     Entry = new (C.pImpl->Alloc) IntegerType(C, NumBits);
289 
290   return Entry;
291 }
292 
293 APInt IntegerType::getMask() const {
294   return APInt::getAllOnesValue(getBitWidth());
295 }
296 
297 //===----------------------------------------------------------------------===//
298 //                       FunctionType Implementation
299 //===----------------------------------------------------------------------===//
300 
301 FunctionType::FunctionType(Type *Result, ArrayRef<Type*> Params,
302                            bool IsVarArgs)
303   : Type(Result->getContext(), FunctionTyID) {
304   Type **SubTys = reinterpret_cast<Type**>(this+1);
305   assert(isValidReturnType(Result) && "invalid return type for function");
306   setSubclassData(IsVarArgs);
307 
308   SubTys[0] = Result;
309 
310   for (unsigned i = 0, e = Params.size(); i != e; ++i) {
311     assert(isValidArgumentType(Params[i]) &&
312            "Not a valid type for function argument!");
313     SubTys[i+1] = Params[i];
314   }
315 
316   ContainedTys = SubTys;
317   NumContainedTys = Params.size() + 1; // + 1 for result type
318 }
319 
320 // This is the factory function for the FunctionType class.
321 FunctionType *FunctionType::get(Type *ReturnType,
322                                 ArrayRef<Type*> Params, bool isVarArg) {
323   LLVMContextImpl *pImpl = ReturnType->getContext().pImpl;
324   const FunctionTypeKeyInfo::KeyTy Key(ReturnType, Params, isVarArg);
325   FunctionType *FT;
326   // Since we only want to allocate a fresh function type in case none is found
327   // and we don't want to perform two lookups (one for checking if existent and
328   // one for inserting the newly allocated one), here we instead lookup based on
329   // Key and update the reference to the function type in-place to a newly
330   // allocated one if not found.
331   auto Insertion = pImpl->FunctionTypes.insert_as(nullptr, Key);
332   if (Insertion.second) {
333     // The function type was not found. Allocate one and update FunctionTypes
334     // in-place.
335     FT = (FunctionType *)pImpl->Alloc.Allocate(
336         sizeof(FunctionType) + sizeof(Type *) * (Params.size() + 1),
337         alignof(FunctionType));
338     new (FT) FunctionType(ReturnType, Params, isVarArg);
339     *Insertion.first = FT;
340   } else {
341     // The function type was found. Just return it.
342     FT = *Insertion.first;
343   }
344   return FT;
345 }
346 
347 FunctionType *FunctionType::get(Type *Result, bool isVarArg) {
348   return get(Result, None, isVarArg);
349 }
350 
351 bool FunctionType::isValidReturnType(Type *RetTy) {
352   return !RetTy->isFunctionTy() && !RetTy->isLabelTy() &&
353   !RetTy->isMetadataTy();
354 }
355 
356 bool FunctionType::isValidArgumentType(Type *ArgTy) {
357   return ArgTy->isFirstClassType();
358 }
359 
360 //===----------------------------------------------------------------------===//
361 //                       StructType Implementation
362 //===----------------------------------------------------------------------===//
363 
364 // Primitive Constructors.
365 
366 StructType *StructType::get(LLVMContext &Context, ArrayRef<Type*> ETypes,
367                             bool isPacked) {
368   LLVMContextImpl *pImpl = Context.pImpl;
369   const AnonStructTypeKeyInfo::KeyTy Key(ETypes, isPacked);
370 
371   StructType *ST;
372   // Since we only want to allocate a fresh struct type in case none is found
373   // and we don't want to perform two lookups (one for checking if existent and
374   // one for inserting the newly allocated one), here we instead lookup based on
375   // Key and update the reference to the struct type in-place to a newly
376   // allocated one if not found.
377   auto Insertion = pImpl->AnonStructTypes.insert_as(nullptr, Key);
378   if (Insertion.second) {
379     // The struct type was not found. Allocate one and update AnonStructTypes
380     // in-place.
381     ST = new (Context.pImpl->Alloc) StructType(Context);
382     ST->setSubclassData(SCDB_IsLiteral);  // Literal struct.
383     ST->setBody(ETypes, isPacked);
384     *Insertion.first = ST;
385   } else {
386     // The struct type was found. Just return it.
387     ST = *Insertion.first;
388   }
389 
390   return ST;
391 }
392 
393 void StructType::setBody(ArrayRef<Type*> Elements, bool isPacked) {
394   assert(isOpaque() && "Struct body already set!");
395 
396   setSubclassData(getSubclassData() | SCDB_HasBody);
397   if (isPacked)
398     setSubclassData(getSubclassData() | SCDB_Packed);
399 
400   NumContainedTys = Elements.size();
401 
402   if (Elements.empty()) {
403     ContainedTys = nullptr;
404     return;
405   }
406 
407   ContainedTys = Elements.copy(getContext().pImpl->Alloc).data();
408 }
409 
410 void StructType::setName(StringRef Name) {
411   if (Name == getName()) return;
412 
413   StringMap<StructType *> &SymbolTable = getContext().pImpl->NamedStructTypes;
414 
415   using EntryTy = StringMap<StructType *>::MapEntryTy;
416 
417   // If this struct already had a name, remove its symbol table entry. Don't
418   // delete the data yet because it may be part of the new name.
419   if (SymbolTableEntry)
420     SymbolTable.remove((EntryTy *)SymbolTableEntry);
421 
422   // If this is just removing the name, we're done.
423   if (Name.empty()) {
424     if (SymbolTableEntry) {
425       // Delete the old string data.
426       ((EntryTy *)SymbolTableEntry)->Destroy(SymbolTable.getAllocator());
427       SymbolTableEntry = nullptr;
428     }
429     return;
430   }
431 
432   // Look up the entry for the name.
433   auto IterBool =
434       getContext().pImpl->NamedStructTypes.insert(std::make_pair(Name, this));
435 
436   // While we have a name collision, try a random rename.
437   if (!IterBool.second) {
438     SmallString<64> TempStr(Name);
439     TempStr.push_back('.');
440     raw_svector_ostream TmpStream(TempStr);
441     unsigned NameSize = Name.size();
442 
443     do {
444       TempStr.resize(NameSize + 1);
445       TmpStream << getContext().pImpl->NamedStructTypesUniqueID++;
446 
447       IterBool = getContext().pImpl->NamedStructTypes.insert(
448           std::make_pair(TmpStream.str(), this));
449     } while (!IterBool.second);
450   }
451 
452   // Delete the old string data.
453   if (SymbolTableEntry)
454     ((EntryTy *)SymbolTableEntry)->Destroy(SymbolTable.getAllocator());
455   SymbolTableEntry = &*IterBool.first;
456 }
457 
458 //===----------------------------------------------------------------------===//
459 // StructType Helper functions.
460 
461 StructType *StructType::create(LLVMContext &Context, StringRef Name) {
462   StructType *ST = new (Context.pImpl->Alloc) StructType(Context);
463   if (!Name.empty())
464     ST->setName(Name);
465   return ST;
466 }
467 
468 StructType *StructType::get(LLVMContext &Context, bool isPacked) {
469   return get(Context, None, isPacked);
470 }
471 
472 StructType *StructType::create(LLVMContext &Context, ArrayRef<Type*> Elements,
473                                StringRef Name, bool isPacked) {
474   StructType *ST = create(Context, Name);
475   ST->setBody(Elements, isPacked);
476   return ST;
477 }
478 
479 StructType *StructType::create(LLVMContext &Context, ArrayRef<Type*> Elements) {
480   return create(Context, Elements, StringRef());
481 }
482 
483 StructType *StructType::create(LLVMContext &Context) {
484   return create(Context, StringRef());
485 }
486 
487 StructType *StructType::create(ArrayRef<Type*> Elements, StringRef Name,
488                                bool isPacked) {
489   assert(!Elements.empty() &&
490          "This method may not be invoked with an empty list");
491   return create(Elements[0]->getContext(), Elements, Name, isPacked);
492 }
493 
494 StructType *StructType::create(ArrayRef<Type*> Elements) {
495   assert(!Elements.empty() &&
496          "This method may not be invoked with an empty list");
497   return create(Elements[0]->getContext(), Elements, StringRef());
498 }
499 
500 bool StructType::isSized(SmallPtrSetImpl<Type*> *Visited) const {
501   if ((getSubclassData() & SCDB_IsSized) != 0)
502     return true;
503   if (isOpaque())
504     return false;
505 
506   if (Visited && !Visited->insert(const_cast<StructType*>(this)).second)
507     return false;
508 
509   // Okay, our struct is sized if all of the elements are, but if one of the
510   // elements is opaque, the struct isn't sized *yet*, but may become sized in
511   // the future, so just bail out without caching.
512   for (element_iterator I = element_begin(), E = element_end(); I != E; ++I)
513     if (!(*I)->isSized(Visited))
514       return false;
515 
516   // Here we cheat a bit and cast away const-ness. The goal is to memoize when
517   // we find a sized type, as types can only move from opaque to sized, not the
518   // other way.
519   const_cast<StructType*>(this)->setSubclassData(
520     getSubclassData() | SCDB_IsSized);
521   return true;
522 }
523 
524 StringRef StructType::getName() const {
525   assert(!isLiteral() && "Literal structs never have names");
526   if (!SymbolTableEntry) return StringRef();
527 
528   return ((StringMapEntry<StructType*> *)SymbolTableEntry)->getKey();
529 }
530 
531 bool StructType::isValidElementType(Type *ElemTy) {
532   return !ElemTy->isVoidTy() && !ElemTy->isLabelTy() &&
533          !ElemTy->isMetadataTy() && !ElemTy->isFunctionTy() &&
534          !ElemTy->isTokenTy() && !isa<ScalableVectorType>(ElemTy);
535 }
536 
537 bool StructType::isLayoutIdentical(StructType *Other) const {
538   if (this == Other) return true;
539 
540   if (isPacked() != Other->isPacked())
541     return false;
542 
543   return elements() == Other->elements();
544 }
545 
546 Type *StructType::getTypeAtIndex(const Value *V) const {
547   unsigned Idx = (unsigned)cast<Constant>(V)->getUniqueInteger().getZExtValue();
548   assert(indexValid(Idx) && "Invalid structure index!");
549   return getElementType(Idx);
550 }
551 
552 bool StructType::indexValid(const Value *V) const {
553   // Structure indexes require (vectors of) 32-bit integer constants.  In the
554   // vector case all of the indices must be equal.
555   if (!V->getType()->isIntOrIntVectorTy(32))
556     return false;
557   if (isa<ScalableVectorType>(V->getType()))
558     return false;
559   const Constant *C = dyn_cast<Constant>(V);
560   if (C && V->getType()->isVectorTy())
561     C = C->getSplatValue();
562   const ConstantInt *CU = dyn_cast_or_null<ConstantInt>(C);
563   return CU && CU->getZExtValue() < getNumElements();
564 }
565 
566 StructType *StructType::getTypeByName(LLVMContext &C, StringRef Name) {
567   return C.pImpl->NamedStructTypes.lookup(Name);
568 }
569 
570 //===----------------------------------------------------------------------===//
571 //                           ArrayType Implementation
572 //===----------------------------------------------------------------------===//
573 
574 ArrayType::ArrayType(Type *ElType, uint64_t NumEl)
575     : Type(ElType->getContext(), ArrayTyID), ContainedType(ElType),
576       NumElements(NumEl) {
577   ContainedTys = &ContainedType;
578   NumContainedTys = 1;
579 }
580 
581 ArrayType *ArrayType::get(Type *ElementType, uint64_t NumElements) {
582   assert(isValidElementType(ElementType) && "Invalid type for array element!");
583 
584   LLVMContextImpl *pImpl = ElementType->getContext().pImpl;
585   ArrayType *&Entry =
586     pImpl->ArrayTypes[std::make_pair(ElementType, NumElements)];
587 
588   if (!Entry)
589     Entry = new (pImpl->Alloc) ArrayType(ElementType, NumElements);
590   return Entry;
591 }
592 
593 bool ArrayType::isValidElementType(Type *ElemTy) {
594   return !ElemTy->isVoidTy() && !ElemTy->isLabelTy() &&
595          !ElemTy->isMetadataTy() && !ElemTy->isFunctionTy() &&
596          !ElemTy->isTokenTy() && !isa<ScalableVectorType>(ElemTy);
597 }
598 
599 //===----------------------------------------------------------------------===//
600 //                          VectorType Implementation
601 //===----------------------------------------------------------------------===//
602 
603 VectorType::VectorType(Type *ElType, unsigned EQ, Type::TypeID TID)
604     : Type(ElType->getContext(), TID), ContainedType(ElType),
605       ElementQuantity(EQ) {
606   ContainedTys = &ContainedType;
607   NumContainedTys = 1;
608 }
609 
610 VectorType *VectorType::get(Type *ElementType, ElementCount EC) {
611   if (EC.isScalable())
612     return ScalableVectorType::get(ElementType, EC.getKnownMinValue());
613   else
614     return FixedVectorType::get(ElementType, EC.getKnownMinValue());
615 }
616 
617 bool VectorType::isValidElementType(Type *ElemTy) {
618   return ElemTy->isIntegerTy() || ElemTy->isFloatingPointTy() ||
619          ElemTy->isPointerTy();
620 }
621 
622 //===----------------------------------------------------------------------===//
623 //                        FixedVectorType Implementation
624 //===----------------------------------------------------------------------===//
625 
626 FixedVectorType *FixedVectorType::get(Type *ElementType, unsigned NumElts) {
627   assert(NumElts > 0 && "#Elements of a VectorType must be greater than 0");
628   assert(isValidElementType(ElementType) && "Element type of a VectorType must "
629                                             "be an integer, floating point, or "
630                                             "pointer type.");
631 
632   auto EC = ElementCount::getFixed(NumElts);
633 
634   LLVMContextImpl *pImpl = ElementType->getContext().pImpl;
635   VectorType *&Entry = ElementType->getContext()
636                            .pImpl->VectorTypes[std::make_pair(ElementType, EC)];
637 
638   if (!Entry)
639     Entry = new (pImpl->Alloc) FixedVectorType(ElementType, NumElts);
640   return cast<FixedVectorType>(Entry);
641 }
642 
643 //===----------------------------------------------------------------------===//
644 //                       ScalableVectorType Implementation
645 //===----------------------------------------------------------------------===//
646 
647 ScalableVectorType *ScalableVectorType::get(Type *ElementType,
648                                             unsigned MinNumElts) {
649   assert(MinNumElts > 0 && "#Elements of a VectorType must be greater than 0");
650   assert(isValidElementType(ElementType) && "Element type of a VectorType must "
651                                             "be an integer, floating point, or "
652                                             "pointer type.");
653 
654   auto EC = ElementCount::getScalable(MinNumElts);
655 
656   LLVMContextImpl *pImpl = ElementType->getContext().pImpl;
657   VectorType *&Entry = ElementType->getContext()
658                            .pImpl->VectorTypes[std::make_pair(ElementType, EC)];
659 
660   if (!Entry)
661     Entry = new (pImpl->Alloc) ScalableVectorType(ElementType, MinNumElts);
662   return cast<ScalableVectorType>(Entry);
663 }
664 
665 //===----------------------------------------------------------------------===//
666 //                         PointerType Implementation
667 //===----------------------------------------------------------------------===//
668 
669 PointerType *PointerType::get(Type *EltTy, unsigned AddressSpace) {
670   assert(EltTy && "Can't get a pointer to <null> type!");
671   assert(isValidElementType(EltTy) && "Invalid type for pointer element!");
672 
673   LLVMContextImpl *CImpl = EltTy->getContext().pImpl;
674 
675   // Since AddressSpace #0 is the common case, we special case it.
676   PointerType *&Entry = AddressSpace == 0 ? CImpl->PointerTypes[EltTy]
677      : CImpl->ASPointerTypes[std::make_pair(EltTy, AddressSpace)];
678 
679   if (!Entry)
680     Entry = new (CImpl->Alloc) PointerType(EltTy, AddressSpace);
681   return Entry;
682 }
683 
684 PointerType::PointerType(Type *E, unsigned AddrSpace)
685   : Type(E->getContext(), PointerTyID), PointeeTy(E) {
686   ContainedTys = &PointeeTy;
687   NumContainedTys = 1;
688   setSubclassData(AddrSpace);
689 }
690 
691 PointerType *Type::getPointerTo(unsigned addrs) const {
692   return PointerType::get(const_cast<Type*>(this), addrs);
693 }
694 
695 bool PointerType::isValidElementType(Type *ElemTy) {
696   return !ElemTy->isVoidTy() && !ElemTy->isLabelTy() &&
697          !ElemTy->isMetadataTy() && !ElemTy->isTokenTy();
698 }
699 
700 bool PointerType::isLoadableOrStorableType(Type *ElemTy) {
701   return isValidElementType(ElemTy) && !ElemTy->isFunctionTy();
702 }
703