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 VMCore library.
12 //===----------------------------------------------------------------------===//
14 #include "LLVMContextImpl.h"
15 #include "llvm/Module.h"
18 #include "llvm/ADT/SmallString.h"
21 //===----------------------------------------------------------------------===//
22 // Type Class Implementation
23 //===----------------------------------------------------------------------===//
25 Type *Type::getPrimitiveType(LLVMContext &C, TypeID IDNumber) {
27 case VoidTyID : return getVoidTy(C);
28 case HalfTyID : return getHalfTy(C);
29 case FloatTyID : return getFloatTy(C);
30 case DoubleTyID : return getDoubleTy(C);
31 case X86_FP80TyID : return getX86_FP80Ty(C);
32 case FP128TyID : return getFP128Ty(C);
33 case PPC_FP128TyID : return getPPC_FP128Ty(C);
34 case LabelTyID : return getLabelTy(C);
35 case MetadataTyID : return getMetadataTy(C);
36 case X86_MMXTyID : return getX86_MMXTy(C);
42 /// getScalarType - If this is a vector type, return the element type,
43 /// otherwise return this.
44 Type *Type::getScalarType() {
45 if (VectorType *VTy = dyn_cast<VectorType>(this))
46 return VTy->getElementType();
50 /// isIntegerTy - Return true if this is an IntegerType of the specified width.
51 bool Type::isIntegerTy(unsigned Bitwidth) const {
52 return isIntegerTy() && cast<IntegerType>(this)->getBitWidth() == Bitwidth;
55 /// isIntOrIntVectorTy - Return true if this is an integer type or a vector of
58 bool Type::isIntOrIntVectorTy() const {
61 if (getTypeID() != Type::VectorTyID) return false;
63 return cast<VectorType>(this)->getElementType()->isIntegerTy();
66 /// isFPOrFPVectorTy - Return true if this is a FP type or a vector of FP types.
68 bool Type::isFPOrFPVectorTy() const {
69 if (getTypeID() == Type::HalfTyID || getTypeID() == Type::FloatTyID ||
70 getTypeID() == Type::DoubleTyID ||
71 getTypeID() == Type::FP128TyID || getTypeID() == Type::X86_FP80TyID ||
72 getTypeID() == Type::PPC_FP128TyID)
74 if (getTypeID() != Type::VectorTyID) return false;
76 return cast<VectorType>(this)->getElementType()->isFloatingPointTy();
79 // canLosslesslyBitCastTo - Return true if this type can be converted to
80 // 'Ty' without any reinterpretation of bits. For example, i8* to i32*.
82 bool Type::canLosslesslyBitCastTo(Type *Ty) const {
83 // Identity cast means no change so return true
87 // They are not convertible unless they are at least first class types
88 if (!this->isFirstClassType() || !Ty->isFirstClassType())
91 // Vector -> Vector conversions are always lossless if the two vector types
92 // have the same size, otherwise not. Also, 64-bit vector types can be
93 // converted to x86mmx.
94 if (const VectorType *thisPTy = dyn_cast<VectorType>(this)) {
95 if (const VectorType *thatPTy = dyn_cast<VectorType>(Ty))
96 return thisPTy->getBitWidth() == thatPTy->getBitWidth();
97 if (Ty->getTypeID() == Type::X86_MMXTyID &&
98 thisPTy->getBitWidth() == 64)
102 if (this->getTypeID() == Type::X86_MMXTyID)
103 if (const VectorType *thatPTy = dyn_cast<VectorType>(Ty))
104 if (thatPTy->getBitWidth() == 64)
107 // At this point we have only various mismatches of the first class types
108 // remaining and ptr->ptr. Just select the lossless conversions. Everything
109 // else is not lossless.
110 if (this->isPointerTy())
111 return Ty->isPointerTy();
112 return false; // Other types have no identity values
115 bool Type::isEmptyTy() const {
116 const ArrayType *ATy = dyn_cast<ArrayType>(this);
118 unsigned NumElements = ATy->getNumElements();
119 return NumElements == 0 || ATy->getElementType()->isEmptyTy();
122 const StructType *STy = dyn_cast<StructType>(this);
124 unsigned NumElements = STy->getNumElements();
125 for (unsigned i = 0; i < NumElements; ++i)
126 if (!STy->getElementType(i)->isEmptyTy())
134 unsigned Type::getPrimitiveSizeInBits() const {
135 switch (getTypeID()) {
136 case Type::HalfTyID: return 16;
137 case Type::FloatTyID: return 32;
138 case Type::DoubleTyID: return 64;
139 case Type::X86_FP80TyID: return 80;
140 case Type::FP128TyID: return 128;
141 case Type::PPC_FP128TyID: return 128;
142 case Type::X86_MMXTyID: return 64;
143 case Type::IntegerTyID: return cast<IntegerType>(this)->getBitWidth();
144 case Type::VectorTyID: return cast<VectorType>(this)->getBitWidth();
149 /// getScalarSizeInBits - If this is a vector type, return the
150 /// getPrimitiveSizeInBits value for the element type. Otherwise return the
151 /// getPrimitiveSizeInBits value for this type.
152 unsigned Type::getScalarSizeInBits() {
153 return getScalarType()->getPrimitiveSizeInBits();
156 /// getFPMantissaWidth - Return the width of the mantissa of this type. This
157 /// is only valid on floating point types. If the FP type does not
158 /// have a stable mantissa (e.g. ppc long double), this method returns -1.
159 int Type::getFPMantissaWidth() const {
160 if (const VectorType *VTy = dyn_cast<VectorType>(this))
161 return VTy->getElementType()->getFPMantissaWidth();
162 assert(isFloatingPointTy() && "Not a floating point type!");
163 if (getTypeID() == HalfTyID) return 11;
164 if (getTypeID() == FloatTyID) return 24;
165 if (getTypeID() == DoubleTyID) return 53;
166 if (getTypeID() == X86_FP80TyID) return 64;
167 if (getTypeID() == FP128TyID) return 113;
168 assert(getTypeID() == PPC_FP128TyID && "unknown fp type");
172 /// isSizedDerivedType - Derived types like structures and arrays are sized
173 /// iff all of the members of the type are sized as well. Since asking for
174 /// their size is relatively uncommon, move this operation out of line.
175 bool Type::isSizedDerivedType() const {
176 if (this->isIntegerTy())
179 if (const ArrayType *ATy = dyn_cast<ArrayType>(this))
180 return ATy->getElementType()->isSized();
182 if (const VectorType *VTy = dyn_cast<VectorType>(this))
183 return VTy->getElementType()->isSized();
185 if (!this->isStructTy())
188 // Opaque structs have no size.
189 if (cast<StructType>(this)->isOpaque())
192 // Okay, our struct is sized if all of the elements are.
193 for (subtype_iterator I = subtype_begin(), E = subtype_end(); I != E; ++I)
194 if (!(*I)->isSized())
200 //===----------------------------------------------------------------------===//
201 // Subclass Helper Methods
202 //===----------------------------------------------------------------------===//
204 unsigned Type::getIntegerBitWidth() const {
205 return cast<IntegerType>(this)->getBitWidth();
208 bool Type::isFunctionVarArg() const {
209 return cast<FunctionType>(this)->isVarArg();
212 Type *Type::getFunctionParamType(unsigned i) const {
213 return cast<FunctionType>(this)->getParamType(i);
216 unsigned Type::getFunctionNumParams() const {
217 return cast<FunctionType>(this)->getNumParams();
220 StringRef Type::getStructName() const {
221 return cast<StructType>(this)->getName();
224 unsigned Type::getStructNumElements() const {
225 return cast<StructType>(this)->getNumElements();
228 Type *Type::getStructElementType(unsigned N) const {
229 return cast<StructType>(this)->getElementType(N);
234 Type *Type::getSequentialElementType() const {
235 return cast<SequentialType>(this)->getElementType();
238 uint64_t Type::getArrayNumElements() const {
239 return cast<ArrayType>(this)->getNumElements();
242 unsigned Type::getVectorNumElements() const {
243 return cast<VectorType>(this)->getNumElements();
246 unsigned Type::getPointerAddressSpace() const {
247 return cast<PointerType>(this)->getAddressSpace();
253 //===----------------------------------------------------------------------===//
254 // Primitive 'Type' data
255 //===----------------------------------------------------------------------===//
257 Type *Type::getVoidTy(LLVMContext &C) { return &C.pImpl->VoidTy; }
258 Type *Type::getLabelTy(LLVMContext &C) { return &C.pImpl->LabelTy; }
259 Type *Type::getHalfTy(LLVMContext &C) { return &C.pImpl->HalfTy; }
260 Type *Type::getFloatTy(LLVMContext &C) { return &C.pImpl->FloatTy; }
261 Type *Type::getDoubleTy(LLVMContext &C) { return &C.pImpl->DoubleTy; }
262 Type *Type::getMetadataTy(LLVMContext &C) { return &C.pImpl->MetadataTy; }
263 Type *Type::getX86_FP80Ty(LLVMContext &C) { return &C.pImpl->X86_FP80Ty; }
264 Type *Type::getFP128Ty(LLVMContext &C) { return &C.pImpl->FP128Ty; }
265 Type *Type::getPPC_FP128Ty(LLVMContext &C) { return &C.pImpl->PPC_FP128Ty; }
266 Type *Type::getX86_MMXTy(LLVMContext &C) { return &C.pImpl->X86_MMXTy; }
268 IntegerType *Type::getInt1Ty(LLVMContext &C) { return &C.pImpl->Int1Ty; }
269 IntegerType *Type::getInt8Ty(LLVMContext &C) { return &C.pImpl->Int8Ty; }
270 IntegerType *Type::getInt16Ty(LLVMContext &C) { return &C.pImpl->Int16Ty; }
271 IntegerType *Type::getInt32Ty(LLVMContext &C) { return &C.pImpl->Int32Ty; }
272 IntegerType *Type::getInt64Ty(LLVMContext &C) { return &C.pImpl->Int64Ty; }
274 IntegerType *Type::getIntNTy(LLVMContext &C, unsigned N) {
275 return IntegerType::get(C, N);
278 PointerType *Type::getHalfPtrTy(LLVMContext &C, unsigned AS) {
279 return getHalfTy(C)->getPointerTo(AS);
282 PointerType *Type::getFloatPtrTy(LLVMContext &C, unsigned AS) {
283 return getFloatTy(C)->getPointerTo(AS);
286 PointerType *Type::getDoublePtrTy(LLVMContext &C, unsigned AS) {
287 return getDoubleTy(C)->getPointerTo(AS);
290 PointerType *Type::getX86_FP80PtrTy(LLVMContext &C, unsigned AS) {
291 return getX86_FP80Ty(C)->getPointerTo(AS);
294 PointerType *Type::getFP128PtrTy(LLVMContext &C, unsigned AS) {
295 return getFP128Ty(C)->getPointerTo(AS);
298 PointerType *Type::getPPC_FP128PtrTy(LLVMContext &C, unsigned AS) {
299 return getPPC_FP128Ty(C)->getPointerTo(AS);
302 PointerType *Type::getX86_MMXPtrTy(LLVMContext &C, unsigned AS) {
303 return getX86_MMXTy(C)->getPointerTo(AS);
306 PointerType *Type::getIntNPtrTy(LLVMContext &C, unsigned N, unsigned AS) {
307 return getIntNTy(C, N)->getPointerTo(AS);
310 PointerType *Type::getInt1PtrTy(LLVMContext &C, unsigned AS) {
311 return getInt1Ty(C)->getPointerTo(AS);
314 PointerType *Type::getInt8PtrTy(LLVMContext &C, unsigned AS) {
315 return getInt8Ty(C)->getPointerTo(AS);
318 PointerType *Type::getInt16PtrTy(LLVMContext &C, unsigned AS) {
319 return getInt16Ty(C)->getPointerTo(AS);
322 PointerType *Type::getInt32PtrTy(LLVMContext &C, unsigned AS) {
323 return getInt32Ty(C)->getPointerTo(AS);
326 PointerType *Type::getInt64PtrTy(LLVMContext &C, unsigned AS) {
327 return getInt64Ty(C)->getPointerTo(AS);
331 //===----------------------------------------------------------------------===//
332 // IntegerType Implementation
333 //===----------------------------------------------------------------------===//
335 IntegerType *IntegerType::get(LLVMContext &C, unsigned NumBits) {
336 assert(NumBits >= MIN_INT_BITS && "bitwidth too small");
337 assert(NumBits <= MAX_INT_BITS && "bitwidth too large");
339 // Check for the built-in integer types
341 case 1: return cast<IntegerType>(Type::getInt1Ty(C));
342 case 8: return cast<IntegerType>(Type::getInt8Ty(C));
343 case 16: return cast<IntegerType>(Type::getInt16Ty(C));
344 case 32: return cast<IntegerType>(Type::getInt32Ty(C));
345 case 64: return cast<IntegerType>(Type::getInt64Ty(C));
350 IntegerType *&Entry = C.pImpl->IntegerTypes[NumBits];
353 Entry = new (C.pImpl->TypeAllocator) IntegerType(C, NumBits);
358 bool IntegerType::isPowerOf2ByteWidth() const {
359 unsigned BitWidth = getBitWidth();
360 return (BitWidth > 7) && isPowerOf2_32(BitWidth);
363 APInt IntegerType::getMask() const {
364 return APInt::getAllOnesValue(getBitWidth());
367 //===----------------------------------------------------------------------===//
368 // FunctionType Implementation
369 //===----------------------------------------------------------------------===//
371 FunctionType::FunctionType(Type *Result, ArrayRef<Type*> Params,
373 : Type(Result->getContext(), FunctionTyID) {
374 Type **SubTys = reinterpret_cast<Type**>(this+1);
375 assert(isValidReturnType(Result) && "invalid return type for function");
376 setSubclassData(IsVarArgs);
378 SubTys[0] = const_cast<Type*>(Result);
380 for (unsigned i = 0, e = Params.size(); i != e; ++i) {
381 assert(isValidArgumentType(Params[i]) &&
382 "Not a valid type for function argument!");
383 SubTys[i+1] = Params[i];
386 ContainedTys = SubTys;
387 NumContainedTys = Params.size() + 1; // + 1 for result type
390 // FunctionType::get - The factory function for the FunctionType class.
391 FunctionType *FunctionType::get(Type *ReturnType,
392 ArrayRef<Type*> Params, bool isVarArg) {
393 LLVMContextImpl *pImpl = ReturnType->getContext().pImpl;
394 FunctionTypeKeyInfo::KeyTy Key(ReturnType, Params, isVarArg);
395 LLVMContextImpl::FunctionTypeMap::iterator I =
396 pImpl->FunctionTypes.find_as(Key);
399 if (I == pImpl->FunctionTypes.end()) {
400 FT = (FunctionType*) pImpl->TypeAllocator.
401 Allocate(sizeof(FunctionType) + sizeof(Type*) * (Params.size() + 1),
402 AlignOf<FunctionType>::Alignment);
403 new (FT) FunctionType(ReturnType, Params, isVarArg);
404 pImpl->FunctionTypes[FT] = true;
413 FunctionType *FunctionType::get(Type *Result, bool isVarArg) {
414 return get(Result, ArrayRef<Type *>(), isVarArg);
418 /// isValidReturnType - Return true if the specified type is valid as a return
420 bool FunctionType::isValidReturnType(Type *RetTy) {
421 return !RetTy->isFunctionTy() && !RetTy->isLabelTy() &&
422 !RetTy->isMetadataTy();
425 /// isValidArgumentType - Return true if the specified type is valid as an
427 bool FunctionType::isValidArgumentType(Type *ArgTy) {
428 return ArgTy->isFirstClassType();
431 //===----------------------------------------------------------------------===//
432 // StructType Implementation
433 //===----------------------------------------------------------------------===//
435 // Primitive Constructors.
437 StructType *StructType::get(LLVMContext &Context, ArrayRef<Type*> ETypes,
439 LLVMContextImpl *pImpl = Context.pImpl;
440 AnonStructTypeKeyInfo::KeyTy Key(ETypes, isPacked);
441 LLVMContextImpl::StructTypeMap::iterator I =
442 pImpl->AnonStructTypes.find_as(Key);
445 if (I == pImpl->AnonStructTypes.end()) {
446 // Value not found. Create a new type!
447 ST = new (Context.pImpl->TypeAllocator) StructType(Context);
448 ST->setSubclassData(SCDB_IsLiteral); // Literal struct.
449 ST->setBody(ETypes, isPacked);
450 Context.pImpl->AnonStructTypes[ST] = true;
458 void StructType::setBody(ArrayRef<Type*> Elements, bool isPacked) {
459 assert(isOpaque() && "Struct body already set!");
461 setSubclassData(getSubclassData() | SCDB_HasBody);
463 setSubclassData(getSubclassData() | SCDB_Packed);
465 unsigned NumElements = Elements.size();
466 Type **Elts = getContext().pImpl->TypeAllocator.Allocate<Type*>(NumElements);
467 memcpy(Elts, Elements.data(), sizeof(Elements[0]) * NumElements);
470 NumContainedTys = NumElements;
473 void StructType::setName(StringRef Name) {
474 if (Name == getName()) return;
476 // If this struct already had a name, remove its symbol table entry.
477 if (SymbolTableEntry) {
478 getContext().pImpl->NamedStructTypes.erase(getName());
479 SymbolTableEntry = 0;
482 // If this is just removing the name, we're done.
486 // Look up the entry for the name.
487 StringMapEntry<StructType*> *Entry =
488 &getContext().pImpl->NamedStructTypes.GetOrCreateValue(Name);
490 // While we have a name collision, try a random rename.
491 if (Entry->getValue()) {
492 SmallString<64> TempStr(Name);
493 TempStr.push_back('.');
494 raw_svector_ostream TmpStream(TempStr);
495 unsigned NameSize = Name.size();
498 TempStr.resize(NameSize + 1);
500 TmpStream << getContext().pImpl->NamedStructTypesUniqueID++;
502 Entry = &getContext().pImpl->
503 NamedStructTypes.GetOrCreateValue(TmpStream.str());
504 } while (Entry->getValue());
507 // Okay, we found an entry that isn't used. It's us!
508 Entry->setValue(this);
510 SymbolTableEntry = Entry;
513 //===----------------------------------------------------------------------===//
514 // StructType Helper functions.
516 StructType *StructType::create(LLVMContext &Context, StringRef Name) {
517 StructType *ST = new (Context.pImpl->TypeAllocator) StructType(Context);
523 StructType *StructType::get(LLVMContext &Context, bool isPacked) {
524 return get(Context, llvm::ArrayRef<Type*>(), isPacked);
527 StructType *StructType::get(Type *type, ...) {
528 assert(type != 0 && "Cannot create a struct type with no elements with this");
529 LLVMContext &Ctx = type->getContext();
531 SmallVector<llvm::Type*, 8> StructFields;
534 StructFields.push_back(type);
535 type = va_arg(ap, llvm::Type*);
537 return llvm::StructType::get(Ctx, StructFields);
540 StructType *StructType::create(LLVMContext &Context, ArrayRef<Type*> Elements,
541 StringRef Name, bool isPacked) {
542 StructType *ST = create(Context, Name);
543 ST->setBody(Elements, isPacked);
547 StructType *StructType::create(LLVMContext &Context, ArrayRef<Type*> Elements) {
548 return create(Context, Elements, StringRef());
551 StructType *StructType::create(LLVMContext &Context) {
552 return create(Context, StringRef());
556 StructType *StructType::create(ArrayRef<Type*> Elements, StringRef Name,
558 assert(!Elements.empty() &&
559 "This method may not be invoked with an empty list");
560 return create(Elements[0]->getContext(), Elements, Name, isPacked);
563 StructType *StructType::create(ArrayRef<Type*> Elements) {
564 assert(!Elements.empty() &&
565 "This method may not be invoked with an empty list");
566 return create(Elements[0]->getContext(), Elements, StringRef());
569 StructType *StructType::create(StringRef Name, Type *type, ...) {
570 assert(type != 0 && "Cannot create a struct type with no elements with this");
571 LLVMContext &Ctx = type->getContext();
573 SmallVector<llvm::Type*, 8> StructFields;
576 StructFields.push_back(type);
577 type = va_arg(ap, llvm::Type*);
579 return llvm::StructType::create(Ctx, StructFields, Name);
583 StringRef StructType::getName() const {
584 assert(!isLiteral() && "Literal structs never have names");
585 if (SymbolTableEntry == 0) return StringRef();
587 return ((StringMapEntry<StructType*> *)SymbolTableEntry)->getKey();
590 void StructType::setBody(Type *type, ...) {
591 assert(type != 0 && "Cannot create a struct type with no elements with this");
593 SmallVector<llvm::Type*, 8> StructFields;
596 StructFields.push_back(type);
597 type = va_arg(ap, llvm::Type*);
599 setBody(StructFields);
602 bool StructType::isValidElementType(Type *ElemTy) {
603 return !ElemTy->isVoidTy() && !ElemTy->isLabelTy() &&
604 !ElemTy->isMetadataTy() && !ElemTy->isFunctionTy();
607 /// isLayoutIdentical - Return true if this is layout identical to the
608 /// specified struct.
609 bool StructType::isLayoutIdentical(StructType *Other) const {
610 if (this == Other) return true;
612 if (isPacked() != Other->isPacked() ||
613 getNumElements() != Other->getNumElements())
616 return std::equal(element_begin(), element_end(), Other->element_begin());
620 /// getTypeByName - Return the type with the specified name, or null if there
621 /// is none by that name.
622 StructType *Module::getTypeByName(StringRef Name) const {
623 StringMap<StructType*>::iterator I =
624 getContext().pImpl->NamedStructTypes.find(Name);
625 if (I != getContext().pImpl->NamedStructTypes.end())
631 //===----------------------------------------------------------------------===//
632 // CompositeType Implementation
633 //===----------------------------------------------------------------------===//
635 Type *CompositeType::getTypeAtIndex(const Value *V) {
636 if (StructType *STy = dyn_cast<StructType>(this)) {
637 unsigned Idx = (unsigned)cast<ConstantInt>(V)->getZExtValue();
638 assert(indexValid(Idx) && "Invalid structure index!");
639 return STy->getElementType(Idx);
642 return cast<SequentialType>(this)->getElementType();
644 Type *CompositeType::getTypeAtIndex(unsigned Idx) {
645 if (StructType *STy = dyn_cast<StructType>(this)) {
646 assert(indexValid(Idx) && "Invalid structure index!");
647 return STy->getElementType(Idx);
650 return cast<SequentialType>(this)->getElementType();
652 bool CompositeType::indexValid(const Value *V) const {
653 if (const StructType *STy = dyn_cast<StructType>(this)) {
654 // Structure indexes require 32-bit integer constants.
655 if (V->getType()->isIntegerTy(32))
656 if (const ConstantInt *CU = dyn_cast<ConstantInt>(V))
657 return CU->getZExtValue() < STy->getNumElements();
661 // Sequential types can be indexed by any integer.
662 return V->getType()->isIntegerTy();
665 bool CompositeType::indexValid(unsigned Idx) const {
666 if (const StructType *STy = dyn_cast<StructType>(this))
667 return Idx < STy->getNumElements();
668 // Sequential types can be indexed by any integer.
673 //===----------------------------------------------------------------------===//
674 // ArrayType Implementation
675 //===----------------------------------------------------------------------===//
677 ArrayType::ArrayType(Type *ElType, uint64_t NumEl)
678 : SequentialType(ArrayTyID, ElType) {
683 ArrayType *ArrayType::get(Type *elementType, uint64_t NumElements) {
684 Type *ElementType = const_cast<Type*>(elementType);
685 assert(isValidElementType(ElementType) && "Invalid type for array element!");
687 LLVMContextImpl *pImpl = ElementType->getContext().pImpl;
689 pImpl->ArrayTypes[std::make_pair(ElementType, NumElements)];
692 Entry = new (pImpl->TypeAllocator) ArrayType(ElementType, NumElements);
696 bool ArrayType::isValidElementType(Type *ElemTy) {
697 return !ElemTy->isVoidTy() && !ElemTy->isLabelTy() &&
698 !ElemTy->isMetadataTy() && !ElemTy->isFunctionTy();
701 //===----------------------------------------------------------------------===//
702 // VectorType Implementation
703 //===----------------------------------------------------------------------===//
705 VectorType::VectorType(Type *ElType, unsigned NumEl)
706 : SequentialType(VectorTyID, ElType) {
710 VectorType *VectorType::get(Type *elementType, unsigned NumElements) {
711 Type *ElementType = const_cast<Type*>(elementType);
712 assert(NumElements > 0 && "#Elements of a VectorType must be greater than 0");
713 assert(isValidElementType(ElementType) &&
714 "Elements of a VectorType must be a primitive type");
716 LLVMContextImpl *pImpl = ElementType->getContext().pImpl;
717 VectorType *&Entry = ElementType->getContext().pImpl
718 ->VectorTypes[std::make_pair(ElementType, NumElements)];
721 Entry = new (pImpl->TypeAllocator) VectorType(ElementType, NumElements);
725 bool VectorType::isValidElementType(Type *ElemTy) {
726 if (PointerType *PTy = dyn_cast<PointerType>(ElemTy))
727 ElemTy = PTy->getElementType();
728 return ElemTy->isIntegerTy() || ElemTy->isFloatingPointTy();
731 //===----------------------------------------------------------------------===//
732 // PointerType Implementation
733 //===----------------------------------------------------------------------===//
735 PointerType *PointerType::get(Type *EltTy, unsigned AddressSpace) {
736 assert(EltTy && "Can't get a pointer to <null> type!");
737 assert(isValidElementType(EltTy) && "Invalid type for pointer element!");
739 LLVMContextImpl *CImpl = EltTy->getContext().pImpl;
741 // Since AddressSpace #0 is the common case, we special case it.
742 PointerType *&Entry = AddressSpace == 0 ? CImpl->PointerTypes[EltTy]
743 : CImpl->ASPointerTypes[std::make_pair(EltTy, AddressSpace)];
746 Entry = new (CImpl->TypeAllocator) PointerType(EltTy, AddressSpace);
751 PointerType::PointerType(Type *E, unsigned AddrSpace)
752 : SequentialType(PointerTyID, E) {
754 const unsigned oldNCT = NumContainedTys;
756 setSubclassData(AddrSpace);
757 // Check for miscompile. PR11652.
758 assert(oldNCT == NumContainedTys && "bitfield written out of bounds?");
761 PointerType *Type::getPointerTo(unsigned addrs) {
762 return PointerType::get(this, addrs);
765 bool PointerType::isValidElementType(Type *ElemTy) {
766 return !ElemTy->isVoidTy() && !ElemTy->isLabelTy() &&
767 !ElemTy->isMetadataTy();