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() {
46 if (VectorType *VTy = dyn_cast<VectorType>(this))
47 return VTy->getElementType();
51 const Type *Type::getScalarType() const {
52 if (const VectorType *VTy = dyn_cast<VectorType>(this))
53 return VTy->getElementType();
57 /// isIntegerTy - Return true if this is an IntegerType of the specified width.
58 bool Type::isIntegerTy(unsigned Bitwidth) const {
59 return isIntegerTy() && cast<IntegerType>(this)->getBitWidth() == Bitwidth;
62 // canLosslesslyBitCastTo - Return true if this type can be converted to
63 // 'Ty' without any reinterpretation of bits. For example, i8* to i32*.
65 bool Type::canLosslesslyBitCastTo(Type *Ty) const {
66 // Identity cast means no change so return true
70 // They are not convertible unless they are at least first class types
71 if (!this->isFirstClassType() || !Ty->isFirstClassType())
74 // Vector -> Vector conversions are always lossless if the two vector types
75 // have the same size, otherwise not. Also, 64-bit vector types can be
76 // converted to x86mmx.
77 if (const VectorType *thisPTy = dyn_cast<VectorType>(this)) {
78 if (const VectorType *thatPTy = dyn_cast<VectorType>(Ty))
79 return thisPTy->getBitWidth() == thatPTy->getBitWidth();
80 if (Ty->getTypeID() == Type::X86_MMXTyID &&
81 thisPTy->getBitWidth() == 64)
85 if (this->getTypeID() == Type::X86_MMXTyID)
86 if (const VectorType *thatPTy = dyn_cast<VectorType>(Ty))
87 if (thatPTy->getBitWidth() == 64)
90 // At this point we have only various mismatches of the first class types
91 // remaining and ptr->ptr. Just select the lossless conversions. Everything
92 // else is not lossless.
93 if (this->isPointerTy())
94 return Ty->isPointerTy();
95 return false; // Other types have no identity values
98 bool Type::isEmptyTy() const {
99 const ArrayType *ATy = dyn_cast<ArrayType>(this);
101 unsigned NumElements = ATy->getNumElements();
102 return NumElements == 0 || ATy->getElementType()->isEmptyTy();
105 const StructType *STy = dyn_cast<StructType>(this);
107 unsigned NumElements = STy->getNumElements();
108 for (unsigned i = 0; i < NumElements; ++i)
109 if (!STy->getElementType(i)->isEmptyTy())
117 unsigned Type::getPrimitiveSizeInBits() const {
118 switch (getTypeID()) {
119 case Type::HalfTyID: return 16;
120 case Type::FloatTyID: return 32;
121 case Type::DoubleTyID: return 64;
122 case Type::X86_FP80TyID: return 80;
123 case Type::FP128TyID: return 128;
124 case Type::PPC_FP128TyID: return 128;
125 case Type::X86_MMXTyID: return 64;
126 case Type::IntegerTyID: return cast<IntegerType>(this)->getBitWidth();
127 case Type::VectorTyID: return cast<VectorType>(this)->getBitWidth();
132 /// getScalarSizeInBits - If this is a vector type, return the
133 /// getPrimitiveSizeInBits value for the element type. Otherwise return the
134 /// getPrimitiveSizeInBits value for this type.
135 unsigned Type::getScalarSizeInBits() {
136 return getScalarType()->getPrimitiveSizeInBits();
139 /// getFPMantissaWidth - Return the width of the mantissa of this type. This
140 /// is only valid on floating point types. If the FP type does not
141 /// have a stable mantissa (e.g. ppc long double), this method returns -1.
142 int Type::getFPMantissaWidth() const {
143 if (const VectorType *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() == FloatTyID) return 24;
148 if (getTypeID() == DoubleTyID) return 53;
149 if (getTypeID() == X86_FP80TyID) return 64;
150 if (getTypeID() == FP128TyID) return 113;
151 assert(getTypeID() == PPC_FP128TyID && "unknown fp type");
155 /// isSizedDerivedType - Derived types like structures and arrays are sized
156 /// iff all of the members of the type are sized as well. Since asking for
157 /// their size is relatively uncommon, move this operation out of line.
158 bool Type::isSizedDerivedType(SmallPtrSet<const Type*, 4> *Visited) const {
159 if (const ArrayType *ATy = dyn_cast<ArrayType>(this))
160 return ATy->getElementType()->isSized(Visited);
162 if (const VectorType *VTy = dyn_cast<VectorType>(this))
163 return VTy->getElementType()->isSized(Visited);
165 return cast<StructType>(this)->isSized(Visited);
168 //===----------------------------------------------------------------------===//
169 // Subclass Helper Methods
170 //===----------------------------------------------------------------------===//
172 unsigned Type::getIntegerBitWidth() const {
173 return cast<IntegerType>(this)->getBitWidth();
176 bool Type::isFunctionVarArg() const {
177 return cast<FunctionType>(this)->isVarArg();
180 Type *Type::getFunctionParamType(unsigned i) const {
181 return cast<FunctionType>(this)->getParamType(i);
184 unsigned Type::getFunctionNumParams() const {
185 return cast<FunctionType>(this)->getNumParams();
188 StringRef Type::getStructName() const {
189 return cast<StructType>(this)->getName();
192 unsigned Type::getStructNumElements() const {
193 return cast<StructType>(this)->getNumElements();
196 Type *Type::getStructElementType(unsigned N) const {
197 return cast<StructType>(this)->getElementType(N);
200 Type *Type::getSequentialElementType() const {
201 return cast<SequentialType>(this)->getElementType();
204 uint64_t Type::getArrayNumElements() const {
205 return cast<ArrayType>(this)->getNumElements();
208 unsigned Type::getVectorNumElements() const {
209 return cast<VectorType>(this)->getNumElements();
212 unsigned Type::getPointerAddressSpace() const {
213 return cast<PointerType>(getScalarType())->getAddressSpace();
217 //===----------------------------------------------------------------------===//
218 // Primitive 'Type' data
219 //===----------------------------------------------------------------------===//
221 Type *Type::getVoidTy(LLVMContext &C) { return &C.pImpl->VoidTy; }
222 Type *Type::getLabelTy(LLVMContext &C) { return &C.pImpl->LabelTy; }
223 Type *Type::getHalfTy(LLVMContext &C) { return &C.pImpl->HalfTy; }
224 Type *Type::getFloatTy(LLVMContext &C) { return &C.pImpl->FloatTy; }
225 Type *Type::getDoubleTy(LLVMContext &C) { return &C.pImpl->DoubleTy; }
226 Type *Type::getMetadataTy(LLVMContext &C) { return &C.pImpl->MetadataTy; }
227 Type *Type::getX86_FP80Ty(LLVMContext &C) { return &C.pImpl->X86_FP80Ty; }
228 Type *Type::getFP128Ty(LLVMContext &C) { return &C.pImpl->FP128Ty; }
229 Type *Type::getPPC_FP128Ty(LLVMContext &C) { return &C.pImpl->PPC_FP128Ty; }
230 Type *Type::getX86_MMXTy(LLVMContext &C) { return &C.pImpl->X86_MMXTy; }
232 IntegerType *Type::getInt1Ty(LLVMContext &C) { return &C.pImpl->Int1Ty; }
233 IntegerType *Type::getInt8Ty(LLVMContext &C) { return &C.pImpl->Int8Ty; }
234 IntegerType *Type::getInt16Ty(LLVMContext &C) { return &C.pImpl->Int16Ty; }
235 IntegerType *Type::getInt32Ty(LLVMContext &C) { return &C.pImpl->Int32Ty; }
236 IntegerType *Type::getInt64Ty(LLVMContext &C) { return &C.pImpl->Int64Ty; }
238 IntegerType *Type::getIntNTy(LLVMContext &C, unsigned N) {
239 return IntegerType::get(C, N);
242 PointerType *Type::getHalfPtrTy(LLVMContext &C, unsigned AS) {
243 return getHalfTy(C)->getPointerTo(AS);
246 PointerType *Type::getFloatPtrTy(LLVMContext &C, unsigned AS) {
247 return getFloatTy(C)->getPointerTo(AS);
250 PointerType *Type::getDoublePtrTy(LLVMContext &C, unsigned AS) {
251 return getDoubleTy(C)->getPointerTo(AS);
254 PointerType *Type::getX86_FP80PtrTy(LLVMContext &C, unsigned AS) {
255 return getX86_FP80Ty(C)->getPointerTo(AS);
258 PointerType *Type::getFP128PtrTy(LLVMContext &C, unsigned AS) {
259 return getFP128Ty(C)->getPointerTo(AS);
262 PointerType *Type::getPPC_FP128PtrTy(LLVMContext &C, unsigned AS) {
263 return getPPC_FP128Ty(C)->getPointerTo(AS);
266 PointerType *Type::getX86_MMXPtrTy(LLVMContext &C, unsigned AS) {
267 return getX86_MMXTy(C)->getPointerTo(AS);
270 PointerType *Type::getIntNPtrTy(LLVMContext &C, unsigned N, unsigned AS) {
271 return getIntNTy(C, N)->getPointerTo(AS);
274 PointerType *Type::getInt1PtrTy(LLVMContext &C, unsigned AS) {
275 return getInt1Ty(C)->getPointerTo(AS);
278 PointerType *Type::getInt8PtrTy(LLVMContext &C, unsigned AS) {
279 return getInt8Ty(C)->getPointerTo(AS);
282 PointerType *Type::getInt16PtrTy(LLVMContext &C, unsigned AS) {
283 return getInt16Ty(C)->getPointerTo(AS);
286 PointerType *Type::getInt32PtrTy(LLVMContext &C, unsigned AS) {
287 return getInt32Ty(C)->getPointerTo(AS);
290 PointerType *Type::getInt64PtrTy(LLVMContext &C, unsigned AS) {
291 return getInt64Ty(C)->getPointerTo(AS);
295 //===----------------------------------------------------------------------===//
296 // IntegerType Implementation
297 //===----------------------------------------------------------------------===//
299 IntegerType *IntegerType::get(LLVMContext &C, unsigned NumBits) {
300 assert(NumBits >= MIN_INT_BITS && "bitwidth too small");
301 assert(NumBits <= MAX_INT_BITS && "bitwidth too large");
303 // Check for the built-in integer types
305 case 1: return cast<IntegerType>(Type::getInt1Ty(C));
306 case 8: return cast<IntegerType>(Type::getInt8Ty(C));
307 case 16: return cast<IntegerType>(Type::getInt16Ty(C));
308 case 32: return cast<IntegerType>(Type::getInt32Ty(C));
309 case 64: return cast<IntegerType>(Type::getInt64Ty(C));
314 IntegerType *&Entry = C.pImpl->IntegerTypes[NumBits];
317 Entry = new (C.pImpl->TypeAllocator) IntegerType(C, NumBits);
322 bool IntegerType::isPowerOf2ByteWidth() const {
323 unsigned BitWidth = getBitWidth();
324 return (BitWidth > 7) && isPowerOf2_32(BitWidth);
327 APInt IntegerType::getMask() const {
328 return APInt::getAllOnesValue(getBitWidth());
331 //===----------------------------------------------------------------------===//
332 // FunctionType Implementation
333 //===----------------------------------------------------------------------===//
335 FunctionType::FunctionType(Type *Result, ArrayRef<Type*> Params,
337 : Type(Result->getContext(), FunctionTyID) {
338 Type **SubTys = reinterpret_cast<Type**>(this+1);
339 assert(isValidReturnType(Result) && "invalid return type for function");
340 setSubclassData(IsVarArgs);
342 SubTys[0] = const_cast<Type*>(Result);
344 for (unsigned i = 0, e = Params.size(); i != e; ++i) {
345 assert(isValidArgumentType(Params[i]) &&
346 "Not a valid type for function argument!");
347 SubTys[i+1] = Params[i];
350 ContainedTys = SubTys;
351 NumContainedTys = Params.size() + 1; // + 1 for result type
354 // FunctionType::get - The factory function for the FunctionType class.
355 FunctionType *FunctionType::get(Type *ReturnType,
356 ArrayRef<Type*> Params, bool isVarArg) {
357 LLVMContextImpl *pImpl = ReturnType->getContext().pImpl;
358 FunctionTypeKeyInfo::KeyTy Key(ReturnType, Params, isVarArg);
359 LLVMContextImpl::FunctionTypeMap::iterator I =
360 pImpl->FunctionTypes.find_as(Key);
363 if (I == pImpl->FunctionTypes.end()) {
364 FT = (FunctionType*) pImpl->TypeAllocator.
365 Allocate(sizeof(FunctionType) + sizeof(Type*) * (Params.size() + 1),
366 AlignOf<FunctionType>::Alignment);
367 new (FT) FunctionType(ReturnType, Params, isVarArg);
368 pImpl->FunctionTypes[FT] = true;
376 FunctionType *FunctionType::get(Type *Result, bool isVarArg) {
377 return get(Result, None, isVarArg);
380 /// isValidReturnType - Return true if the specified type is valid as a return
382 bool FunctionType::isValidReturnType(Type *RetTy) {
383 return !RetTy->isFunctionTy() && !RetTy->isLabelTy() &&
384 !RetTy->isMetadataTy();
387 /// isValidArgumentType - Return true if the specified type is valid as an
389 bool FunctionType::isValidArgumentType(Type *ArgTy) {
390 return ArgTy->isFirstClassType();
393 //===----------------------------------------------------------------------===//
394 // StructType Implementation
395 //===----------------------------------------------------------------------===//
397 // Primitive Constructors.
399 StructType *StructType::get(LLVMContext &Context, ArrayRef<Type*> ETypes,
401 LLVMContextImpl *pImpl = Context.pImpl;
402 AnonStructTypeKeyInfo::KeyTy Key(ETypes, isPacked);
403 LLVMContextImpl::StructTypeMap::iterator I =
404 pImpl->AnonStructTypes.find_as(Key);
407 if (I == pImpl->AnonStructTypes.end()) {
408 // Value not found. Create a new type!
409 ST = new (Context.pImpl->TypeAllocator) StructType(Context);
410 ST->setSubclassData(SCDB_IsLiteral); // Literal struct.
411 ST->setBody(ETypes, isPacked);
412 Context.pImpl->AnonStructTypes[ST] = true;
420 void StructType::setBody(ArrayRef<Type*> Elements, bool isPacked) {
421 assert(isOpaque() && "Struct body already set!");
423 setSubclassData(getSubclassData() | SCDB_HasBody);
425 setSubclassData(getSubclassData() | SCDB_Packed);
427 unsigned NumElements = Elements.size();
428 Type **Elts = getContext().pImpl->TypeAllocator.Allocate<Type*>(NumElements);
429 memcpy(Elts, Elements.data(), sizeof(Elements[0]) * NumElements);
432 NumContainedTys = NumElements;
435 void StructType::setName(StringRef Name) {
436 if (Name == getName()) return;
438 StringMap<StructType *> &SymbolTable = getContext().pImpl->NamedStructTypes;
439 typedef StringMap<StructType *>::MapEntryTy EntryTy;
441 // If this struct already had a name, remove its symbol table entry. Don't
442 // delete the data yet because it may be part of the new name.
443 if (SymbolTableEntry)
444 SymbolTable.remove((EntryTy *)SymbolTableEntry);
446 // If this is just removing the name, we're done.
448 if (SymbolTableEntry) {
449 // Delete the old string data.
450 ((EntryTy *)SymbolTableEntry)->Destroy(SymbolTable.getAllocator());
451 SymbolTableEntry = 0;
456 // Look up the entry for the name.
457 EntryTy *Entry = &getContext().pImpl->NamedStructTypes.GetOrCreateValue(Name);
459 // While we have a name collision, try a random rename.
460 if (Entry->getValue()) {
461 SmallString<64> TempStr(Name);
462 TempStr.push_back('.');
463 raw_svector_ostream TmpStream(TempStr);
464 unsigned NameSize = Name.size();
467 TempStr.resize(NameSize + 1);
469 TmpStream << getContext().pImpl->NamedStructTypesUniqueID++;
471 Entry = &getContext().pImpl->
472 NamedStructTypes.GetOrCreateValue(TmpStream.str());
473 } while (Entry->getValue());
476 // Okay, we found an entry that isn't used. It's us!
477 Entry->setValue(this);
479 // Delete the old string data.
480 if (SymbolTableEntry)
481 ((EntryTy *)SymbolTableEntry)->Destroy(SymbolTable.getAllocator());
482 SymbolTableEntry = Entry;
485 //===----------------------------------------------------------------------===//
486 // StructType Helper functions.
488 StructType *StructType::create(LLVMContext &Context, StringRef Name) {
489 StructType *ST = new (Context.pImpl->TypeAllocator) StructType(Context);
495 StructType *StructType::get(LLVMContext &Context, bool isPacked) {
496 return get(Context, None, isPacked);
499 StructType *StructType::get(Type *type, ...) {
500 assert(type != 0 && "Cannot create a struct type with no elements with this");
501 LLVMContext &Ctx = type->getContext();
503 SmallVector<llvm::Type*, 8> StructFields;
506 StructFields.push_back(type);
507 type = va_arg(ap, llvm::Type*);
509 return llvm::StructType::get(Ctx, StructFields);
512 StructType *StructType::create(LLVMContext &Context, ArrayRef<Type*> Elements,
513 StringRef Name, bool isPacked) {
514 StructType *ST = create(Context, Name);
515 ST->setBody(Elements, isPacked);
519 StructType *StructType::create(LLVMContext &Context, ArrayRef<Type*> Elements) {
520 return create(Context, Elements, StringRef());
523 StructType *StructType::create(LLVMContext &Context) {
524 return create(Context, StringRef());
527 StructType *StructType::create(ArrayRef<Type*> Elements, StringRef Name,
529 assert(!Elements.empty() &&
530 "This method may not be invoked with an empty list");
531 return create(Elements[0]->getContext(), Elements, Name, isPacked);
534 StructType *StructType::create(ArrayRef<Type*> Elements) {
535 assert(!Elements.empty() &&
536 "This method may not be invoked with an empty list");
537 return create(Elements[0]->getContext(), Elements, StringRef());
540 StructType *StructType::create(StringRef Name, Type *type, ...) {
541 assert(type != 0 && "Cannot create a struct type with no elements with this");
542 LLVMContext &Ctx = type->getContext();
544 SmallVector<llvm::Type*, 8> StructFields;
547 StructFields.push_back(type);
548 type = va_arg(ap, llvm::Type*);
550 return llvm::StructType::create(Ctx, StructFields, Name);
553 bool StructType::isSized(SmallPtrSet<const Type*, 4> *Visited) const {
554 if ((getSubclassData() & SCDB_IsSized) != 0)
559 if (Visited && !Visited->insert(this))
562 // Okay, our struct is sized if all of the elements are, but if one of the
563 // elements is opaque, the struct isn't sized *yet*, but may become sized in
564 // the future, so just bail out without caching.
565 for (element_iterator I = element_begin(), E = element_end(); I != E; ++I)
566 if (!(*I)->isSized(Visited))
569 // Here we cheat a bit and cast away const-ness. The goal is to memoize when
570 // we find a sized type, as types can only move from opaque to sized, not the
572 const_cast<StructType*>(this)->setSubclassData(
573 getSubclassData() | SCDB_IsSized);
577 StringRef StructType::getName() const {
578 assert(!isLiteral() && "Literal structs never have names");
579 if (SymbolTableEntry == 0) return StringRef();
581 return ((StringMapEntry<StructType*> *)SymbolTableEntry)->getKey();
584 void StructType::setBody(Type *type, ...) {
585 assert(type != 0 && "Cannot create a struct type with no elements with this");
587 SmallVector<llvm::Type*, 8> StructFields;
590 StructFields.push_back(type);
591 type = va_arg(ap, llvm::Type*);
593 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 std::equal(element_begin(), element_end(), Other->element_begin());
613 /// getTypeByName - Return the type with the specified name, or null if there
614 /// is none by that name.
615 StructType *Module::getTypeByName(StringRef Name) const {
616 return getContext().pImpl->NamedStructTypes.lookup(Name);
620 //===----------------------------------------------------------------------===//
621 // CompositeType Implementation
622 //===----------------------------------------------------------------------===//
624 Type *CompositeType::getTypeAtIndex(const Value *V) {
625 if (StructType *STy = dyn_cast<StructType>(this)) {
627 (unsigned)cast<Constant>(V)->getUniqueInteger().getZExtValue();
628 assert(indexValid(Idx) && "Invalid structure index!");
629 return STy->getElementType(Idx);
632 return cast<SequentialType>(this)->getElementType();
634 Type *CompositeType::getTypeAtIndex(unsigned Idx) {
635 if (StructType *STy = dyn_cast<StructType>(this)) {
636 assert(indexValid(Idx) && "Invalid structure index!");
637 return STy->getElementType(Idx);
640 return cast<SequentialType>(this)->getElementType();
642 bool CompositeType::indexValid(const Value *V) const {
643 if (const StructType *STy = dyn_cast<StructType>(this)) {
644 // Structure indexes require (vectors of) 32-bit integer constants. In the
645 // vector case all of the indices must be equal.
646 if (!V->getType()->getScalarType()->isIntegerTy(32))
648 const Constant *C = dyn_cast<Constant>(V);
649 if (C && V->getType()->isVectorTy())
650 C = C->getSplatValue();
651 const ConstantInt *CU = dyn_cast_or_null<ConstantInt>(C);
652 return CU && CU->getZExtValue() < STy->getNumElements();
655 // Sequential types can be indexed by any integer.
656 return V->getType()->isIntOrIntVectorTy();
659 bool CompositeType::indexValid(unsigned Idx) const {
660 if (const StructType *STy = dyn_cast<StructType>(this))
661 return Idx < STy->getNumElements();
662 // Sequential types can be indexed by any integer.
667 //===----------------------------------------------------------------------===//
668 // ArrayType Implementation
669 //===----------------------------------------------------------------------===//
671 ArrayType::ArrayType(Type *ElType, uint64_t NumEl)
672 : SequentialType(ArrayTyID, ElType) {
676 ArrayType *ArrayType::get(Type *elementType, uint64_t NumElements) {
677 Type *ElementType = const_cast<Type*>(elementType);
678 assert(isValidElementType(ElementType) && "Invalid type for array element!");
680 LLVMContextImpl *pImpl = ElementType->getContext().pImpl;
682 pImpl->ArrayTypes[std::make_pair(ElementType, NumElements)];
685 Entry = new (pImpl->TypeAllocator) ArrayType(ElementType, NumElements);
689 bool ArrayType::isValidElementType(Type *ElemTy) {
690 return !ElemTy->isVoidTy() && !ElemTy->isLabelTy() &&
691 !ElemTy->isMetadataTy() && !ElemTy->isFunctionTy();
694 //===----------------------------------------------------------------------===//
695 // VectorType Implementation
696 //===----------------------------------------------------------------------===//
698 VectorType::VectorType(Type *ElType, unsigned NumEl)
699 : SequentialType(VectorTyID, ElType) {
703 VectorType *VectorType::get(Type *elementType, unsigned NumElements) {
704 Type *ElementType = const_cast<Type*>(elementType);
705 assert(NumElements > 0 && "#Elements of a VectorType must be greater than 0");
706 assert(isValidElementType(ElementType) &&
707 "Elements of a VectorType must be a primitive type");
709 LLVMContextImpl *pImpl = ElementType->getContext().pImpl;
710 VectorType *&Entry = ElementType->getContext().pImpl
711 ->VectorTypes[std::make_pair(ElementType, NumElements)];
714 Entry = new (pImpl->TypeAllocator) VectorType(ElementType, NumElements);
718 bool VectorType::isValidElementType(Type *ElemTy) {
719 return ElemTy->isIntegerTy() || ElemTy->isFloatingPointTy() ||
720 ElemTy->isPointerTy();
723 //===----------------------------------------------------------------------===//
724 // PointerType Implementation
725 //===----------------------------------------------------------------------===//
727 PointerType *PointerType::get(Type *EltTy, unsigned AddressSpace) {
728 assert(EltTy && "Can't get a pointer to <null> type!");
729 assert(isValidElementType(EltTy) && "Invalid type for pointer element!");
731 LLVMContextImpl *CImpl = EltTy->getContext().pImpl;
733 // Since AddressSpace #0 is the common case, we special case it.
734 PointerType *&Entry = AddressSpace == 0 ? CImpl->PointerTypes[EltTy]
735 : CImpl->ASPointerTypes[std::make_pair(EltTy, AddressSpace)];
738 Entry = new (CImpl->TypeAllocator) PointerType(EltTy, AddressSpace);
743 PointerType::PointerType(Type *E, unsigned AddrSpace)
744 : SequentialType(PointerTyID, E) {
746 const unsigned oldNCT = NumContainedTys;
748 setSubclassData(AddrSpace);
749 // Check for miscompile. PR11652.
750 assert(oldNCT == NumContainedTys && "bitfield written out of bounds?");
753 PointerType *Type::getPointerTo(unsigned addrs) {
754 return PointerType::get(this, addrs);
757 bool PointerType::isValidElementType(Type *ElemTy) {
758 return !ElemTy->isVoidTy() && !ElemTy->isLabelTy() &&
759 !ElemTy->isMetadataTy();