1 //===- Type.cpp - Implement the Type class --------------------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This file implements the Type class for the IR library.
11 //===----------------------------------------------------------------------===//
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"
35 //===----------------------------------------------------------------------===//
36 // Type Class Implementation
37 //===----------------------------------------------------------------------===//
39 Type *Type::getPrimitiveType(LLVMContext &C, TypeID 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);
59 bool Type::isIntegerTy(unsigned Bitwidth) const {
60 return isIntegerTy() && cast<IntegerType>(this)->getBitWidth() == Bitwidth;
63 bool Type::canLosslesslyBitCastTo(Type *Ty) const {
64 // Identity cast means no change so return true
68 // They are not convertible unless they are at least first class types
69 if (!this->isFirstClassType() || !Ty->isFirstClassType())
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();
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)
81 if ((isX86_MMXTy() && isa<FixedVectorType>(Ty)) &&
82 Ty->getPrimitiveSizeInBits().getFixedSize() == 64)
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)
89 if ((isX86_AMXTy() && isa<FixedVectorType>(Ty)) &&
90 Ty->getPrimitiveSizeInBits().getFixedSize() == 8192)
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();
102 return false; // Other types have no identity values
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();
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())
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()};
143 default: return TypeSize::Fixed(0);
147 unsigned Type::getScalarSizeInBits() const {
148 // It is safe to assume that the scalar types have a fixed size.
149 return getScalarType()->getPrimitiveSizeInBits().getFixedSize();
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");
166 bool Type::isSizedDerivedType(SmallPtrSetImpl<Type*> *Visited) const {
167 if (auto *ATy = dyn_cast<ArrayType>(this))
168 return ATy->getElementType()->isSized(Visited);
170 if (auto *VTy = dyn_cast<VectorType>(this))
171 return VTy->getElementType()->isSized(Visited);
173 return cast<StructType>(this)->isSized(Visited);
176 //===----------------------------------------------------------------------===//
177 // Primitive 'Type' data
178 //===----------------------------------------------------------------------===//
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; }
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; }
201 IntegerType *Type::getIntNTy(LLVMContext &C, unsigned N) {
202 return IntegerType::get(C, N);
205 PointerType *Type::getHalfPtrTy(LLVMContext &C, unsigned AS) {
206 return getHalfTy(C)->getPointerTo(AS);
209 PointerType *Type::getBFloatPtrTy(LLVMContext &C, unsigned AS) {
210 return getBFloatTy(C)->getPointerTo(AS);
213 PointerType *Type::getFloatPtrTy(LLVMContext &C, unsigned AS) {
214 return getFloatTy(C)->getPointerTo(AS);
217 PointerType *Type::getDoublePtrTy(LLVMContext &C, unsigned AS) {
218 return getDoubleTy(C)->getPointerTo(AS);
221 PointerType *Type::getX86_FP80PtrTy(LLVMContext &C, unsigned AS) {
222 return getX86_FP80Ty(C)->getPointerTo(AS);
225 PointerType *Type::getFP128PtrTy(LLVMContext &C, unsigned AS) {
226 return getFP128Ty(C)->getPointerTo(AS);
229 PointerType *Type::getPPC_FP128PtrTy(LLVMContext &C, unsigned AS) {
230 return getPPC_FP128Ty(C)->getPointerTo(AS);
233 PointerType *Type::getX86_MMXPtrTy(LLVMContext &C, unsigned AS) {
234 return getX86_MMXTy(C)->getPointerTo(AS);
237 PointerType *Type::getX86_AMXPtrTy(LLVMContext &C, unsigned AS) {
238 return getX86_AMXTy(C)->getPointerTo(AS);
241 PointerType *Type::getIntNPtrTy(LLVMContext &C, unsigned N, unsigned AS) {
242 return getIntNTy(C, N)->getPointerTo(AS);
245 PointerType *Type::getInt1PtrTy(LLVMContext &C, unsigned AS) {
246 return getInt1Ty(C)->getPointerTo(AS);
249 PointerType *Type::getInt8PtrTy(LLVMContext &C, unsigned AS) {
250 return getInt8Ty(C)->getPointerTo(AS);
253 PointerType *Type::getInt16PtrTy(LLVMContext &C, unsigned AS) {
254 return getInt16Ty(C)->getPointerTo(AS);
257 PointerType *Type::getInt32PtrTy(LLVMContext &C, unsigned AS) {
258 return getInt32Ty(C)->getPointerTo(AS);
261 PointerType *Type::getInt64PtrTy(LLVMContext &C, unsigned AS) {
262 return getInt64Ty(C)->getPointerTo(AS);
265 //===----------------------------------------------------------------------===//
266 // IntegerType Implementation
267 //===----------------------------------------------------------------------===//
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");
273 // Check for the built-in integer types
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));
285 IntegerType *&Entry = C.pImpl->IntegerTypes[NumBits];
288 Entry = new (C.pImpl->Alloc) IntegerType(C, NumBits);
293 APInt IntegerType::getMask() const {
294 return APInt::getAllOnesValue(getBitWidth());
297 //===----------------------------------------------------------------------===//
298 // FunctionType Implementation
299 //===----------------------------------------------------------------------===//
301 FunctionType::FunctionType(Type *Result, ArrayRef<Type*> Params,
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);
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];
316 ContainedTys = SubTys;
317 NumContainedTys = Params.size() + 1; // + 1 for result type
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);
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
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;
341 // The function type was found. Just return it.
342 FT = *Insertion.first;
347 FunctionType *FunctionType::get(Type *Result, bool isVarArg) {
348 return get(Result, None, isVarArg);
351 bool FunctionType::isValidReturnType(Type *RetTy) {
352 return !RetTy->isFunctionTy() && !RetTy->isLabelTy() &&
353 !RetTy->isMetadataTy();
356 bool FunctionType::isValidArgumentType(Type *ArgTy) {
357 return ArgTy->isFirstClassType();
360 //===----------------------------------------------------------------------===//
361 // StructType Implementation
362 //===----------------------------------------------------------------------===//
364 // Primitive Constructors.
366 StructType *StructType::get(LLVMContext &Context, ArrayRef<Type*> ETypes,
368 LLVMContextImpl *pImpl = Context.pImpl;
369 const AnonStructTypeKeyInfo::KeyTy Key(ETypes, isPacked);
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
381 ST = new (Context.pImpl->Alloc) StructType(Context);
382 ST->setSubclassData(SCDB_IsLiteral); // Literal struct.
383 ST->setBody(ETypes, isPacked);
384 *Insertion.first = ST;
386 // The struct type was found. Just return it.
387 ST = *Insertion.first;
393 bool StructType::containsScalableVectorType() const {
394 for (Type *Ty : elements()) {
395 if (isa<ScalableVectorType>(Ty))
397 if (auto *STy = dyn_cast<StructType>(Ty))
398 if (STy->containsScalableVectorType())
405 void StructType::setBody(ArrayRef<Type*> Elements, bool isPacked) {
406 assert(isOpaque() && "Struct body already set!");
408 setSubclassData(getSubclassData() | SCDB_HasBody);
410 setSubclassData(getSubclassData() | SCDB_Packed);
412 NumContainedTys = Elements.size();
414 if (Elements.empty()) {
415 ContainedTys = nullptr;
419 ContainedTys = Elements.copy(getContext().pImpl->Alloc).data();
422 void StructType::setName(StringRef Name) {
423 if (Name == getName()) return;
425 StringMap<StructType *> &SymbolTable = getContext().pImpl->NamedStructTypes;
427 using EntryTy = StringMap<StructType *>::MapEntryTy;
429 // If this struct already had a name, remove its symbol table entry. Don't
430 // delete the data yet because it may be part of the new name.
431 if (SymbolTableEntry)
432 SymbolTable.remove((EntryTy *)SymbolTableEntry);
434 // If this is just removing the name, we're done.
436 if (SymbolTableEntry) {
437 // Delete the old string data.
438 ((EntryTy *)SymbolTableEntry)->Destroy(SymbolTable.getAllocator());
439 SymbolTableEntry = nullptr;
444 // Look up the entry for the name.
446 getContext().pImpl->NamedStructTypes.insert(std::make_pair(Name, this));
448 // While we have a name collision, try a random rename.
449 if (!IterBool.second) {
450 SmallString<64> TempStr(Name);
451 TempStr.push_back('.');
452 raw_svector_ostream TmpStream(TempStr);
453 unsigned NameSize = Name.size();
456 TempStr.resize(NameSize + 1);
457 TmpStream << getContext().pImpl->NamedStructTypesUniqueID++;
459 IterBool = getContext().pImpl->NamedStructTypes.insert(
460 std::make_pair(TmpStream.str(), this));
461 } while (!IterBool.second);
464 // Delete the old string data.
465 if (SymbolTableEntry)
466 ((EntryTy *)SymbolTableEntry)->Destroy(SymbolTable.getAllocator());
467 SymbolTableEntry = &*IterBool.first;
470 //===----------------------------------------------------------------------===//
471 // StructType Helper functions.
473 StructType *StructType::create(LLVMContext &Context, StringRef Name) {
474 StructType *ST = new (Context.pImpl->Alloc) StructType(Context);
480 StructType *StructType::get(LLVMContext &Context, bool isPacked) {
481 return get(Context, None, isPacked);
484 StructType *StructType::create(LLVMContext &Context, ArrayRef<Type*> Elements,
485 StringRef Name, bool isPacked) {
486 StructType *ST = create(Context, Name);
487 ST->setBody(Elements, isPacked);
491 StructType *StructType::create(LLVMContext &Context, ArrayRef<Type*> Elements) {
492 return create(Context, Elements, StringRef());
495 StructType *StructType::create(LLVMContext &Context) {
496 return create(Context, StringRef());
499 StructType *StructType::create(ArrayRef<Type*> Elements, StringRef Name,
501 assert(!Elements.empty() &&
502 "This method may not be invoked with an empty list");
503 return create(Elements[0]->getContext(), Elements, Name, isPacked);
506 StructType *StructType::create(ArrayRef<Type*> Elements) {
507 assert(!Elements.empty() &&
508 "This method may not be invoked with an empty list");
509 return create(Elements[0]->getContext(), Elements, StringRef());
512 bool StructType::isSized(SmallPtrSetImpl<Type*> *Visited) const {
513 if ((getSubclassData() & SCDB_IsSized) != 0)
518 if (Visited && !Visited->insert(const_cast<StructType*>(this)).second)
521 // Okay, our struct is sized if all of the elements are, but if one of the
522 // elements is opaque, the struct isn't sized *yet*, but may become sized in
523 // the future, so just bail out without caching.
524 for (Type *Ty : elements()) {
525 // If the struct contains a scalable vector type, don't consider it sized.
526 // This prevents it from being used in loads/stores/allocas/GEPs.
527 if (isa<ScalableVectorType>(Ty))
529 if (!Ty->isSized(Visited))
533 // Here we cheat a bit and cast away const-ness. The goal is to memoize when
534 // we find a sized type, as types can only move from opaque to sized, not the
536 const_cast<StructType*>(this)->setSubclassData(
537 getSubclassData() | SCDB_IsSized);
541 StringRef StructType::getName() const {
542 assert(!isLiteral() && "Literal structs never have names");
543 if (!SymbolTableEntry) return StringRef();
545 return ((StringMapEntry<StructType*> *)SymbolTableEntry)->getKey();
548 bool StructType::isValidElementType(Type *ElemTy) {
549 return !ElemTy->isVoidTy() && !ElemTy->isLabelTy() &&
550 !ElemTy->isMetadataTy() && !ElemTy->isFunctionTy() &&
551 !ElemTy->isTokenTy();
554 bool StructType::isLayoutIdentical(StructType *Other) const {
555 if (this == Other) return true;
557 if (isPacked() != Other->isPacked())
560 return elements() == Other->elements();
563 Type *StructType::getTypeAtIndex(const Value *V) const {
564 unsigned Idx = (unsigned)cast<Constant>(V)->getUniqueInteger().getZExtValue();
565 assert(indexValid(Idx) && "Invalid structure index!");
566 return getElementType(Idx);
569 bool StructType::indexValid(const Value *V) const {
570 // Structure indexes require (vectors of) 32-bit integer constants. In the
571 // vector case all of the indices must be equal.
572 if (!V->getType()->isIntOrIntVectorTy(32))
574 if (isa<ScalableVectorType>(V->getType()))
576 const Constant *C = dyn_cast<Constant>(V);
577 if (C && V->getType()->isVectorTy())
578 C = C->getSplatValue();
579 const ConstantInt *CU = dyn_cast_or_null<ConstantInt>(C);
580 return CU && CU->getZExtValue() < getNumElements();
583 StructType *StructType::getTypeByName(LLVMContext &C, StringRef Name) {
584 return C.pImpl->NamedStructTypes.lookup(Name);
587 //===----------------------------------------------------------------------===//
588 // ArrayType Implementation
589 //===----------------------------------------------------------------------===//
591 ArrayType::ArrayType(Type *ElType, uint64_t NumEl)
592 : Type(ElType->getContext(), ArrayTyID), ContainedType(ElType),
594 ContainedTys = &ContainedType;
598 ArrayType *ArrayType::get(Type *ElementType, uint64_t NumElements) {
599 assert(isValidElementType(ElementType) && "Invalid type for array element!");
601 LLVMContextImpl *pImpl = ElementType->getContext().pImpl;
603 pImpl->ArrayTypes[std::make_pair(ElementType, NumElements)];
606 Entry = new (pImpl->Alloc) ArrayType(ElementType, NumElements);
610 bool ArrayType::isValidElementType(Type *ElemTy) {
611 return !ElemTy->isVoidTy() && !ElemTy->isLabelTy() &&
612 !ElemTy->isMetadataTy() && !ElemTy->isFunctionTy() &&
613 !ElemTy->isTokenTy() && !isa<ScalableVectorType>(ElemTy);
616 //===----------------------------------------------------------------------===//
617 // VectorType Implementation
618 //===----------------------------------------------------------------------===//
620 VectorType::VectorType(Type *ElType, unsigned EQ, Type::TypeID TID)
621 : Type(ElType->getContext(), TID), ContainedType(ElType),
622 ElementQuantity(EQ) {
623 ContainedTys = &ContainedType;
627 VectorType *VectorType::get(Type *ElementType, ElementCount EC) {
629 return ScalableVectorType::get(ElementType, EC.getKnownMinValue());
631 return FixedVectorType::get(ElementType, EC.getKnownMinValue());
634 bool VectorType::isValidElementType(Type *ElemTy) {
635 return ElemTy->isIntegerTy() || ElemTy->isFloatingPointTy() ||
636 ElemTy->isPointerTy();
639 //===----------------------------------------------------------------------===//
640 // FixedVectorType Implementation
641 //===----------------------------------------------------------------------===//
643 FixedVectorType *FixedVectorType::get(Type *ElementType, unsigned NumElts) {
644 assert(NumElts > 0 && "#Elements of a VectorType must be greater than 0");
645 assert(isValidElementType(ElementType) && "Element type of a VectorType must "
646 "be an integer, floating point, or "
649 auto EC = ElementCount::getFixed(NumElts);
651 LLVMContextImpl *pImpl = ElementType->getContext().pImpl;
652 VectorType *&Entry = ElementType->getContext()
653 .pImpl->VectorTypes[std::make_pair(ElementType, EC)];
656 Entry = new (pImpl->Alloc) FixedVectorType(ElementType, NumElts);
657 return cast<FixedVectorType>(Entry);
660 //===----------------------------------------------------------------------===//
661 // ScalableVectorType Implementation
662 //===----------------------------------------------------------------------===//
664 ScalableVectorType *ScalableVectorType::get(Type *ElementType,
665 unsigned MinNumElts) {
666 assert(MinNumElts > 0 && "#Elements of a VectorType must be greater than 0");
667 assert(isValidElementType(ElementType) && "Element type of a VectorType must "
668 "be an integer, floating point, or "
671 auto EC = ElementCount::getScalable(MinNumElts);
673 LLVMContextImpl *pImpl = ElementType->getContext().pImpl;
674 VectorType *&Entry = ElementType->getContext()
675 .pImpl->VectorTypes[std::make_pair(ElementType, EC)];
678 Entry = new (pImpl->Alloc) ScalableVectorType(ElementType, MinNumElts);
679 return cast<ScalableVectorType>(Entry);
682 //===----------------------------------------------------------------------===//
683 // PointerType Implementation
684 //===----------------------------------------------------------------------===//
686 PointerType *PointerType::get(Type *EltTy, unsigned AddressSpace) {
687 assert(EltTy && "Can't get a pointer to <null> type!");
688 assert(isValidElementType(EltTy) && "Invalid type for pointer element!");
690 LLVMContextImpl *CImpl = EltTy->getContext().pImpl;
692 // Since AddressSpace #0 is the common case, we special case it.
693 PointerType *&Entry = AddressSpace == 0 ? CImpl->PointerTypes[EltTy]
694 : CImpl->ASPointerTypes[std::make_pair(EltTy, AddressSpace)];
697 Entry = new (CImpl->Alloc) PointerType(EltTy, AddressSpace);
701 PointerType::PointerType(Type *E, unsigned AddrSpace)
702 : Type(E->getContext(), PointerTyID), PointeeTy(E) {
703 ContainedTys = &PointeeTy;
705 setSubclassData(AddrSpace);
708 PointerType *Type::getPointerTo(unsigned addrs) const {
709 return PointerType::get(const_cast<Type*>(this), addrs);
712 bool PointerType::isValidElementType(Type *ElemTy) {
713 return !ElemTy->isVoidTy() && !ElemTy->isLabelTy() &&
714 !ElemTy->isMetadataTy() && !ElemTy->isTokenTy();
717 bool PointerType::isLoadableOrStorableType(Type *ElemTy) {
718 return isValidElementType(ElemTy) && !ElemTy->isFunctionTy();