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() const {
159 if (this->isIntegerTy())
162 if (const ArrayType *ATy = dyn_cast<ArrayType>(this))
163 return ATy->getElementType()->isSized();
165 if (const VectorType *VTy = dyn_cast<VectorType>(this))
166 return VTy->getElementType()->isSized();
168 if (!this->isStructTy())
171 return cast<StructType>(this)->isSized();
174 //===----------------------------------------------------------------------===//
175 // Subclass Helper Methods
176 //===----------------------------------------------------------------------===//
178 unsigned Type::getIntegerBitWidth() const {
179 return cast<IntegerType>(this)->getBitWidth();
182 bool Type::isFunctionVarArg() const {
183 return cast<FunctionType>(this)->isVarArg();
186 Type *Type::getFunctionParamType(unsigned i) const {
187 return cast<FunctionType>(this)->getParamType(i);
190 unsigned Type::getFunctionNumParams() const {
191 return cast<FunctionType>(this)->getNumParams();
194 StringRef Type::getStructName() const {
195 return cast<StructType>(this)->getName();
198 unsigned Type::getStructNumElements() const {
199 return cast<StructType>(this)->getNumElements();
202 Type *Type::getStructElementType(unsigned N) const {
203 return cast<StructType>(this)->getElementType(N);
206 Type *Type::getSequentialElementType() const {
207 return cast<SequentialType>(this)->getElementType();
210 uint64_t Type::getArrayNumElements() const {
211 return cast<ArrayType>(this)->getNumElements();
214 unsigned Type::getVectorNumElements() const {
215 return cast<VectorType>(this)->getNumElements();
218 unsigned Type::getPointerAddressSpace() const {
219 return cast<PointerType>(getScalarType())->getAddressSpace();
223 //===----------------------------------------------------------------------===//
224 // Primitive 'Type' data
225 //===----------------------------------------------------------------------===//
227 Type *Type::getVoidTy(LLVMContext &C) { return &C.pImpl->VoidTy; }
228 Type *Type::getLabelTy(LLVMContext &C) { return &C.pImpl->LabelTy; }
229 Type *Type::getHalfTy(LLVMContext &C) { return &C.pImpl->HalfTy; }
230 Type *Type::getFloatTy(LLVMContext &C) { return &C.pImpl->FloatTy; }
231 Type *Type::getDoubleTy(LLVMContext &C) { return &C.pImpl->DoubleTy; }
232 Type *Type::getMetadataTy(LLVMContext &C) { return &C.pImpl->MetadataTy; }
233 Type *Type::getX86_FP80Ty(LLVMContext &C) { return &C.pImpl->X86_FP80Ty; }
234 Type *Type::getFP128Ty(LLVMContext &C) { return &C.pImpl->FP128Ty; }
235 Type *Type::getPPC_FP128Ty(LLVMContext &C) { return &C.pImpl->PPC_FP128Ty; }
236 Type *Type::getX86_MMXTy(LLVMContext &C) { return &C.pImpl->X86_MMXTy; }
238 IntegerType *Type::getInt1Ty(LLVMContext &C) { return &C.pImpl->Int1Ty; }
239 IntegerType *Type::getInt8Ty(LLVMContext &C) { return &C.pImpl->Int8Ty; }
240 IntegerType *Type::getInt16Ty(LLVMContext &C) { return &C.pImpl->Int16Ty; }
241 IntegerType *Type::getInt32Ty(LLVMContext &C) { return &C.pImpl->Int32Ty; }
242 IntegerType *Type::getInt64Ty(LLVMContext &C) { return &C.pImpl->Int64Ty; }
244 IntegerType *Type::getIntNTy(LLVMContext &C, unsigned N) {
245 return IntegerType::get(C, N);
248 PointerType *Type::getHalfPtrTy(LLVMContext &C, unsigned AS) {
249 return getHalfTy(C)->getPointerTo(AS);
252 PointerType *Type::getFloatPtrTy(LLVMContext &C, unsigned AS) {
253 return getFloatTy(C)->getPointerTo(AS);
256 PointerType *Type::getDoublePtrTy(LLVMContext &C, unsigned AS) {
257 return getDoubleTy(C)->getPointerTo(AS);
260 PointerType *Type::getX86_FP80PtrTy(LLVMContext &C, unsigned AS) {
261 return getX86_FP80Ty(C)->getPointerTo(AS);
264 PointerType *Type::getFP128PtrTy(LLVMContext &C, unsigned AS) {
265 return getFP128Ty(C)->getPointerTo(AS);
268 PointerType *Type::getPPC_FP128PtrTy(LLVMContext &C, unsigned AS) {
269 return getPPC_FP128Ty(C)->getPointerTo(AS);
272 PointerType *Type::getX86_MMXPtrTy(LLVMContext &C, unsigned AS) {
273 return getX86_MMXTy(C)->getPointerTo(AS);
276 PointerType *Type::getIntNPtrTy(LLVMContext &C, unsigned N, unsigned AS) {
277 return getIntNTy(C, N)->getPointerTo(AS);
280 PointerType *Type::getInt1PtrTy(LLVMContext &C, unsigned AS) {
281 return getInt1Ty(C)->getPointerTo(AS);
284 PointerType *Type::getInt8PtrTy(LLVMContext &C, unsigned AS) {
285 return getInt8Ty(C)->getPointerTo(AS);
288 PointerType *Type::getInt16PtrTy(LLVMContext &C, unsigned AS) {
289 return getInt16Ty(C)->getPointerTo(AS);
292 PointerType *Type::getInt32PtrTy(LLVMContext &C, unsigned AS) {
293 return getInt32Ty(C)->getPointerTo(AS);
296 PointerType *Type::getInt64PtrTy(LLVMContext &C, unsigned AS) {
297 return getInt64Ty(C)->getPointerTo(AS);
301 //===----------------------------------------------------------------------===//
302 // IntegerType Implementation
303 //===----------------------------------------------------------------------===//
305 IntegerType *IntegerType::get(LLVMContext &C, unsigned NumBits) {
306 assert(NumBits >= MIN_INT_BITS && "bitwidth too small");
307 assert(NumBits <= MAX_INT_BITS && "bitwidth too large");
309 // Check for the built-in integer types
311 case 1: return cast<IntegerType>(Type::getInt1Ty(C));
312 case 8: return cast<IntegerType>(Type::getInt8Ty(C));
313 case 16: return cast<IntegerType>(Type::getInt16Ty(C));
314 case 32: return cast<IntegerType>(Type::getInt32Ty(C));
315 case 64: return cast<IntegerType>(Type::getInt64Ty(C));
320 IntegerType *&Entry = C.pImpl->IntegerTypes[NumBits];
323 Entry = new (C.pImpl->TypeAllocator) IntegerType(C, NumBits);
328 bool IntegerType::isPowerOf2ByteWidth() const {
329 unsigned BitWidth = getBitWidth();
330 return (BitWidth > 7) && isPowerOf2_32(BitWidth);
333 APInt IntegerType::getMask() const {
334 return APInt::getAllOnesValue(getBitWidth());
337 //===----------------------------------------------------------------------===//
338 // FunctionType Implementation
339 //===----------------------------------------------------------------------===//
341 FunctionType::FunctionType(Type *Result, ArrayRef<Type*> Params,
343 : Type(Result->getContext(), FunctionTyID) {
344 Type **SubTys = reinterpret_cast<Type**>(this+1);
345 assert(isValidReturnType(Result) && "invalid return type for function");
346 setSubclassData(IsVarArgs);
348 SubTys[0] = const_cast<Type*>(Result);
350 for (unsigned i = 0, e = Params.size(); i != e; ++i) {
351 assert(isValidArgumentType(Params[i]) &&
352 "Not a valid type for function argument!");
353 SubTys[i+1] = Params[i];
356 ContainedTys = SubTys;
357 NumContainedTys = Params.size() + 1; // + 1 for result type
360 // FunctionType::get - The factory function for the FunctionType class.
361 FunctionType *FunctionType::get(Type *ReturnType,
362 ArrayRef<Type*> Params, bool isVarArg) {
363 LLVMContextImpl *pImpl = ReturnType->getContext().pImpl;
364 FunctionTypeKeyInfo::KeyTy Key(ReturnType, Params, isVarArg);
365 LLVMContextImpl::FunctionTypeMap::iterator I =
366 pImpl->FunctionTypes.find_as(Key);
369 if (I == pImpl->FunctionTypes.end()) {
370 FT = (FunctionType*) pImpl->TypeAllocator.
371 Allocate(sizeof(FunctionType) + sizeof(Type*) * (Params.size() + 1),
372 AlignOf<FunctionType>::Alignment);
373 new (FT) FunctionType(ReturnType, Params, isVarArg);
374 pImpl->FunctionTypes[FT] = true;
382 FunctionType *FunctionType::get(Type *Result, bool isVarArg) {
383 return get(Result, ArrayRef<Type *>(), isVarArg);
386 /// isValidReturnType - Return true if the specified type is valid as a return
388 bool FunctionType::isValidReturnType(Type *RetTy) {
389 return !RetTy->isFunctionTy() && !RetTy->isLabelTy() &&
390 !RetTy->isMetadataTy();
393 /// isValidArgumentType - Return true if the specified type is valid as an
395 bool FunctionType::isValidArgumentType(Type *ArgTy) {
396 return ArgTy->isFirstClassType();
399 //===----------------------------------------------------------------------===//
400 // StructType Implementation
401 //===----------------------------------------------------------------------===//
403 // Primitive Constructors.
405 StructType *StructType::get(LLVMContext &Context, ArrayRef<Type*> ETypes,
407 LLVMContextImpl *pImpl = Context.pImpl;
408 AnonStructTypeKeyInfo::KeyTy Key(ETypes, isPacked);
409 LLVMContextImpl::StructTypeMap::iterator I =
410 pImpl->AnonStructTypes.find_as(Key);
413 if (I == pImpl->AnonStructTypes.end()) {
414 // Value not found. Create a new type!
415 ST = new (Context.pImpl->TypeAllocator) StructType(Context);
416 ST->setSubclassData(SCDB_IsLiteral); // Literal struct.
417 ST->setBody(ETypes, isPacked);
418 Context.pImpl->AnonStructTypes[ST] = true;
426 void StructType::setBody(ArrayRef<Type*> Elements, bool isPacked) {
427 assert(isOpaque() && "Struct body already set!");
429 setSubclassData(getSubclassData() | SCDB_HasBody);
431 setSubclassData(getSubclassData() | SCDB_Packed);
433 unsigned NumElements = Elements.size();
434 Type **Elts = getContext().pImpl->TypeAllocator.Allocate<Type*>(NumElements);
435 memcpy(Elts, Elements.data(), sizeof(Elements[0]) * NumElements);
438 NumContainedTys = NumElements;
441 void StructType::setName(StringRef Name) {
442 if (Name == getName()) return;
444 StringMap<StructType *> &SymbolTable = getContext().pImpl->NamedStructTypes;
445 typedef StringMap<StructType *>::MapEntryTy EntryTy;
447 // If this struct already had a name, remove its symbol table entry. Don't
448 // delete the data yet because it may be part of the new name.
449 if (SymbolTableEntry)
450 SymbolTable.remove((EntryTy *)SymbolTableEntry);
452 // If this is just removing the name, we're done.
454 if (SymbolTableEntry) {
455 // Delete the old string data.
456 ((EntryTy *)SymbolTableEntry)->Destroy(SymbolTable.getAllocator());
457 SymbolTableEntry = 0;
462 // Look up the entry for the name.
463 EntryTy *Entry = &getContext().pImpl->NamedStructTypes.GetOrCreateValue(Name);
465 // While we have a name collision, try a random rename.
466 if (Entry->getValue()) {
467 SmallString<64> TempStr(Name);
468 TempStr.push_back('.');
469 raw_svector_ostream TmpStream(TempStr);
470 unsigned NameSize = Name.size();
473 TempStr.resize(NameSize + 1);
475 TmpStream << getContext().pImpl->NamedStructTypesUniqueID++;
477 Entry = &getContext().pImpl->
478 NamedStructTypes.GetOrCreateValue(TmpStream.str());
479 } while (Entry->getValue());
482 // Okay, we found an entry that isn't used. It's us!
483 Entry->setValue(this);
485 // Delete the old string data.
486 if (SymbolTableEntry)
487 ((EntryTy *)SymbolTableEntry)->Destroy(SymbolTable.getAllocator());
488 SymbolTableEntry = Entry;
491 //===----------------------------------------------------------------------===//
492 // StructType Helper functions.
494 StructType *StructType::create(LLVMContext &Context, StringRef Name) {
495 StructType *ST = new (Context.pImpl->TypeAllocator) StructType(Context);
501 StructType *StructType::get(LLVMContext &Context, bool isPacked) {
502 return get(Context, llvm::ArrayRef<Type*>(), isPacked);
505 StructType *StructType::get(Type *type, ...) {
506 assert(type != 0 && "Cannot create a struct type with no elements with this");
507 LLVMContext &Ctx = type->getContext();
509 SmallVector<llvm::Type*, 8> StructFields;
512 StructFields.push_back(type);
513 type = va_arg(ap, llvm::Type*);
515 return llvm::StructType::get(Ctx, StructFields);
518 StructType *StructType::create(LLVMContext &Context, ArrayRef<Type*> Elements,
519 StringRef Name, bool isPacked) {
520 StructType *ST = create(Context, Name);
521 ST->setBody(Elements, isPacked);
525 StructType *StructType::create(LLVMContext &Context, ArrayRef<Type*> Elements) {
526 return create(Context, Elements, StringRef());
529 StructType *StructType::create(LLVMContext &Context) {
530 return create(Context, StringRef());
533 StructType *StructType::create(ArrayRef<Type*> Elements, StringRef Name,
535 assert(!Elements.empty() &&
536 "This method may not be invoked with an empty list");
537 return create(Elements[0]->getContext(), Elements, Name, isPacked);
540 StructType *StructType::create(ArrayRef<Type*> Elements) {
541 assert(!Elements.empty() &&
542 "This method may not be invoked with an empty list");
543 return create(Elements[0]->getContext(), Elements, StringRef());
546 StructType *StructType::create(StringRef Name, Type *type, ...) {
547 assert(type != 0 && "Cannot create a struct type with no elements with this");
548 LLVMContext &Ctx = type->getContext();
550 SmallVector<llvm::Type*, 8> StructFields;
553 StructFields.push_back(type);
554 type = va_arg(ap, llvm::Type*);
556 return llvm::StructType::create(Ctx, StructFields, Name);
559 bool StructType::isSized() const {
560 if ((getSubclassData() & SCDB_IsSized) != 0)
565 // Okay, our struct is sized if all of the elements are, but if one of the
566 // elements is opaque, the struct isn't sized *yet*, but may become sized in
567 // the future, so just bail out without caching.
568 for (element_iterator I = element_begin(), E = element_end(); I != E; ++I)
569 if (!(*I)->isSized())
572 // Here we cheat a bit and cast away const-ness. The goal is to memoize when
573 // we find a sized type, as types can only move from opaque to sized, not the
575 const_cast<StructType*>(this)->setSubclassData(
576 getSubclassData() | SCDB_IsSized);
580 StringRef StructType::getName() const {
581 assert(!isLiteral() && "Literal structs never have names");
582 if (SymbolTableEntry == 0) return StringRef();
584 return ((StringMapEntry<StructType*> *)SymbolTableEntry)->getKey();
587 void StructType::setBody(Type *type, ...) {
588 assert(type != 0 && "Cannot create a struct type with no elements with this");
590 SmallVector<llvm::Type*, 8> StructFields;
593 StructFields.push_back(type);
594 type = va_arg(ap, llvm::Type*);
596 setBody(StructFields);
599 bool StructType::isValidElementType(Type *ElemTy) {
600 return !ElemTy->isVoidTy() && !ElemTy->isLabelTy() &&
601 !ElemTy->isMetadataTy() && !ElemTy->isFunctionTy();
604 /// isLayoutIdentical - Return true if this is layout identical to the
605 /// specified struct.
606 bool StructType::isLayoutIdentical(StructType *Other) const {
607 if (this == Other) return true;
609 if (isPacked() != Other->isPacked() ||
610 getNumElements() != Other->getNumElements())
613 return std::equal(element_begin(), element_end(), Other->element_begin());
616 /// getTypeByName - Return the type with the specified name, or null if there
617 /// is none by that name.
618 StructType *Module::getTypeByName(StringRef Name) const {
619 StringMap<StructType*>::iterator I =
620 getContext().pImpl->NamedStructTypes.find(Name);
621 if (I != getContext().pImpl->NamedStructTypes.end())
627 //===----------------------------------------------------------------------===//
628 // CompositeType Implementation
629 //===----------------------------------------------------------------------===//
631 Type *CompositeType::getTypeAtIndex(const Value *V) {
632 if (StructType *STy = dyn_cast<StructType>(this)) {
634 (unsigned)cast<Constant>(V)->getUniqueInteger().getZExtValue();
635 assert(indexValid(Idx) && "Invalid structure index!");
636 return STy->getElementType(Idx);
639 return cast<SequentialType>(this)->getElementType();
641 Type *CompositeType::getTypeAtIndex(unsigned Idx) {
642 if (StructType *STy = dyn_cast<StructType>(this)) {
643 assert(indexValid(Idx) && "Invalid structure index!");
644 return STy->getElementType(Idx);
647 return cast<SequentialType>(this)->getElementType();
649 bool CompositeType::indexValid(const Value *V) const {
650 if (const StructType *STy = dyn_cast<StructType>(this)) {
651 // Structure indexes require (vectors of) 32-bit integer constants. In the
652 // vector case all of the indices must be equal.
653 if (!V->getType()->getScalarType()->isIntegerTy(32))
655 const Constant *C = dyn_cast<Constant>(V);
656 if (C && V->getType()->isVectorTy())
657 C = C->getSplatValue();
658 const ConstantInt *CU = dyn_cast_or_null<ConstantInt>(C);
659 return CU && CU->getZExtValue() < STy->getNumElements();
662 // Sequential types can be indexed by any integer.
663 return V->getType()->isIntOrIntVectorTy();
666 bool CompositeType::indexValid(unsigned Idx) const {
667 if (const StructType *STy = dyn_cast<StructType>(this))
668 return Idx < STy->getNumElements();
669 // Sequential types can be indexed by any integer.
674 //===----------------------------------------------------------------------===//
675 // ArrayType Implementation
676 //===----------------------------------------------------------------------===//
678 ArrayType::ArrayType(Type *ElType, uint64_t NumEl)
679 : 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 return ElemTy->isIntegerTy() || ElemTy->isFloatingPointTy() ||
727 ElemTy->isPointerTy();
730 //===----------------------------------------------------------------------===//
731 // PointerType Implementation
732 //===----------------------------------------------------------------------===//
734 PointerType *PointerType::get(Type *EltTy, unsigned AddressSpace) {
735 assert(EltTy && "Can't get a pointer to <null> type!");
736 assert(isValidElementType(EltTy) && "Invalid type for pointer element!");
738 LLVMContextImpl *CImpl = EltTy->getContext().pImpl;
740 // Since AddressSpace #0 is the common case, we special case it.
741 PointerType *&Entry = AddressSpace == 0 ? CImpl->PointerTypes[EltTy]
742 : CImpl->ASPointerTypes[std::make_pair(EltTy, AddressSpace)];
745 Entry = new (CImpl->TypeAllocator) PointerType(EltTy, AddressSpace);
750 PointerType::PointerType(Type *E, unsigned AddrSpace)
751 : SequentialType(PointerTyID, E) {
753 const unsigned oldNCT = NumContainedTys;
755 setSubclassData(AddrSpace);
756 // Check for miscompile. PR11652.
757 assert(oldNCT == NumContainedTys && "bitfield written out of bounds?");
760 PointerType *Type::getPointerTo(unsigned addrs) {
761 return PointerType::get(this, addrs);
764 bool PointerType::isValidElementType(Type *ElemTy) {
765 return !ElemTy->isVoidTy() && !ElemTy->isLabelTy() &&
766 !ElemTy->isMetadataTy();