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 TokenTyID : return getTokenTy(C);
58 bool Type::isIntegerTy(unsigned Bitwidth) const {
59 return isIntegerTy() && cast<IntegerType>(this)->getBitWidth() == Bitwidth;
62 bool Type::canLosslesslyBitCastTo(Type *Ty) const {
63 // Identity cast means no change so return true
67 // They are not convertible unless they are at least first class types
68 if (!this->isFirstClassType() || !Ty->isFirstClassType())
71 // Vector -> Vector conversions are always lossless if the two vector types
72 // have the same size, otherwise not.
73 if (isa<VectorType>(this) && isa<VectorType>(Ty))
74 return getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits();
76 // 64-bit fixed width vector types can be losslessly converted to x86mmx.
77 if (((isa<FixedVectorType>(this)) && Ty->isX86_MMXTy()) &&
78 getPrimitiveSizeInBits().getFixedSize() == 64)
80 if ((isX86_MMXTy() && isa<FixedVectorType>(Ty)) &&
81 Ty->getPrimitiveSizeInBits().getFixedSize() == 64)
84 // At this point we have only various mismatches of the first class types
85 // remaining and ptr->ptr. Just select the lossless conversions. Everything
86 // else is not lossless. Conservatively assume we can't losslessly convert
87 // between pointers with different address spaces.
88 if (auto *PTy = dyn_cast<PointerType>(this)) {
89 if (auto *OtherPTy = dyn_cast<PointerType>(Ty))
90 return PTy->getAddressSpace() == OtherPTy->getAddressSpace();
93 return false; // Other types have no identity values
96 bool Type::isEmptyTy() const {
97 if (auto *ATy = dyn_cast<ArrayType>(this)) {
98 unsigned NumElements = ATy->getNumElements();
99 return NumElements == 0 || ATy->getElementType()->isEmptyTy();
102 if (auto *STy = dyn_cast<StructType>(this)) {
103 unsigned NumElements = STy->getNumElements();
104 for (unsigned i = 0; i < NumElements; ++i)
105 if (!STy->getElementType(i)->isEmptyTy())
113 TypeSize Type::getPrimitiveSizeInBits() const {
114 switch (getTypeID()) {
115 case Type::HalfTyID: return TypeSize::Fixed(16);
116 case Type::BFloatTyID: return TypeSize::Fixed(16);
117 case Type::FloatTyID: return TypeSize::Fixed(32);
118 case Type::DoubleTyID: return TypeSize::Fixed(64);
119 case Type::X86_FP80TyID: return TypeSize::Fixed(80);
120 case Type::FP128TyID: return TypeSize::Fixed(128);
121 case Type::PPC_FP128TyID: return TypeSize::Fixed(128);
122 case Type::X86_MMXTyID: return TypeSize::Fixed(64);
123 case Type::IntegerTyID:
124 return TypeSize::Fixed(cast<IntegerType>(this)->getBitWidth());
125 case Type::FixedVectorTyID:
126 case Type::ScalableVectorTyID: {
127 const VectorType *VTy = cast<VectorType>(this);
128 ElementCount EC = VTy->getElementCount();
129 TypeSize ETS = VTy->getElementType()->getPrimitiveSizeInBits();
130 assert(!ETS.isScalable() && "Vector type should have fixed-width elements");
131 return {ETS.getFixedSize() * EC.Min, EC.Scalable};
133 default: return TypeSize::Fixed(0);
137 unsigned Type::getScalarSizeInBits() const {
138 // It is safe to assume that the scalar types have a fixed size.
139 return getScalarType()->getPrimitiveSizeInBits().getFixedSize();
142 int Type::getFPMantissaWidth() const {
143 if (auto *VTy = dyn_cast<VectorType>(this))
144 return VTy->getElementType()->getFPMantissaWidth();
145 assert(isFloatingPointTy() && "Not a floating point type!");
146 if (getTypeID() == HalfTyID) return 11;
147 if (getTypeID() == BFloatTyID) return 8;
148 if (getTypeID() == FloatTyID) return 24;
149 if (getTypeID() == DoubleTyID) return 53;
150 if (getTypeID() == X86_FP80TyID) return 64;
151 if (getTypeID() == FP128TyID) return 113;
152 assert(getTypeID() == PPC_FP128TyID && "unknown fp type");
156 bool Type::isSizedDerivedType(SmallPtrSetImpl<Type*> *Visited) const {
157 if (auto *ATy = dyn_cast<ArrayType>(this))
158 return ATy->getElementType()->isSized(Visited);
160 if (auto *VTy = dyn_cast<VectorType>(this))
161 return VTy->getElementType()->isSized(Visited);
163 return cast<StructType>(this)->isSized(Visited);
166 //===----------------------------------------------------------------------===//
167 // Primitive 'Type' data
168 //===----------------------------------------------------------------------===//
170 Type *Type::getVoidTy(LLVMContext &C) { return &C.pImpl->VoidTy; }
171 Type *Type::getLabelTy(LLVMContext &C) { return &C.pImpl->LabelTy; }
172 Type *Type::getHalfTy(LLVMContext &C) { return &C.pImpl->HalfTy; }
173 Type *Type::getBFloatTy(LLVMContext &C) { return &C.pImpl->BFloatTy; }
174 Type *Type::getFloatTy(LLVMContext &C) { return &C.pImpl->FloatTy; }
175 Type *Type::getDoubleTy(LLVMContext &C) { return &C.pImpl->DoubleTy; }
176 Type *Type::getMetadataTy(LLVMContext &C) { return &C.pImpl->MetadataTy; }
177 Type *Type::getTokenTy(LLVMContext &C) { return &C.pImpl->TokenTy; }
178 Type *Type::getX86_FP80Ty(LLVMContext &C) { return &C.pImpl->X86_FP80Ty; }
179 Type *Type::getFP128Ty(LLVMContext &C) { return &C.pImpl->FP128Ty; }
180 Type *Type::getPPC_FP128Ty(LLVMContext &C) { return &C.pImpl->PPC_FP128Ty; }
181 Type *Type::getX86_MMXTy(LLVMContext &C) { return &C.pImpl->X86_MMXTy; }
183 IntegerType *Type::getInt1Ty(LLVMContext &C) { return &C.pImpl->Int1Ty; }
184 IntegerType *Type::getInt8Ty(LLVMContext &C) { return &C.pImpl->Int8Ty; }
185 IntegerType *Type::getInt16Ty(LLVMContext &C) { return &C.pImpl->Int16Ty; }
186 IntegerType *Type::getInt32Ty(LLVMContext &C) { return &C.pImpl->Int32Ty; }
187 IntegerType *Type::getInt64Ty(LLVMContext &C) { return &C.pImpl->Int64Ty; }
188 IntegerType *Type::getInt128Ty(LLVMContext &C) { return &C.pImpl->Int128Ty; }
190 IntegerType *Type::getIntNTy(LLVMContext &C, unsigned N) {
191 return IntegerType::get(C, N);
194 PointerType *Type::getHalfPtrTy(LLVMContext &C, unsigned AS) {
195 return getHalfTy(C)->getPointerTo(AS);
198 PointerType *Type::getBFloatPtrTy(LLVMContext &C, unsigned AS) {
199 return getBFloatTy(C)->getPointerTo(AS);
202 PointerType *Type::getFloatPtrTy(LLVMContext &C, unsigned AS) {
203 return getFloatTy(C)->getPointerTo(AS);
206 PointerType *Type::getDoublePtrTy(LLVMContext &C, unsigned AS) {
207 return getDoubleTy(C)->getPointerTo(AS);
210 PointerType *Type::getX86_FP80PtrTy(LLVMContext &C, unsigned AS) {
211 return getX86_FP80Ty(C)->getPointerTo(AS);
214 PointerType *Type::getFP128PtrTy(LLVMContext &C, unsigned AS) {
215 return getFP128Ty(C)->getPointerTo(AS);
218 PointerType *Type::getPPC_FP128PtrTy(LLVMContext &C, unsigned AS) {
219 return getPPC_FP128Ty(C)->getPointerTo(AS);
222 PointerType *Type::getX86_MMXPtrTy(LLVMContext &C, unsigned AS) {
223 return getX86_MMXTy(C)->getPointerTo(AS);
226 PointerType *Type::getIntNPtrTy(LLVMContext &C, unsigned N, unsigned AS) {
227 return getIntNTy(C, N)->getPointerTo(AS);
230 PointerType *Type::getInt1PtrTy(LLVMContext &C, unsigned AS) {
231 return getInt1Ty(C)->getPointerTo(AS);
234 PointerType *Type::getInt8PtrTy(LLVMContext &C, unsigned AS) {
235 return getInt8Ty(C)->getPointerTo(AS);
238 PointerType *Type::getInt16PtrTy(LLVMContext &C, unsigned AS) {
239 return getInt16Ty(C)->getPointerTo(AS);
242 PointerType *Type::getInt32PtrTy(LLVMContext &C, unsigned AS) {
243 return getInt32Ty(C)->getPointerTo(AS);
246 PointerType *Type::getInt64PtrTy(LLVMContext &C, unsigned AS) {
247 return getInt64Ty(C)->getPointerTo(AS);
250 //===----------------------------------------------------------------------===//
251 // IntegerType Implementation
252 //===----------------------------------------------------------------------===//
254 IntegerType *IntegerType::get(LLVMContext &C, unsigned NumBits) {
255 assert(NumBits >= MIN_INT_BITS && "bitwidth too small");
256 assert(NumBits <= MAX_INT_BITS && "bitwidth too large");
258 // Check for the built-in integer types
260 case 1: return cast<IntegerType>(Type::getInt1Ty(C));
261 case 8: return cast<IntegerType>(Type::getInt8Ty(C));
262 case 16: return cast<IntegerType>(Type::getInt16Ty(C));
263 case 32: return cast<IntegerType>(Type::getInt32Ty(C));
264 case 64: return cast<IntegerType>(Type::getInt64Ty(C));
265 case 128: return cast<IntegerType>(Type::getInt128Ty(C));
270 IntegerType *&Entry = C.pImpl->IntegerTypes[NumBits];
273 Entry = new (C.pImpl->Alloc) IntegerType(C, NumBits);
278 bool IntegerType::isPowerOf2ByteWidth() const {
279 unsigned BitWidth = getBitWidth();
280 return (BitWidth > 7) && isPowerOf2_32(BitWidth);
283 APInt IntegerType::getMask() const {
284 return APInt::getAllOnesValue(getBitWidth());
287 //===----------------------------------------------------------------------===//
288 // FunctionType Implementation
289 //===----------------------------------------------------------------------===//
291 FunctionType::FunctionType(Type *Result, ArrayRef<Type*> Params,
293 : Type(Result->getContext(), FunctionTyID) {
294 Type **SubTys = reinterpret_cast<Type**>(this+1);
295 assert(isValidReturnType(Result) && "invalid return type for function");
296 setSubclassData(IsVarArgs);
300 for (unsigned i = 0, e = Params.size(); i != e; ++i) {
301 assert(isValidArgumentType(Params[i]) &&
302 "Not a valid type for function argument!");
303 SubTys[i+1] = Params[i];
306 ContainedTys = SubTys;
307 NumContainedTys = Params.size() + 1; // + 1 for result type
310 // This is the factory function for the FunctionType class.
311 FunctionType *FunctionType::get(Type *ReturnType,
312 ArrayRef<Type*> Params, bool isVarArg) {
313 LLVMContextImpl *pImpl = ReturnType->getContext().pImpl;
314 const FunctionTypeKeyInfo::KeyTy Key(ReturnType, Params, isVarArg);
316 // Since we only want to allocate a fresh function type in case none is found
317 // and we don't want to perform two lookups (one for checking if existent and
318 // one for inserting the newly allocated one), here we instead lookup based on
319 // Key and update the reference to the function type in-place to a newly
320 // allocated one if not found.
321 auto Insertion = pImpl->FunctionTypes.insert_as(nullptr, Key);
322 if (Insertion.second) {
323 // The function type was not found. Allocate one and update FunctionTypes
325 FT = (FunctionType *)pImpl->Alloc.Allocate(
326 sizeof(FunctionType) + sizeof(Type *) * (Params.size() + 1),
327 alignof(FunctionType));
328 new (FT) FunctionType(ReturnType, Params, isVarArg);
329 *Insertion.first = FT;
331 // The function type was found. Just return it.
332 FT = *Insertion.first;
337 FunctionType *FunctionType::get(Type *Result, bool isVarArg) {
338 return get(Result, None, isVarArg);
341 bool FunctionType::isValidReturnType(Type *RetTy) {
342 return !RetTy->isFunctionTy() && !RetTy->isLabelTy() &&
343 !RetTy->isMetadataTy();
346 bool FunctionType::isValidArgumentType(Type *ArgTy) {
347 return ArgTy->isFirstClassType();
350 //===----------------------------------------------------------------------===//
351 // StructType Implementation
352 //===----------------------------------------------------------------------===//
354 // Primitive Constructors.
356 StructType *StructType::get(LLVMContext &Context, ArrayRef<Type*> ETypes,
358 LLVMContextImpl *pImpl = Context.pImpl;
359 const AnonStructTypeKeyInfo::KeyTy Key(ETypes, isPacked);
362 // Since we only want to allocate a fresh struct type in case none is found
363 // and we don't want to perform two lookups (one for checking if existent and
364 // one for inserting the newly allocated one), here we instead lookup based on
365 // Key and update the reference to the struct type in-place to a newly
366 // allocated one if not found.
367 auto Insertion = pImpl->AnonStructTypes.insert_as(nullptr, Key);
368 if (Insertion.second) {
369 // The struct type was not found. Allocate one and update AnonStructTypes
371 ST = new (Context.pImpl->Alloc) StructType(Context);
372 ST->setSubclassData(SCDB_IsLiteral); // Literal struct.
373 ST->setBody(ETypes, isPacked);
374 *Insertion.first = ST;
376 // The struct type was found. Just return it.
377 ST = *Insertion.first;
383 void StructType::setBody(ArrayRef<Type*> Elements, bool isPacked) {
384 assert(isOpaque() && "Struct body already set!");
386 setSubclassData(getSubclassData() | SCDB_HasBody);
388 setSubclassData(getSubclassData() | SCDB_Packed);
390 NumContainedTys = Elements.size();
392 if (Elements.empty()) {
393 ContainedTys = nullptr;
397 ContainedTys = Elements.copy(getContext().pImpl->Alloc).data();
400 void StructType::setName(StringRef Name) {
401 if (Name == getName()) return;
403 StringMap<StructType *> &SymbolTable = getContext().pImpl->NamedStructTypes;
405 using EntryTy = StringMap<StructType *>::MapEntryTy;
407 // If this struct already had a name, remove its symbol table entry. Don't
408 // delete the data yet because it may be part of the new name.
409 if (SymbolTableEntry)
410 SymbolTable.remove((EntryTy *)SymbolTableEntry);
412 // If this is just removing the name, we're done.
414 if (SymbolTableEntry) {
415 // Delete the old string data.
416 ((EntryTy *)SymbolTableEntry)->Destroy(SymbolTable.getAllocator());
417 SymbolTableEntry = nullptr;
422 // Look up the entry for the name.
424 getContext().pImpl->NamedStructTypes.insert(std::make_pair(Name, this));
426 // While we have a name collision, try a random rename.
427 if (!IterBool.second) {
428 SmallString<64> TempStr(Name);
429 TempStr.push_back('.');
430 raw_svector_ostream TmpStream(TempStr);
431 unsigned NameSize = Name.size();
434 TempStr.resize(NameSize + 1);
435 TmpStream << getContext().pImpl->NamedStructTypesUniqueID++;
437 IterBool = getContext().pImpl->NamedStructTypes.insert(
438 std::make_pair(TmpStream.str(), this));
439 } while (!IterBool.second);
442 // Delete the old string data.
443 if (SymbolTableEntry)
444 ((EntryTy *)SymbolTableEntry)->Destroy(SymbolTable.getAllocator());
445 SymbolTableEntry = &*IterBool.first;
448 //===----------------------------------------------------------------------===//
449 // StructType Helper functions.
451 StructType *StructType::create(LLVMContext &Context, StringRef Name) {
452 StructType *ST = new (Context.pImpl->Alloc) StructType(Context);
458 StructType *StructType::get(LLVMContext &Context, bool isPacked) {
459 return get(Context, None, isPacked);
462 StructType *StructType::create(LLVMContext &Context, ArrayRef<Type*> Elements,
463 StringRef Name, bool isPacked) {
464 StructType *ST = create(Context, Name);
465 ST->setBody(Elements, isPacked);
469 StructType *StructType::create(LLVMContext &Context, ArrayRef<Type*> Elements) {
470 return create(Context, Elements, StringRef());
473 StructType *StructType::create(LLVMContext &Context) {
474 return create(Context, StringRef());
477 StructType *StructType::create(ArrayRef<Type*> Elements, StringRef Name,
479 assert(!Elements.empty() &&
480 "This method may not be invoked with an empty list");
481 return create(Elements[0]->getContext(), Elements, Name, isPacked);
484 StructType *StructType::create(ArrayRef<Type*> Elements) {
485 assert(!Elements.empty() &&
486 "This method may not be invoked with an empty list");
487 return create(Elements[0]->getContext(), Elements, StringRef());
490 bool StructType::isSized(SmallPtrSetImpl<Type*> *Visited) const {
491 if ((getSubclassData() & SCDB_IsSized) != 0)
496 if (Visited && !Visited->insert(const_cast<StructType*>(this)).second)
499 // Okay, our struct is sized if all of the elements are, but if one of the
500 // elements is opaque, the struct isn't sized *yet*, but may become sized in
501 // the future, so just bail out without caching.
502 for (element_iterator I = element_begin(), E = element_end(); I != E; ++I)
503 if (!(*I)->isSized(Visited))
506 // Here we cheat a bit and cast away const-ness. The goal is to memoize when
507 // we find a sized type, as types can only move from opaque to sized, not the
509 const_cast<StructType*>(this)->setSubclassData(
510 getSubclassData() | SCDB_IsSized);
514 StringRef StructType::getName() const {
515 assert(!isLiteral() && "Literal structs never have names");
516 if (!SymbolTableEntry) return StringRef();
518 return ((StringMapEntry<StructType*> *)SymbolTableEntry)->getKey();
521 bool StructType::isValidElementType(Type *ElemTy) {
522 return !ElemTy->isVoidTy() && !ElemTy->isLabelTy() &&
523 !ElemTy->isMetadataTy() && !ElemTy->isFunctionTy() &&
524 !ElemTy->isTokenTy() && !isa<ScalableVectorType>(ElemTy);
527 bool StructType::isLayoutIdentical(StructType *Other) const {
528 if (this == Other) return true;
530 if (isPacked() != Other->isPacked())
533 return elements() == Other->elements();
536 StructType *Module::getTypeByName(StringRef Name) const {
537 return getContext().pImpl->NamedStructTypes.lookup(Name);
540 Type *StructType::getTypeAtIndex(const Value *V) const {
541 unsigned Idx = (unsigned)cast<Constant>(V)->getUniqueInteger().getZExtValue();
542 assert(indexValid(Idx) && "Invalid structure index!");
543 return getElementType(Idx);
546 bool StructType::indexValid(const Value *V) const {
547 // Structure indexes require (vectors of) 32-bit integer constants. In the
548 // vector case all of the indices must be equal.
549 if (!V->getType()->isIntOrIntVectorTy(32))
551 if (isa<ScalableVectorType>(V->getType()))
553 const Constant *C = dyn_cast<Constant>(V);
554 if (C && V->getType()->isVectorTy())
555 C = C->getSplatValue();
556 const ConstantInt *CU = dyn_cast_or_null<ConstantInt>(C);
557 return CU && CU->getZExtValue() < getNumElements();
560 //===----------------------------------------------------------------------===//
561 // ArrayType Implementation
562 //===----------------------------------------------------------------------===//
564 ArrayType::ArrayType(Type *ElType, uint64_t NumEl)
565 : Type(ElType->getContext(), ArrayTyID), ContainedType(ElType),
567 ContainedTys = &ContainedType;
571 ArrayType *ArrayType::get(Type *ElementType, uint64_t NumElements) {
572 assert(isValidElementType(ElementType) && "Invalid type for array element!");
574 LLVMContextImpl *pImpl = ElementType->getContext().pImpl;
576 pImpl->ArrayTypes[std::make_pair(ElementType, NumElements)];
579 Entry = new (pImpl->Alloc) ArrayType(ElementType, NumElements);
583 bool ArrayType::isValidElementType(Type *ElemTy) {
584 return !ElemTy->isVoidTy() && !ElemTy->isLabelTy() &&
585 !ElemTy->isMetadataTy() && !ElemTy->isFunctionTy() &&
586 !ElemTy->isTokenTy() && !isa<ScalableVectorType>(ElemTy);
589 //===----------------------------------------------------------------------===//
590 // VectorType Implementation
591 //===----------------------------------------------------------------------===//
593 VectorType::VectorType(Type *ElType, unsigned EQ, Type::TypeID TID)
594 : Type(ElType->getContext(), TID), ContainedType(ElType),
595 ElementQuantity(EQ) {
596 ContainedTys = &ContainedType;
600 VectorType *VectorType::get(Type *ElementType, ElementCount EC) {
602 return ScalableVectorType::get(ElementType, EC.Min);
604 return FixedVectorType::get(ElementType, EC.Min);
607 bool VectorType::isValidElementType(Type *ElemTy) {
608 return ElemTy->isIntegerTy() || ElemTy->isFloatingPointTy() ||
609 ElemTy->isPointerTy();
612 //===----------------------------------------------------------------------===//
613 // FixedVectorType Implementation
614 //===----------------------------------------------------------------------===//
616 FixedVectorType *FixedVectorType::get(Type *ElementType, unsigned NumElts) {
617 assert(NumElts > 0 && "#Elements of a VectorType must be greater than 0");
618 assert(isValidElementType(ElementType) && "Element type of a VectorType must "
619 "be an integer, floating point, or "
622 ElementCount EC(NumElts, false);
624 LLVMContextImpl *pImpl = ElementType->getContext().pImpl;
625 VectorType *&Entry = ElementType->getContext()
626 .pImpl->VectorTypes[std::make_pair(ElementType, EC)];
629 Entry = new (pImpl->Alloc) FixedVectorType(ElementType, NumElts);
630 return cast<FixedVectorType>(Entry);
633 //===----------------------------------------------------------------------===//
634 // ScalableVectorType Implementation
635 //===----------------------------------------------------------------------===//
637 ScalableVectorType *ScalableVectorType::get(Type *ElementType,
638 unsigned MinNumElts) {
639 assert(MinNumElts > 0 && "#Elements of a VectorType must be greater than 0");
640 assert(isValidElementType(ElementType) && "Element type of a VectorType must "
641 "be an integer, floating point, or "
644 ElementCount EC(MinNumElts, true);
646 LLVMContextImpl *pImpl = ElementType->getContext().pImpl;
647 VectorType *&Entry = ElementType->getContext()
648 .pImpl->VectorTypes[std::make_pair(ElementType, EC)];
651 Entry = new (pImpl->Alloc) ScalableVectorType(ElementType, MinNumElts);
652 return cast<ScalableVectorType>(Entry);
655 //===----------------------------------------------------------------------===//
656 // PointerType Implementation
657 //===----------------------------------------------------------------------===//
659 PointerType *PointerType::get(Type *EltTy, unsigned AddressSpace) {
660 assert(EltTy && "Can't get a pointer to <null> type!");
661 assert(isValidElementType(EltTy) && "Invalid type for pointer element!");
663 LLVMContextImpl *CImpl = EltTy->getContext().pImpl;
665 // Since AddressSpace #0 is the common case, we special case it.
666 PointerType *&Entry = AddressSpace == 0 ? CImpl->PointerTypes[EltTy]
667 : CImpl->ASPointerTypes[std::make_pair(EltTy, AddressSpace)];
670 Entry = new (CImpl->Alloc) PointerType(EltTy, AddressSpace);
674 PointerType::PointerType(Type *E, unsigned AddrSpace)
675 : Type(E->getContext(), PointerTyID), PointeeTy(E) {
676 ContainedTys = &PointeeTy;
678 setSubclassData(AddrSpace);
681 PointerType *Type::getPointerTo(unsigned addrs) const {
682 return PointerType::get(const_cast<Type*>(this), addrs);
685 bool PointerType::isValidElementType(Type *ElemTy) {
686 return !ElemTy->isVoidTy() && !ElemTy->isLabelTy() &&
687 !ElemTy->isMetadataTy() && !ElemTy->isTokenTy();
690 bool PointerType::isLoadableOrStorableType(Type *ElemTy) {
691 return isValidElementType(ElemTy) && !ElemTy->isFunctionTy();