1 //===-- Type.cpp - Implement the Type class -------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the Type class for the IR library.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/IR/Type.h"
15 #include "LLVMContextImpl.h"
16 #include "llvm/ADT/SmallString.h"
17 #include "llvm/IR/Module.h"
22 //===----------------------------------------------------------------------===//
23 // Type Class Implementation
24 //===----------------------------------------------------------------------===//
26 Type *Type::getPrimitiveType(LLVMContext &C, TypeID IDNumber) {
28 case VoidTyID : return getVoidTy(C);
29 case HalfTyID : return getHalfTy(C);
30 case FloatTyID : return getFloatTy(C);
31 case DoubleTyID : return getDoubleTy(C);
32 case X86_FP80TyID : return getX86_FP80Ty(C);
33 case FP128TyID : return getFP128Ty(C);
34 case PPC_FP128TyID : return getPPC_FP128Ty(C);
35 case LabelTyID : return getLabelTy(C);
36 case MetadataTyID : return getMetadataTy(C);
37 case X86_MMXTyID : return getX86_MMXTy(C);
38 case TokenTyID : return getTokenTy(C);
44 /// getScalarType - If this is a vector type, return the element type,
45 /// otherwise return this.
46 Type *Type::getScalarType() const {
47 if (auto *VTy = dyn_cast<VectorType>(this))
48 return VTy->getElementType();
49 return const_cast<Type*>(this);
52 /// isIntegerTy - Return true if this is an IntegerType of the specified width.
53 bool Type::isIntegerTy(unsigned Bitwidth) const {
54 return isIntegerTy() && cast<IntegerType>(this)->getBitWidth() == Bitwidth;
57 // canLosslesslyBitCastTo - Return true if this type can be converted to
58 // 'Ty' without any reinterpretation of bits. For example, i8* to i32*.
60 bool Type::canLosslesslyBitCastTo(Type *Ty) const {
61 // Identity cast means no change so return true
65 // They are not convertible unless they are at least first class types
66 if (!this->isFirstClassType() || !Ty->isFirstClassType())
69 // Vector -> Vector conversions are always lossless if the two vector types
70 // have the same size, otherwise not. Also, 64-bit vector types can be
71 // converted to x86mmx.
72 if (auto *thisPTy = dyn_cast<VectorType>(this)) {
73 if (auto *thatPTy = dyn_cast<VectorType>(Ty))
74 return thisPTy->getBitWidth() == thatPTy->getBitWidth();
75 if (Ty->getTypeID() == Type::X86_MMXTyID &&
76 thisPTy->getBitWidth() == 64)
80 if (this->getTypeID() == Type::X86_MMXTyID)
81 if (auto *thatPTy = dyn_cast<VectorType>(Ty))
82 if (thatPTy->getBitWidth() == 64)
85 // At this point we have only various mismatches of the first class types
86 // remaining and ptr->ptr. Just select the lossless conversions. Everything
87 // else is not lossless. Conservatively assume we can't losslessly convert
88 // between pointers with different address spaces.
89 if (auto *PTy = dyn_cast<PointerType>(this)) {
90 if (auto *OtherPTy = dyn_cast<PointerType>(Ty))
91 return PTy->getAddressSpace() == OtherPTy->getAddressSpace();
94 return false; // Other types have no identity values
97 bool Type::isEmptyTy() const {
98 if (auto *ATy = dyn_cast<ArrayType>(this)) {
99 unsigned NumElements = ATy->getNumElements();
100 return NumElements == 0 || ATy->getElementType()->isEmptyTy();
103 if (auto *STy = dyn_cast<StructType>(this)) {
104 unsigned NumElements = STy->getNumElements();
105 for (unsigned i = 0; i < NumElements; ++i)
106 if (!STy->getElementType(i)->isEmptyTy())
114 unsigned Type::getPrimitiveSizeInBits() const {
115 switch (getTypeID()) {
116 case Type::HalfTyID: return 16;
117 case Type::FloatTyID: return 32;
118 case Type::DoubleTyID: return 64;
119 case Type::X86_FP80TyID: return 80;
120 case Type::FP128TyID: return 128;
121 case Type::PPC_FP128TyID: return 128;
122 case Type::X86_MMXTyID: return 64;
123 case Type::IntegerTyID: return cast<IntegerType>(this)->getBitWidth();
124 case Type::VectorTyID: return cast<VectorType>(this)->getBitWidth();
129 /// getScalarSizeInBits - If this is a vector type, return the
130 /// getPrimitiveSizeInBits value for the element type. Otherwise return the
131 /// getPrimitiveSizeInBits value for this type.
132 unsigned Type::getScalarSizeInBits() const {
133 return getScalarType()->getPrimitiveSizeInBits();
136 /// getFPMantissaWidth - Return the width of the mantissa of this type. This
137 /// is only valid on floating point types. If the FP type does not
138 /// have a stable mantissa (e.g. ppc long double), this method returns -1.
139 int Type::getFPMantissaWidth() const {
140 if (auto *VTy = dyn_cast<VectorType>(this))
141 return VTy->getElementType()->getFPMantissaWidth();
142 assert(isFloatingPointTy() && "Not a floating point type!");
143 if (getTypeID() == HalfTyID) return 11;
144 if (getTypeID() == FloatTyID) return 24;
145 if (getTypeID() == DoubleTyID) return 53;
146 if (getTypeID() == X86_FP80TyID) return 64;
147 if (getTypeID() == FP128TyID) return 113;
148 assert(getTypeID() == PPC_FP128TyID && "unknown fp type");
152 /// isSizedDerivedType - Derived types like structures and arrays are sized
153 /// iff all of the members of the type are sized as well. Since asking for
154 /// their size is relatively uncommon, move this operation out of line.
155 bool Type::isSizedDerivedType(SmallPtrSetImpl<Type*> *Visited) const {
156 if (auto *ATy = dyn_cast<ArrayType>(this))
157 return ATy->getElementType()->isSized(Visited);
159 if (auto *VTy = dyn_cast<VectorType>(this))
160 return VTy->getElementType()->isSized(Visited);
162 return cast<StructType>(this)->isSized(Visited);
165 //===----------------------------------------------------------------------===//
166 // Primitive 'Type' data
167 //===----------------------------------------------------------------------===//
169 Type *Type::getVoidTy(LLVMContext &C) { return &C.pImpl->VoidTy; }
170 Type *Type::getLabelTy(LLVMContext &C) { return &C.pImpl->LabelTy; }
171 Type *Type::getHalfTy(LLVMContext &C) { return &C.pImpl->HalfTy; }
172 Type *Type::getFloatTy(LLVMContext &C) { return &C.pImpl->FloatTy; }
173 Type *Type::getDoubleTy(LLVMContext &C) { return &C.pImpl->DoubleTy; }
174 Type *Type::getMetadataTy(LLVMContext &C) { return &C.pImpl->MetadataTy; }
175 Type *Type::getTokenTy(LLVMContext &C) { return &C.pImpl->TokenTy; }
176 Type *Type::getX86_FP80Ty(LLVMContext &C) { return &C.pImpl->X86_FP80Ty; }
177 Type *Type::getFP128Ty(LLVMContext &C) { return &C.pImpl->FP128Ty; }
178 Type *Type::getPPC_FP128Ty(LLVMContext &C) { return &C.pImpl->PPC_FP128Ty; }
179 Type *Type::getX86_MMXTy(LLVMContext &C) { return &C.pImpl->X86_MMXTy; }
181 IntegerType *Type::getInt1Ty(LLVMContext &C) { return &C.pImpl->Int1Ty; }
182 IntegerType *Type::getInt8Ty(LLVMContext &C) { return &C.pImpl->Int8Ty; }
183 IntegerType *Type::getInt16Ty(LLVMContext &C) { return &C.pImpl->Int16Ty; }
184 IntegerType *Type::getInt32Ty(LLVMContext &C) { return &C.pImpl->Int32Ty; }
185 IntegerType *Type::getInt64Ty(LLVMContext &C) { return &C.pImpl->Int64Ty; }
186 IntegerType *Type::getInt128Ty(LLVMContext &C) { return &C.pImpl->Int128Ty; }
188 IntegerType *Type::getIntNTy(LLVMContext &C, unsigned N) {
189 return IntegerType::get(C, N);
192 PointerType *Type::getHalfPtrTy(LLVMContext &C, unsigned AS) {
193 return getHalfTy(C)->getPointerTo(AS);
196 PointerType *Type::getFloatPtrTy(LLVMContext &C, unsigned AS) {
197 return getFloatTy(C)->getPointerTo(AS);
200 PointerType *Type::getDoublePtrTy(LLVMContext &C, unsigned AS) {
201 return getDoubleTy(C)->getPointerTo(AS);
204 PointerType *Type::getX86_FP80PtrTy(LLVMContext &C, unsigned AS) {
205 return getX86_FP80Ty(C)->getPointerTo(AS);
208 PointerType *Type::getFP128PtrTy(LLVMContext &C, unsigned AS) {
209 return getFP128Ty(C)->getPointerTo(AS);
212 PointerType *Type::getPPC_FP128PtrTy(LLVMContext &C, unsigned AS) {
213 return getPPC_FP128Ty(C)->getPointerTo(AS);
216 PointerType *Type::getX86_MMXPtrTy(LLVMContext &C, unsigned AS) {
217 return getX86_MMXTy(C)->getPointerTo(AS);
220 PointerType *Type::getIntNPtrTy(LLVMContext &C, unsigned N, unsigned AS) {
221 return getIntNTy(C, N)->getPointerTo(AS);
224 PointerType *Type::getInt1PtrTy(LLVMContext &C, unsigned AS) {
225 return getInt1Ty(C)->getPointerTo(AS);
228 PointerType *Type::getInt8PtrTy(LLVMContext &C, unsigned AS) {
229 return getInt8Ty(C)->getPointerTo(AS);
232 PointerType *Type::getInt16PtrTy(LLVMContext &C, unsigned AS) {
233 return getInt16Ty(C)->getPointerTo(AS);
236 PointerType *Type::getInt32PtrTy(LLVMContext &C, unsigned AS) {
237 return getInt32Ty(C)->getPointerTo(AS);
240 PointerType *Type::getInt64PtrTy(LLVMContext &C, unsigned AS) {
241 return getInt64Ty(C)->getPointerTo(AS);
245 //===----------------------------------------------------------------------===//
246 // IntegerType Implementation
247 //===----------------------------------------------------------------------===//
249 IntegerType *IntegerType::get(LLVMContext &C, unsigned NumBits) {
250 assert(NumBits >= MIN_INT_BITS && "bitwidth too small");
251 assert(NumBits <= MAX_INT_BITS && "bitwidth too large");
253 // Check for the built-in integer types
255 case 1: return cast<IntegerType>(Type::getInt1Ty(C));
256 case 8: return cast<IntegerType>(Type::getInt8Ty(C));
257 case 16: return cast<IntegerType>(Type::getInt16Ty(C));
258 case 32: return cast<IntegerType>(Type::getInt32Ty(C));
259 case 64: return cast<IntegerType>(Type::getInt64Ty(C));
260 case 128: return cast<IntegerType>(Type::getInt128Ty(C));
265 IntegerType *&Entry = C.pImpl->IntegerTypes[NumBits];
268 Entry = new (C.pImpl->TypeAllocator) IntegerType(C, NumBits);
273 bool IntegerType::isPowerOf2ByteWidth() const {
274 unsigned BitWidth = getBitWidth();
275 return (BitWidth > 7) && isPowerOf2_32(BitWidth);
278 APInt IntegerType::getMask() const {
279 return APInt::getAllOnesValue(getBitWidth());
282 //===----------------------------------------------------------------------===//
283 // FunctionType Implementation
284 //===----------------------------------------------------------------------===//
286 FunctionType::FunctionType(Type *Result, ArrayRef<Type*> Params,
288 : Type(Result->getContext(), FunctionTyID) {
289 Type **SubTys = reinterpret_cast<Type**>(this+1);
290 assert(isValidReturnType(Result) && "invalid return type for function");
291 setSubclassData(IsVarArgs);
295 for (unsigned i = 0, e = Params.size(); i != e; ++i) {
296 assert(isValidArgumentType(Params[i]) &&
297 "Not a valid type for function argument!");
298 SubTys[i+1] = Params[i];
301 ContainedTys = SubTys;
302 NumContainedTys = Params.size() + 1; // + 1 for result type
305 // FunctionType::get - The factory function for the FunctionType class.
306 FunctionType *FunctionType::get(Type *ReturnType,
307 ArrayRef<Type*> Params, bool isVarArg) {
308 LLVMContextImpl *pImpl = ReturnType->getContext().pImpl;
309 FunctionTypeKeyInfo::KeyTy Key(ReturnType, Params, isVarArg);
310 auto I = pImpl->FunctionTypes.find_as(Key);
313 if (I == pImpl->FunctionTypes.end()) {
314 FT = (FunctionType*) pImpl->TypeAllocator.
315 Allocate(sizeof(FunctionType) + sizeof(Type*) * (Params.size() + 1),
316 AlignOf<FunctionType>::Alignment);
317 new (FT) FunctionType(ReturnType, Params, isVarArg);
318 pImpl->FunctionTypes.insert(FT);
326 FunctionType *FunctionType::get(Type *Result, bool isVarArg) {
327 return get(Result, None, isVarArg);
330 /// isValidReturnType - Return true if the specified type is valid as a return
332 bool FunctionType::isValidReturnType(Type *RetTy) {
333 return !RetTy->isFunctionTy() && !RetTy->isLabelTy() &&
334 !RetTy->isMetadataTy();
337 /// isValidArgumentType - Return true if the specified type is valid as an
339 bool FunctionType::isValidArgumentType(Type *ArgTy) {
340 return ArgTy->isFirstClassType();
343 //===----------------------------------------------------------------------===//
344 // StructType Implementation
345 //===----------------------------------------------------------------------===//
347 // Primitive Constructors.
349 StructType *StructType::get(LLVMContext &Context, ArrayRef<Type*> ETypes,
351 LLVMContextImpl *pImpl = Context.pImpl;
352 AnonStructTypeKeyInfo::KeyTy Key(ETypes, isPacked);
353 auto I = pImpl->AnonStructTypes.find_as(Key);
356 if (I == pImpl->AnonStructTypes.end()) {
357 // Value not found. Create a new type!
358 ST = new (Context.pImpl->TypeAllocator) StructType(Context);
359 ST->setSubclassData(SCDB_IsLiteral); // Literal struct.
360 ST->setBody(ETypes, isPacked);
361 Context.pImpl->AnonStructTypes.insert(ST);
369 void StructType::setBody(ArrayRef<Type*> Elements, bool isPacked) {
370 assert(isOpaque() && "Struct body already set!");
372 setSubclassData(getSubclassData() | SCDB_HasBody);
374 setSubclassData(getSubclassData() | SCDB_Packed);
376 NumContainedTys = Elements.size();
378 if (Elements.empty()) {
379 ContainedTys = nullptr;
383 ContainedTys = Elements.copy(getContext().pImpl->TypeAllocator).data();
386 void StructType::setName(StringRef Name) {
387 if (Name == getName()) return;
389 StringMap<StructType *> &SymbolTable = getContext().pImpl->NamedStructTypes;
390 typedef StringMap<StructType *>::MapEntryTy EntryTy;
392 // If this struct already had a name, remove its symbol table entry. Don't
393 // delete the data yet because it may be part of the new name.
394 if (SymbolTableEntry)
395 SymbolTable.remove((EntryTy *)SymbolTableEntry);
397 // If this is just removing the name, we're done.
399 if (SymbolTableEntry) {
400 // Delete the old string data.
401 ((EntryTy *)SymbolTableEntry)->Destroy(SymbolTable.getAllocator());
402 SymbolTableEntry = nullptr;
407 // Look up the entry for the name.
409 getContext().pImpl->NamedStructTypes.insert(std::make_pair(Name, this));
411 // While we have a name collision, try a random rename.
412 if (!IterBool.second) {
413 SmallString<64> TempStr(Name);
414 TempStr.push_back('.');
415 raw_svector_ostream TmpStream(TempStr);
416 unsigned NameSize = Name.size();
419 TempStr.resize(NameSize + 1);
420 TmpStream << getContext().pImpl->NamedStructTypesUniqueID++;
422 IterBool = getContext().pImpl->NamedStructTypes.insert(
423 std::make_pair(TmpStream.str(), this));
424 } while (!IterBool.second);
427 // Delete the old string data.
428 if (SymbolTableEntry)
429 ((EntryTy *)SymbolTableEntry)->Destroy(SymbolTable.getAllocator());
430 SymbolTableEntry = &*IterBool.first;
433 //===----------------------------------------------------------------------===//
434 // StructType Helper functions.
436 StructType *StructType::create(LLVMContext &Context, StringRef Name) {
437 StructType *ST = new (Context.pImpl->TypeAllocator) StructType(Context);
443 StructType *StructType::get(LLVMContext &Context, bool isPacked) {
444 return get(Context, None, isPacked);
447 StructType *StructType::get(Type *type, ...) {
448 assert(type && "Cannot create a struct type with no elements with this");
449 LLVMContext &Ctx = type->getContext();
451 SmallVector<llvm::Type*, 8> StructFields;
454 StructFields.push_back(type);
455 type = va_arg(ap, llvm::Type*);
457 auto *Ret = llvm::StructType::get(Ctx, StructFields);
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 StructType *StructType::create(StringRef Name, Type *type, ...) {
491 assert(type && "Cannot create a struct type with no elements with this");
492 LLVMContext &Ctx = type->getContext();
494 SmallVector<llvm::Type*, 8> StructFields;
497 StructFields.push_back(type);
498 type = va_arg(ap, llvm::Type*);
500 auto *Ret = llvm::StructType::create(Ctx, StructFields, Name);
505 bool StructType::isSized(SmallPtrSetImpl<Type*> *Visited) const {
506 if ((getSubclassData() & SCDB_IsSized) != 0)
511 if (Visited && !Visited->insert(const_cast<StructType*>(this)).second)
514 // Okay, our struct is sized if all of the elements are, but if one of the
515 // elements is opaque, the struct isn't sized *yet*, but may become sized in
516 // the future, so just bail out without caching.
517 for (element_iterator I = element_begin(), E = element_end(); I != E; ++I)
518 if (!(*I)->isSized(Visited))
521 // Here we cheat a bit and cast away const-ness. The goal is to memoize when
522 // we find a sized type, as types can only move from opaque to sized, not the
524 const_cast<StructType*>(this)->setSubclassData(
525 getSubclassData() | SCDB_IsSized);
529 StringRef StructType::getName() const {
530 assert(!isLiteral() && "Literal structs never have names");
531 if (!SymbolTableEntry) return StringRef();
533 return ((StringMapEntry<StructType*> *)SymbolTableEntry)->getKey();
536 void StructType::setBody(Type *type, ...) {
537 assert(type && "Cannot create a struct type with no elements with this");
539 SmallVector<llvm::Type*, 8> StructFields;
542 StructFields.push_back(type);
543 type = va_arg(ap, llvm::Type*);
545 setBody(StructFields);
549 bool StructType::isValidElementType(Type *ElemTy) {
550 return !ElemTy->isVoidTy() && !ElemTy->isLabelTy() &&
551 !ElemTy->isMetadataTy() && !ElemTy->isFunctionTy() &&
552 !ElemTy->isTokenTy();
555 /// isLayoutIdentical - Return true if this is layout identical to the
556 /// specified struct.
557 bool StructType::isLayoutIdentical(StructType *Other) const {
558 if (this == Other) return true;
560 if (isPacked() != Other->isPacked())
563 return elements() == Other->elements();
566 /// getTypeByName - Return the type with the specified name, or null if there
567 /// is none by that name.
568 StructType *Module::getTypeByName(StringRef Name) const {
569 return getContext().pImpl->NamedStructTypes.lookup(Name);
573 //===----------------------------------------------------------------------===//
574 // CompositeType Implementation
575 //===----------------------------------------------------------------------===//
577 Type *CompositeType::getTypeAtIndex(const Value *V) const {
578 if (auto *STy = dyn_cast<StructType>(this)) {
580 (unsigned)cast<Constant>(V)->getUniqueInteger().getZExtValue();
581 assert(indexValid(Idx) && "Invalid structure index!");
582 return STy->getElementType(Idx);
585 return cast<SequentialType>(this)->getElementType();
588 Type *CompositeType::getTypeAtIndex(unsigned Idx) const{
589 if (auto *STy = dyn_cast<StructType>(this)) {
590 assert(indexValid(Idx) && "Invalid structure index!");
591 return STy->getElementType(Idx);
594 return cast<SequentialType>(this)->getElementType();
597 bool CompositeType::indexValid(const Value *V) const {
598 if (auto *STy = dyn_cast<StructType>(this)) {
599 // Structure indexes require (vectors of) 32-bit integer constants. In the
600 // vector case all of the indices must be equal.
601 if (!V->getType()->getScalarType()->isIntegerTy(32))
603 const Constant *C = dyn_cast<Constant>(V);
604 if (C && V->getType()->isVectorTy())
605 C = C->getSplatValue();
606 const ConstantInt *CU = dyn_cast_or_null<ConstantInt>(C);
607 return CU && CU->getZExtValue() < STy->getNumElements();
610 // Sequential types can be indexed by any integer.
611 return V->getType()->isIntOrIntVectorTy();
614 bool CompositeType::indexValid(unsigned Idx) const {
615 if (auto *STy = dyn_cast<StructType>(this))
616 return Idx < STy->getNumElements();
617 // Sequential types can be indexed by any integer.
622 //===----------------------------------------------------------------------===//
623 // ArrayType Implementation
624 //===----------------------------------------------------------------------===//
626 ArrayType::ArrayType(Type *ElType, uint64_t NumEl)
627 : SequentialType(ArrayTyID, ElType) {
631 ArrayType *ArrayType::get(Type *ElementType, uint64_t NumElements) {
632 assert(isValidElementType(ElementType) && "Invalid type for array element!");
634 LLVMContextImpl *pImpl = ElementType->getContext().pImpl;
636 pImpl->ArrayTypes[std::make_pair(ElementType, NumElements)];
639 Entry = new (pImpl->TypeAllocator) ArrayType(ElementType, NumElements);
643 bool ArrayType::isValidElementType(Type *ElemTy) {
644 return !ElemTy->isVoidTy() && !ElemTy->isLabelTy() &&
645 !ElemTy->isMetadataTy() && !ElemTy->isFunctionTy() &&
646 !ElemTy->isTokenTy();
649 //===----------------------------------------------------------------------===//
650 // VectorType Implementation
651 //===----------------------------------------------------------------------===//
653 VectorType::VectorType(Type *ElType, unsigned NumEl)
654 : SequentialType(VectorTyID, ElType) {
658 VectorType *VectorType::get(Type *ElementType, unsigned NumElements) {
659 assert(NumElements > 0 && "#Elements of a VectorType must be greater than 0");
660 assert(isValidElementType(ElementType) && "Element type of a VectorType must "
661 "be an integer, floating point, or "
664 LLVMContextImpl *pImpl = ElementType->getContext().pImpl;
665 VectorType *&Entry = ElementType->getContext().pImpl
666 ->VectorTypes[std::make_pair(ElementType, NumElements)];
669 Entry = new (pImpl->TypeAllocator) VectorType(ElementType, NumElements);
673 bool VectorType::isValidElementType(Type *ElemTy) {
674 return ElemTy->isIntegerTy() || ElemTy->isFloatingPointTy() ||
675 ElemTy->isPointerTy();
678 //===----------------------------------------------------------------------===//
679 // PointerType Implementation
680 //===----------------------------------------------------------------------===//
682 PointerType *PointerType::get(Type *EltTy, unsigned AddressSpace) {
683 assert(EltTy && "Can't get a pointer to <null> type!");
684 assert(isValidElementType(EltTy) && "Invalid type for pointer element!");
686 LLVMContextImpl *CImpl = EltTy->getContext().pImpl;
688 // Since AddressSpace #0 is the common case, we special case it.
689 PointerType *&Entry = AddressSpace == 0 ? CImpl->PointerTypes[EltTy]
690 : CImpl->ASPointerTypes[std::make_pair(EltTy, AddressSpace)];
693 Entry = new (CImpl->TypeAllocator) PointerType(EltTy, AddressSpace);
698 PointerType::PointerType(Type *E, unsigned AddrSpace)
699 : SequentialType(PointerTyID, E) {
701 const unsigned oldNCT = NumContainedTys;
703 setSubclassData(AddrSpace);
704 // Check for miscompile. PR11652.
705 assert(oldNCT == NumContainedTys && "bitfield written out of bounds?");
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();