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);
43 /// getScalarType - If this is a vector type, return the element type,
44 /// otherwise return this.
45 Type *Type::getScalarType() const {
46 if (auto *VTy = dyn_cast<VectorType>(this))
47 return VTy->getElementType();
48 return const_cast<Type*>(this);
51 /// isIntegerTy - Return true if this is an IntegerType of the specified width.
52 bool Type::isIntegerTy(unsigned Bitwidth) const {
53 return isIntegerTy() && cast<IntegerType>(this)->getBitWidth() == Bitwidth;
56 // canLosslesslyBitCastTo - Return true if this type can be converted to
57 // 'Ty' without any reinterpretation of bits. For example, i8* to i32*.
59 bool Type::canLosslesslyBitCastTo(Type *Ty) const {
60 // Identity cast means no change so return true
64 // They are not convertible unless they are at least first class types
65 if (!this->isFirstClassType() || !Ty->isFirstClassType())
68 // Vector -> Vector conversions are always lossless if the two vector types
69 // have the same size, otherwise not. Also, 64-bit vector types can be
70 // converted to x86mmx.
71 if (auto *thisPTy = dyn_cast<VectorType>(this)) {
72 if (auto *thatPTy = dyn_cast<VectorType>(Ty))
73 return thisPTy->getBitWidth() == thatPTy->getBitWidth();
74 if (Ty->getTypeID() == Type::X86_MMXTyID &&
75 thisPTy->getBitWidth() == 64)
79 if (this->getTypeID() == Type::X86_MMXTyID)
80 if (auto *thatPTy = dyn_cast<VectorType>(Ty))
81 if (thatPTy->getBitWidth() == 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 unsigned Type::getPrimitiveSizeInBits() const {
114 switch (getTypeID()) {
115 case Type::HalfTyID: return 16;
116 case Type::FloatTyID: return 32;
117 case Type::DoubleTyID: return 64;
118 case Type::X86_FP80TyID: return 80;
119 case Type::FP128TyID: return 128;
120 case Type::PPC_FP128TyID: return 128;
121 case Type::X86_MMXTyID: return 64;
122 case Type::IntegerTyID: return cast<IntegerType>(this)->getBitWidth();
123 case Type::VectorTyID: return cast<VectorType>(this)->getBitWidth();
128 /// getScalarSizeInBits - If this is a vector type, return the
129 /// getPrimitiveSizeInBits value for the element type. Otherwise return the
130 /// getPrimitiveSizeInBits value for this type.
131 unsigned Type::getScalarSizeInBits() const {
132 return getScalarType()->getPrimitiveSizeInBits();
135 /// getFPMantissaWidth - Return the width of the mantissa of this type. This
136 /// is only valid on floating point types. If the FP type does not
137 /// have a stable mantissa (e.g. ppc long double), this method returns -1.
138 int Type::getFPMantissaWidth() const {
139 if (auto *VTy = dyn_cast<VectorType>(this))
140 return VTy->getElementType()->getFPMantissaWidth();
141 assert(isFloatingPointTy() && "Not a floating point type!");
142 if (getTypeID() == HalfTyID) return 11;
143 if (getTypeID() == FloatTyID) return 24;
144 if (getTypeID() == DoubleTyID) return 53;
145 if (getTypeID() == X86_FP80TyID) return 64;
146 if (getTypeID() == FP128TyID) return 113;
147 assert(getTypeID() == PPC_FP128TyID && "unknown fp type");
151 /// isSizedDerivedType - Derived types like structures and arrays are sized
152 /// iff all of the members of the type are sized as well. Since asking for
153 /// their size is relatively uncommon, move this operation out of line.
154 bool Type::isSizedDerivedType(SmallPtrSetImpl<Type*> *Visited) const {
155 if (auto *ATy = dyn_cast<ArrayType>(this))
156 return ATy->getElementType()->isSized(Visited);
158 if (auto *VTy = dyn_cast<VectorType>(this))
159 return VTy->getElementType()->isSized(Visited);
161 return cast<StructType>(this)->isSized(Visited);
164 //===----------------------------------------------------------------------===//
165 // Subclass Helper Methods
166 //===----------------------------------------------------------------------===//
168 unsigned Type::getIntegerBitWidth() const {
169 return cast<IntegerType>(this)->getBitWidth();
172 bool Type::isFunctionVarArg() const {
173 return cast<FunctionType>(this)->isVarArg();
176 Type *Type::getFunctionParamType(unsigned i) const {
177 return cast<FunctionType>(this)->getParamType(i);
180 unsigned Type::getFunctionNumParams() const {
181 return cast<FunctionType>(this)->getNumParams();
184 StringRef Type::getStructName() const {
185 return cast<StructType>(this)->getName();
188 unsigned Type::getStructNumElements() const {
189 return cast<StructType>(this)->getNumElements();
192 Type *Type::getStructElementType(unsigned N) const {
193 return cast<StructType>(this)->getElementType(N);
196 Type *Type::getSequentialElementType() const {
197 return cast<SequentialType>(this)->getElementType();
200 uint64_t Type::getArrayNumElements() const {
201 return cast<ArrayType>(this)->getNumElements();
204 unsigned Type::getVectorNumElements() const {
205 return cast<VectorType>(this)->getNumElements();
208 unsigned Type::getPointerAddressSpace() const {
209 return cast<PointerType>(getScalarType())->getAddressSpace();
213 //===----------------------------------------------------------------------===//
214 // Primitive 'Type' data
215 //===----------------------------------------------------------------------===//
217 Type *Type::getVoidTy(LLVMContext &C) { return &C.pImpl->VoidTy; }
218 Type *Type::getLabelTy(LLVMContext &C) { return &C.pImpl->LabelTy; }
219 Type *Type::getHalfTy(LLVMContext &C) { return &C.pImpl->HalfTy; }
220 Type *Type::getFloatTy(LLVMContext &C) { return &C.pImpl->FloatTy; }
221 Type *Type::getDoubleTy(LLVMContext &C) { return &C.pImpl->DoubleTy; }
222 Type *Type::getMetadataTy(LLVMContext &C) { return &C.pImpl->MetadataTy; }
223 Type *Type::getX86_FP80Ty(LLVMContext &C) { return &C.pImpl->X86_FP80Ty; }
224 Type *Type::getFP128Ty(LLVMContext &C) { return &C.pImpl->FP128Ty; }
225 Type *Type::getPPC_FP128Ty(LLVMContext &C) { return &C.pImpl->PPC_FP128Ty; }
226 Type *Type::getX86_MMXTy(LLVMContext &C) { return &C.pImpl->X86_MMXTy; }
228 IntegerType *Type::getInt1Ty(LLVMContext &C) { return &C.pImpl->Int1Ty; }
229 IntegerType *Type::getInt8Ty(LLVMContext &C) { return &C.pImpl->Int8Ty; }
230 IntegerType *Type::getInt16Ty(LLVMContext &C) { return &C.pImpl->Int16Ty; }
231 IntegerType *Type::getInt32Ty(LLVMContext &C) { return &C.pImpl->Int32Ty; }
232 IntegerType *Type::getInt64Ty(LLVMContext &C) { return &C.pImpl->Int64Ty; }
233 IntegerType *Type::getInt128Ty(LLVMContext &C) { return &C.pImpl->Int128Ty; }
235 IntegerType *Type::getIntNTy(LLVMContext &C, unsigned N) {
236 return IntegerType::get(C, N);
239 PointerType *Type::getHalfPtrTy(LLVMContext &C, unsigned AS) {
240 return getHalfTy(C)->getPointerTo(AS);
243 PointerType *Type::getFloatPtrTy(LLVMContext &C, unsigned AS) {
244 return getFloatTy(C)->getPointerTo(AS);
247 PointerType *Type::getDoublePtrTy(LLVMContext &C, unsigned AS) {
248 return getDoubleTy(C)->getPointerTo(AS);
251 PointerType *Type::getX86_FP80PtrTy(LLVMContext &C, unsigned AS) {
252 return getX86_FP80Ty(C)->getPointerTo(AS);
255 PointerType *Type::getFP128PtrTy(LLVMContext &C, unsigned AS) {
256 return getFP128Ty(C)->getPointerTo(AS);
259 PointerType *Type::getPPC_FP128PtrTy(LLVMContext &C, unsigned AS) {
260 return getPPC_FP128Ty(C)->getPointerTo(AS);
263 PointerType *Type::getX86_MMXPtrTy(LLVMContext &C, unsigned AS) {
264 return getX86_MMXTy(C)->getPointerTo(AS);
267 PointerType *Type::getIntNPtrTy(LLVMContext &C, unsigned N, unsigned AS) {
268 return getIntNTy(C, N)->getPointerTo(AS);
271 PointerType *Type::getInt1PtrTy(LLVMContext &C, unsigned AS) {
272 return getInt1Ty(C)->getPointerTo(AS);
275 PointerType *Type::getInt8PtrTy(LLVMContext &C, unsigned AS) {
276 return getInt8Ty(C)->getPointerTo(AS);
279 PointerType *Type::getInt16PtrTy(LLVMContext &C, unsigned AS) {
280 return getInt16Ty(C)->getPointerTo(AS);
283 PointerType *Type::getInt32PtrTy(LLVMContext &C, unsigned AS) {
284 return getInt32Ty(C)->getPointerTo(AS);
287 PointerType *Type::getInt64PtrTy(LLVMContext &C, unsigned AS) {
288 return getInt64Ty(C)->getPointerTo(AS);
292 //===----------------------------------------------------------------------===//
293 // IntegerType Implementation
294 //===----------------------------------------------------------------------===//
296 IntegerType *IntegerType::get(LLVMContext &C, unsigned NumBits) {
297 assert(NumBits >= MIN_INT_BITS && "bitwidth too small");
298 assert(NumBits <= MAX_INT_BITS && "bitwidth too large");
300 // Check for the built-in integer types
302 case 1: return cast<IntegerType>(Type::getInt1Ty(C));
303 case 8: return cast<IntegerType>(Type::getInt8Ty(C));
304 case 16: return cast<IntegerType>(Type::getInt16Ty(C));
305 case 32: return cast<IntegerType>(Type::getInt32Ty(C));
306 case 64: return cast<IntegerType>(Type::getInt64Ty(C));
307 case 128: return cast<IntegerType>(Type::getInt128Ty(C));
312 IntegerType *&Entry = C.pImpl->IntegerTypes[NumBits];
315 Entry = new (C.pImpl->TypeAllocator) IntegerType(C, NumBits);
320 bool IntegerType::isPowerOf2ByteWidth() const {
321 unsigned BitWidth = getBitWidth();
322 return (BitWidth > 7) && isPowerOf2_32(BitWidth);
325 APInt IntegerType::getMask() const {
326 return APInt::getAllOnesValue(getBitWidth());
329 //===----------------------------------------------------------------------===//
330 // FunctionType Implementation
331 //===----------------------------------------------------------------------===//
333 FunctionType::FunctionType(Type *Result, ArrayRef<Type*> Params,
335 : Type(Result->getContext(), FunctionTyID) {
336 Type **SubTys = reinterpret_cast<Type**>(this+1);
337 assert(isValidReturnType(Result) && "invalid return type for function");
338 setSubclassData(IsVarArgs);
342 for (unsigned i = 0, e = Params.size(); i != e; ++i) {
343 assert(isValidArgumentType(Params[i]) &&
344 "Not a valid type for function argument!");
345 SubTys[i+1] = Params[i];
348 ContainedTys = SubTys;
349 NumContainedTys = Params.size() + 1; // + 1 for result type
352 // FunctionType::get - The factory function for the FunctionType class.
353 FunctionType *FunctionType::get(Type *ReturnType,
354 ArrayRef<Type*> Params, bool isVarArg) {
355 LLVMContextImpl *pImpl = ReturnType->getContext().pImpl;
356 FunctionTypeKeyInfo::KeyTy Key(ReturnType, Params, isVarArg);
357 auto I = pImpl->FunctionTypes.find_as(Key);
360 if (I == pImpl->FunctionTypes.end()) {
361 FT = (FunctionType*) pImpl->TypeAllocator.
362 Allocate(sizeof(FunctionType) + sizeof(Type*) * (Params.size() + 1),
363 AlignOf<FunctionType>::Alignment);
364 new (FT) FunctionType(ReturnType, Params, isVarArg);
365 pImpl->FunctionTypes.insert(FT);
373 FunctionType *FunctionType::get(Type *Result, bool isVarArg) {
374 return get(Result, None, isVarArg);
377 /// isValidReturnType - Return true if the specified type is valid as a return
379 bool FunctionType::isValidReturnType(Type *RetTy) {
380 return !RetTy->isFunctionTy() && !RetTy->isLabelTy() &&
381 !RetTy->isMetadataTy();
384 /// isValidArgumentType - Return true if the specified type is valid as an
386 bool FunctionType::isValidArgumentType(Type *ArgTy) {
387 return ArgTy->isFirstClassType();
390 //===----------------------------------------------------------------------===//
391 // StructType Implementation
392 //===----------------------------------------------------------------------===//
394 // Primitive Constructors.
396 StructType *StructType::get(LLVMContext &Context, ArrayRef<Type*> ETypes,
398 LLVMContextImpl *pImpl = Context.pImpl;
399 AnonStructTypeKeyInfo::KeyTy Key(ETypes, isPacked);
400 auto I = pImpl->AnonStructTypes.find_as(Key);
403 if (I == pImpl->AnonStructTypes.end()) {
404 // Value not found. Create a new type!
405 ST = new (Context.pImpl->TypeAllocator) StructType(Context);
406 ST->setSubclassData(SCDB_IsLiteral); // Literal struct.
407 ST->setBody(ETypes, isPacked);
408 Context.pImpl->AnonStructTypes.insert(ST);
416 void StructType::setBody(ArrayRef<Type*> Elements, bool isPacked) {
417 assert(isOpaque() && "Struct body already set!");
419 setSubclassData(getSubclassData() | SCDB_HasBody);
421 setSubclassData(getSubclassData() | SCDB_Packed);
423 NumContainedTys = Elements.size();
425 if (Elements.empty()) {
426 ContainedTys = nullptr;
430 ContainedTys = Elements.copy(getContext().pImpl->TypeAllocator).data();
433 void StructType::setName(StringRef Name) {
434 if (Name == getName()) return;
436 StringMap<StructType *> &SymbolTable = getContext().pImpl->NamedStructTypes;
437 typedef StringMap<StructType *>::MapEntryTy EntryTy;
439 // If this struct already had a name, remove its symbol table entry. Don't
440 // delete the data yet because it may be part of the new name.
441 if (SymbolTableEntry)
442 SymbolTable.remove((EntryTy *)SymbolTableEntry);
444 // If this is just removing the name, we're done.
446 if (SymbolTableEntry) {
447 // Delete the old string data.
448 ((EntryTy *)SymbolTableEntry)->Destroy(SymbolTable.getAllocator());
449 SymbolTableEntry = nullptr;
454 // Look up the entry for the name.
456 getContext().pImpl->NamedStructTypes.insert(std::make_pair(Name, this));
458 // While we have a name collision, try a random rename.
459 if (!IterBool.second) {
460 SmallString<64> TempStr(Name);
461 TempStr.push_back('.');
462 raw_svector_ostream TmpStream(TempStr);
463 unsigned NameSize = Name.size();
466 TempStr.resize(NameSize + 1);
467 TmpStream << getContext().pImpl->NamedStructTypesUniqueID++;
469 IterBool = getContext().pImpl->NamedStructTypes.insert(
470 std::make_pair(TmpStream.str(), this));
471 } while (!IterBool.second);
474 // Delete the old string data.
475 if (SymbolTableEntry)
476 ((EntryTy *)SymbolTableEntry)->Destroy(SymbolTable.getAllocator());
477 SymbolTableEntry = &*IterBool.first;
480 //===----------------------------------------------------------------------===//
481 // StructType Helper functions.
483 StructType *StructType::create(LLVMContext &Context, StringRef Name) {
484 StructType *ST = new (Context.pImpl->TypeAllocator) StructType(Context);
490 StructType *StructType::get(LLVMContext &Context, bool isPacked) {
491 return get(Context, None, isPacked);
494 StructType *StructType::get(Type *type, ...) {
495 assert(type && "Cannot create a struct type with no elements with this");
496 LLVMContext &Ctx = type->getContext();
498 SmallVector<llvm::Type*, 8> StructFields;
501 StructFields.push_back(type);
502 type = va_arg(ap, llvm::Type*);
504 auto *Ret = llvm::StructType::get(Ctx, StructFields);
509 StructType *StructType::create(LLVMContext &Context, ArrayRef<Type*> Elements,
510 StringRef Name, bool isPacked) {
511 StructType *ST = create(Context, Name);
512 ST->setBody(Elements, isPacked);
516 StructType *StructType::create(LLVMContext &Context, ArrayRef<Type*> Elements) {
517 return create(Context, Elements, StringRef());
520 StructType *StructType::create(LLVMContext &Context) {
521 return create(Context, StringRef());
524 StructType *StructType::create(ArrayRef<Type*> Elements, StringRef Name,
526 assert(!Elements.empty() &&
527 "This method may not be invoked with an empty list");
528 return create(Elements[0]->getContext(), Elements, Name, isPacked);
531 StructType *StructType::create(ArrayRef<Type*> Elements) {
532 assert(!Elements.empty() &&
533 "This method may not be invoked with an empty list");
534 return create(Elements[0]->getContext(), Elements, StringRef());
537 StructType *StructType::create(StringRef Name, Type *type, ...) {
538 assert(type && "Cannot create a struct type with no elements with this");
539 LLVMContext &Ctx = type->getContext();
541 SmallVector<llvm::Type*, 8> StructFields;
544 StructFields.push_back(type);
545 type = va_arg(ap, llvm::Type*);
547 auto *Ret = llvm::StructType::create(Ctx, StructFields, Name);
552 bool StructType::isSized(SmallPtrSetImpl<Type*> *Visited) const {
553 if ((getSubclassData() & SCDB_IsSized) != 0)
558 if (Visited && !Visited->insert(const_cast<StructType*>(this)).second)
561 // Okay, our struct is sized if all of the elements are, but if one of the
562 // elements is opaque, the struct isn't sized *yet*, but may become sized in
563 // the future, so just bail out without caching.
564 for (element_iterator I = element_begin(), E = element_end(); I != E; ++I)
565 if (!(*I)->isSized(Visited))
568 // Here we cheat a bit and cast away const-ness. The goal is to memoize when
569 // we find a sized type, as types can only move from opaque to sized, not the
571 const_cast<StructType*>(this)->setSubclassData(
572 getSubclassData() | SCDB_IsSized);
576 StringRef StructType::getName() const {
577 assert(!isLiteral() && "Literal structs never have names");
578 if (!SymbolTableEntry) return StringRef();
580 return ((StringMapEntry<StructType*> *)SymbolTableEntry)->getKey();
583 void StructType::setBody(Type *type, ...) {
584 assert(type && "Cannot create a struct type with no elements with this");
586 SmallVector<llvm::Type*, 8> StructFields;
589 StructFields.push_back(type);
590 type = va_arg(ap, llvm::Type*);
592 setBody(StructFields);
596 bool StructType::isValidElementType(Type *ElemTy) {
597 return !ElemTy->isVoidTy() && !ElemTy->isLabelTy() &&
598 !ElemTy->isMetadataTy() && !ElemTy->isFunctionTy();
601 /// isLayoutIdentical - Return true if this is layout identical to the
602 /// specified struct.
603 bool StructType::isLayoutIdentical(StructType *Other) const {
604 if (this == Other) return true;
606 if (isPacked() != Other->isPacked() ||
607 getNumElements() != Other->getNumElements())
610 return element_begin() &&
611 std::equal(element_begin(), element_end(), Other->element_begin());
614 /// getTypeByName - Return the type with the specified name, or null if there
615 /// is none by that name.
616 StructType *Module::getTypeByName(StringRef Name) const {
617 return getContext().pImpl->NamedStructTypes.lookup(Name);
621 //===----------------------------------------------------------------------===//
622 // CompositeType Implementation
623 //===----------------------------------------------------------------------===//
625 Type *CompositeType::getTypeAtIndex(const Value *V) const {
626 if (auto *STy = dyn_cast<StructType>(this)) {
628 (unsigned)cast<Constant>(V)->getUniqueInteger().getZExtValue();
629 assert(indexValid(Idx) && "Invalid structure index!");
630 return STy->getElementType(Idx);
633 return cast<SequentialType>(this)->getElementType();
636 Type *CompositeType::getTypeAtIndex(unsigned Idx) const{
637 if (auto *STy = dyn_cast<StructType>(this)) {
638 assert(indexValid(Idx) && "Invalid structure index!");
639 return STy->getElementType(Idx);
642 return cast<SequentialType>(this)->getElementType();
645 bool CompositeType::indexValid(const Value *V) const {
646 if (auto *STy = dyn_cast<StructType>(this)) {
647 // Structure indexes require (vectors of) 32-bit integer constants. In the
648 // vector case all of the indices must be equal.
649 if (!V->getType()->getScalarType()->isIntegerTy(32))
651 const Constant *C = dyn_cast<Constant>(V);
652 if (C && V->getType()->isVectorTy())
653 C = C->getSplatValue();
654 const ConstantInt *CU = dyn_cast_or_null<ConstantInt>(C);
655 return CU && CU->getZExtValue() < STy->getNumElements();
658 // Sequential types can be indexed by any integer.
659 return V->getType()->isIntOrIntVectorTy();
662 bool CompositeType::indexValid(unsigned Idx) const {
663 if (auto *STy = dyn_cast<StructType>(this))
664 return Idx < STy->getNumElements();
665 // Sequential types can be indexed by any integer.
670 //===----------------------------------------------------------------------===//
671 // ArrayType Implementation
672 //===----------------------------------------------------------------------===//
674 ArrayType::ArrayType(Type *ElType, uint64_t NumEl)
675 : SequentialType(ArrayTyID, ElType) {
679 ArrayType *ArrayType::get(Type *ElementType, uint64_t NumElements) {
680 assert(isValidElementType(ElementType) && "Invalid type for array element!");
682 LLVMContextImpl *pImpl = ElementType->getContext().pImpl;
684 pImpl->ArrayTypes[std::make_pair(ElementType, NumElements)];
687 Entry = new (pImpl->TypeAllocator) ArrayType(ElementType, NumElements);
691 bool ArrayType::isValidElementType(Type *ElemTy) {
692 return !ElemTy->isVoidTy() && !ElemTy->isLabelTy() &&
693 !ElemTy->isMetadataTy() && !ElemTy->isFunctionTy();
696 //===----------------------------------------------------------------------===//
697 // VectorType Implementation
698 //===----------------------------------------------------------------------===//
700 VectorType::VectorType(Type *ElType, unsigned NumEl)
701 : SequentialType(VectorTyID, ElType) {
705 VectorType *VectorType::get(Type *ElementType, unsigned NumElements) {
706 assert(NumElements > 0 && "#Elements of a VectorType must be greater than 0");
707 assert(isValidElementType(ElementType) && "Element type of a VectorType must "
708 "be an integer, floating point, or "
711 LLVMContextImpl *pImpl = ElementType->getContext().pImpl;
712 VectorType *&Entry = ElementType->getContext().pImpl
713 ->VectorTypes[std::make_pair(ElementType, NumElements)];
716 Entry = new (pImpl->TypeAllocator) VectorType(ElementType, NumElements);
720 bool VectorType::isValidElementType(Type *ElemTy) {
721 return ElemTy->isIntegerTy() || ElemTy->isFloatingPointTy() ||
722 ElemTy->isPointerTy();
725 //===----------------------------------------------------------------------===//
726 // PointerType Implementation
727 //===----------------------------------------------------------------------===//
729 PointerType *PointerType::get(Type *EltTy, unsigned AddressSpace) {
730 assert(EltTy && "Can't get a pointer to <null> type!");
731 assert(isValidElementType(EltTy) && "Invalid type for pointer element!");
733 LLVMContextImpl *CImpl = EltTy->getContext().pImpl;
735 // Since AddressSpace #0 is the common case, we special case it.
736 PointerType *&Entry = AddressSpace == 0 ? CImpl->PointerTypes[EltTy]
737 : CImpl->ASPointerTypes[std::make_pair(EltTy, AddressSpace)];
740 Entry = new (CImpl->TypeAllocator) PointerType(EltTy, AddressSpace);
745 PointerType::PointerType(Type *E, unsigned AddrSpace)
746 : SequentialType(PointerTyID, E) {
748 const unsigned oldNCT = NumContainedTys;
750 setSubclassData(AddrSpace);
751 // Check for miscompile. PR11652.
752 assert(oldNCT == NumContainedTys && "bitfield written out of bounds?");
755 PointerType *Type::getPointerTo(unsigned addrs) const {
756 return PointerType::get(const_cast<Type*>(this), addrs);
759 bool PointerType::isValidElementType(Type *ElemTy) {
760 return !ElemTy->isVoidTy() && !ElemTy->isLabelTy() &&
761 !ElemTy->isMetadataTy();
764 bool PointerType::isLoadableOrStorableType(Type *ElemTy) {
765 return isValidElementType(ElemTy) && !ElemTy->isFunctionTy();