xref: /llvm-project-15.0.7/llvm/lib/IR/Type.cpp (revision ddda456a)
1 //===-- Type.cpp - Implement the Type class -------------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements the Type class for the IR library.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "llvm/IR/Type.h"
15 #include "LLVMContextImpl.h"
16 #include "llvm/ADT/SmallString.h"
17 #include "llvm/IR/Module.h"
18 #include <algorithm>
19 #include <cstdarg>
20 using namespace llvm;
21 
22 //===----------------------------------------------------------------------===//
23 //                         Type Class Implementation
24 //===----------------------------------------------------------------------===//
25 
26 Type *Type::getPrimitiveType(LLVMContext &C, TypeID IDNumber) {
27   switch (IDNumber) {
28   case VoidTyID      : return getVoidTy(C);
29   case HalfTyID      : return getHalfTy(C);
30   case FloatTyID     : return getFloatTy(C);
31   case DoubleTyID    : return getDoubleTy(C);
32   case X86_FP80TyID  : return getX86_FP80Ty(C);
33   case FP128TyID     : return getFP128Ty(C);
34   case PPC_FP128TyID : return getPPC_FP128Ty(C);
35   case LabelTyID     : return getLabelTy(C);
36   case MetadataTyID  : return getMetadataTy(C);
37   case X86_MMXTyID   : return getX86_MMXTy(C);
38   case TokenTyID     : return getTokenTy(C);
39   default:
40     return nullptr;
41   }
42 }
43 
44 bool Type::isIntegerTy(unsigned Bitwidth) const {
45   return isIntegerTy() && cast<IntegerType>(this)->getBitWidth() == Bitwidth;
46 }
47 
48 bool Type::canLosslesslyBitCastTo(Type *Ty) const {
49   // Identity cast means no change so return true
50   if (this == Ty)
51     return true;
52 
53   // They are not convertible unless they are at least first class types
54   if (!this->isFirstClassType() || !Ty->isFirstClassType())
55     return false;
56 
57   // Vector -> Vector conversions are always lossless if the two vector types
58   // have the same size, otherwise not.  Also, 64-bit vector types can be
59   // converted to x86mmx.
60   if (auto *thisPTy = dyn_cast<VectorType>(this)) {
61     if (auto *thatPTy = dyn_cast<VectorType>(Ty))
62       return thisPTy->getBitWidth() == thatPTy->getBitWidth();
63     if (Ty->getTypeID() == Type::X86_MMXTyID &&
64         thisPTy->getBitWidth() == 64)
65       return true;
66   }
67 
68   if (this->getTypeID() == Type::X86_MMXTyID)
69     if (auto *thatPTy = dyn_cast<VectorType>(Ty))
70       if (thatPTy->getBitWidth() == 64)
71         return true;
72 
73   // At this point we have only various mismatches of the first class types
74   // remaining and ptr->ptr. Just select the lossless conversions. Everything
75   // else is not lossless. Conservatively assume we can't losslessly convert
76   // between pointers with different address spaces.
77   if (auto *PTy = dyn_cast<PointerType>(this)) {
78     if (auto *OtherPTy = dyn_cast<PointerType>(Ty))
79       return PTy->getAddressSpace() == OtherPTy->getAddressSpace();
80     return false;
81   }
82   return false;  // Other types have no identity values
83 }
84 
85 bool Type::isEmptyTy() const {
86   if (auto *ATy = dyn_cast<ArrayType>(this)) {
87     unsigned NumElements = ATy->getNumElements();
88     return NumElements == 0 || ATy->getElementType()->isEmptyTy();
89   }
90 
91   if (auto *STy = dyn_cast<StructType>(this)) {
92     unsigned NumElements = STy->getNumElements();
93     for (unsigned i = 0; i < NumElements; ++i)
94       if (!STy->getElementType(i)->isEmptyTy())
95         return false;
96     return true;
97   }
98 
99   return false;
100 }
101 
102 unsigned Type::getPrimitiveSizeInBits() const {
103   switch (getTypeID()) {
104   case Type::HalfTyID: return 16;
105   case Type::FloatTyID: return 32;
106   case Type::DoubleTyID: return 64;
107   case Type::X86_FP80TyID: return 80;
108   case Type::FP128TyID: return 128;
109   case Type::PPC_FP128TyID: return 128;
110   case Type::X86_MMXTyID: return 64;
111   case Type::IntegerTyID: return cast<IntegerType>(this)->getBitWidth();
112   case Type::VectorTyID:  return cast<VectorType>(this)->getBitWidth();
113   default: return 0;
114   }
115 }
116 
117 unsigned Type::getScalarSizeInBits() const {
118   return getScalarType()->getPrimitiveSizeInBits();
119 }
120 
121 int Type::getFPMantissaWidth() const {
122   if (auto *VTy = dyn_cast<VectorType>(this))
123     return VTy->getElementType()->getFPMantissaWidth();
124   assert(isFloatingPointTy() && "Not a floating point type!");
125   if (getTypeID() == HalfTyID) return 11;
126   if (getTypeID() == FloatTyID) return 24;
127   if (getTypeID() == DoubleTyID) return 53;
128   if (getTypeID() == X86_FP80TyID) return 64;
129   if (getTypeID() == FP128TyID) return 113;
130   assert(getTypeID() == PPC_FP128TyID && "unknown fp type");
131   return -1;
132 }
133 
134 bool Type::isSizedDerivedType(SmallPtrSetImpl<Type*> *Visited) const {
135   if (auto *ATy = dyn_cast<ArrayType>(this))
136     return ATy->getElementType()->isSized(Visited);
137 
138   if (auto *VTy = dyn_cast<VectorType>(this))
139     return VTy->getElementType()->isSized(Visited);
140 
141   return cast<StructType>(this)->isSized(Visited);
142 }
143 
144 //===----------------------------------------------------------------------===//
145 //                          Primitive 'Type' data
146 //===----------------------------------------------------------------------===//
147 
148 Type *Type::getVoidTy(LLVMContext &C) { return &C.pImpl->VoidTy; }
149 Type *Type::getLabelTy(LLVMContext &C) { return &C.pImpl->LabelTy; }
150 Type *Type::getHalfTy(LLVMContext &C) { return &C.pImpl->HalfTy; }
151 Type *Type::getFloatTy(LLVMContext &C) { return &C.pImpl->FloatTy; }
152 Type *Type::getDoubleTy(LLVMContext &C) { return &C.pImpl->DoubleTy; }
153 Type *Type::getMetadataTy(LLVMContext &C) { return &C.pImpl->MetadataTy; }
154 Type *Type::getTokenTy(LLVMContext &C) { return &C.pImpl->TokenTy; }
155 Type *Type::getX86_FP80Ty(LLVMContext &C) { return &C.pImpl->X86_FP80Ty; }
156 Type *Type::getFP128Ty(LLVMContext &C) { return &C.pImpl->FP128Ty; }
157 Type *Type::getPPC_FP128Ty(LLVMContext &C) { return &C.pImpl->PPC_FP128Ty; }
158 Type *Type::getX86_MMXTy(LLVMContext &C) { return &C.pImpl->X86_MMXTy; }
159 
160 IntegerType *Type::getInt1Ty(LLVMContext &C) { return &C.pImpl->Int1Ty; }
161 IntegerType *Type::getInt8Ty(LLVMContext &C) { return &C.pImpl->Int8Ty; }
162 IntegerType *Type::getInt16Ty(LLVMContext &C) { return &C.pImpl->Int16Ty; }
163 IntegerType *Type::getInt32Ty(LLVMContext &C) { return &C.pImpl->Int32Ty; }
164 IntegerType *Type::getInt64Ty(LLVMContext &C) { return &C.pImpl->Int64Ty; }
165 IntegerType *Type::getInt128Ty(LLVMContext &C) { return &C.pImpl->Int128Ty; }
166 
167 IntegerType *Type::getIntNTy(LLVMContext &C, unsigned N) {
168   return IntegerType::get(C, N);
169 }
170 
171 PointerType *Type::getHalfPtrTy(LLVMContext &C, unsigned AS) {
172   return getHalfTy(C)->getPointerTo(AS);
173 }
174 
175 PointerType *Type::getFloatPtrTy(LLVMContext &C, unsigned AS) {
176   return getFloatTy(C)->getPointerTo(AS);
177 }
178 
179 PointerType *Type::getDoublePtrTy(LLVMContext &C, unsigned AS) {
180   return getDoubleTy(C)->getPointerTo(AS);
181 }
182 
183 PointerType *Type::getX86_FP80PtrTy(LLVMContext &C, unsigned AS) {
184   return getX86_FP80Ty(C)->getPointerTo(AS);
185 }
186 
187 PointerType *Type::getFP128PtrTy(LLVMContext &C, unsigned AS) {
188   return getFP128Ty(C)->getPointerTo(AS);
189 }
190 
191 PointerType *Type::getPPC_FP128PtrTy(LLVMContext &C, unsigned AS) {
192   return getPPC_FP128Ty(C)->getPointerTo(AS);
193 }
194 
195 PointerType *Type::getX86_MMXPtrTy(LLVMContext &C, unsigned AS) {
196   return getX86_MMXTy(C)->getPointerTo(AS);
197 }
198 
199 PointerType *Type::getIntNPtrTy(LLVMContext &C, unsigned N, unsigned AS) {
200   return getIntNTy(C, N)->getPointerTo(AS);
201 }
202 
203 PointerType *Type::getInt1PtrTy(LLVMContext &C, unsigned AS) {
204   return getInt1Ty(C)->getPointerTo(AS);
205 }
206 
207 PointerType *Type::getInt8PtrTy(LLVMContext &C, unsigned AS) {
208   return getInt8Ty(C)->getPointerTo(AS);
209 }
210 
211 PointerType *Type::getInt16PtrTy(LLVMContext &C, unsigned AS) {
212   return getInt16Ty(C)->getPointerTo(AS);
213 }
214 
215 PointerType *Type::getInt32PtrTy(LLVMContext &C, unsigned AS) {
216   return getInt32Ty(C)->getPointerTo(AS);
217 }
218 
219 PointerType *Type::getInt64PtrTy(LLVMContext &C, unsigned AS) {
220   return getInt64Ty(C)->getPointerTo(AS);
221 }
222 
223 
224 //===----------------------------------------------------------------------===//
225 //                       IntegerType Implementation
226 //===----------------------------------------------------------------------===//
227 
228 IntegerType *IntegerType::get(LLVMContext &C, unsigned NumBits) {
229   assert(NumBits >= MIN_INT_BITS && "bitwidth too small");
230   assert(NumBits <= MAX_INT_BITS && "bitwidth too large");
231 
232   // Check for the built-in integer types
233   switch (NumBits) {
234   case   1: return cast<IntegerType>(Type::getInt1Ty(C));
235   case   8: return cast<IntegerType>(Type::getInt8Ty(C));
236   case  16: return cast<IntegerType>(Type::getInt16Ty(C));
237   case  32: return cast<IntegerType>(Type::getInt32Ty(C));
238   case  64: return cast<IntegerType>(Type::getInt64Ty(C));
239   case 128: return cast<IntegerType>(Type::getInt128Ty(C));
240   default:
241     break;
242   }
243 
244   IntegerType *&Entry = C.pImpl->IntegerTypes[NumBits];
245 
246   if (!Entry)
247     Entry = new (C.pImpl->TypeAllocator) IntegerType(C, NumBits);
248 
249   return Entry;
250 }
251 
252 bool IntegerType::isPowerOf2ByteWidth() const {
253   unsigned BitWidth = getBitWidth();
254   return (BitWidth > 7) && isPowerOf2_32(BitWidth);
255 }
256 
257 APInt IntegerType::getMask() const {
258   return APInt::getAllOnesValue(getBitWidth());
259 }
260 
261 //===----------------------------------------------------------------------===//
262 //                       FunctionType Implementation
263 //===----------------------------------------------------------------------===//
264 
265 FunctionType::FunctionType(Type *Result, ArrayRef<Type*> Params,
266                            bool IsVarArgs)
267   : Type(Result->getContext(), FunctionTyID) {
268   Type **SubTys = reinterpret_cast<Type**>(this+1);
269   assert(isValidReturnType(Result) && "invalid return type for function");
270   setSubclassData(IsVarArgs);
271 
272   SubTys[0] = Result;
273 
274   for (unsigned i = 0, e = Params.size(); i != e; ++i) {
275     assert(isValidArgumentType(Params[i]) &&
276            "Not a valid type for function argument!");
277     SubTys[i+1] = Params[i];
278   }
279 
280   ContainedTys = SubTys;
281   NumContainedTys = Params.size() + 1; // + 1 for result type
282 }
283 
284 // This is the factory function for the FunctionType class.
285 FunctionType *FunctionType::get(Type *ReturnType,
286                                 ArrayRef<Type*> Params, bool isVarArg) {
287   LLVMContextImpl *pImpl = ReturnType->getContext().pImpl;
288   FunctionTypeKeyInfo::KeyTy Key(ReturnType, Params, isVarArg);
289   auto I = pImpl->FunctionTypes.find_as(Key);
290   FunctionType *FT;
291 
292   if (I == pImpl->FunctionTypes.end()) {
293     FT = (FunctionType *)pImpl->TypeAllocator.Allocate(
294         sizeof(FunctionType) + sizeof(Type *) * (Params.size() + 1),
295         alignof(FunctionType));
296     new (FT) FunctionType(ReturnType, Params, isVarArg);
297     pImpl->FunctionTypes.insert(FT);
298   } else {
299     FT = *I;
300   }
301 
302   return FT;
303 }
304 
305 FunctionType *FunctionType::get(Type *Result, bool isVarArg) {
306   return get(Result, None, isVarArg);
307 }
308 
309 bool FunctionType::isValidReturnType(Type *RetTy) {
310   return !RetTy->isFunctionTy() && !RetTy->isLabelTy() &&
311   !RetTy->isMetadataTy();
312 }
313 
314 bool FunctionType::isValidArgumentType(Type *ArgTy) {
315   return ArgTy->isFirstClassType();
316 }
317 
318 //===----------------------------------------------------------------------===//
319 //                       StructType Implementation
320 //===----------------------------------------------------------------------===//
321 
322 // Primitive Constructors.
323 
324 StructType *StructType::get(LLVMContext &Context, ArrayRef<Type*> ETypes,
325                             bool isPacked) {
326   LLVMContextImpl *pImpl = Context.pImpl;
327   AnonStructTypeKeyInfo::KeyTy Key(ETypes, isPacked);
328   auto I = pImpl->AnonStructTypes.find_as(Key);
329   StructType *ST;
330 
331   if (I == pImpl->AnonStructTypes.end()) {
332     // Value not found.  Create a new type!
333     ST = new (Context.pImpl->TypeAllocator) StructType(Context);
334     ST->setSubclassData(SCDB_IsLiteral);  // Literal struct.
335     ST->setBody(ETypes, isPacked);
336     Context.pImpl->AnonStructTypes.insert(ST);
337   } else {
338     ST = *I;
339   }
340 
341   return ST;
342 }
343 
344 void StructType::setBody(ArrayRef<Type*> Elements, bool isPacked) {
345   assert(isOpaque() && "Struct body already set!");
346 
347   setSubclassData(getSubclassData() | SCDB_HasBody);
348   if (isPacked)
349     setSubclassData(getSubclassData() | SCDB_Packed);
350 
351   NumContainedTys = Elements.size();
352 
353   if (Elements.empty()) {
354     ContainedTys = nullptr;
355     return;
356   }
357 
358   ContainedTys = Elements.copy(getContext().pImpl->TypeAllocator).data();
359 }
360 
361 void StructType::setName(StringRef Name) {
362   if (Name == getName()) return;
363 
364   StringMap<StructType *> &SymbolTable = getContext().pImpl->NamedStructTypes;
365   typedef StringMap<StructType *>::MapEntryTy EntryTy;
366 
367   // If this struct already had a name, remove its symbol table entry. Don't
368   // delete the data yet because it may be part of the new name.
369   if (SymbolTableEntry)
370     SymbolTable.remove((EntryTy *)SymbolTableEntry);
371 
372   // If this is just removing the name, we're done.
373   if (Name.empty()) {
374     if (SymbolTableEntry) {
375       // Delete the old string data.
376       ((EntryTy *)SymbolTableEntry)->Destroy(SymbolTable.getAllocator());
377       SymbolTableEntry = nullptr;
378     }
379     return;
380   }
381 
382   // Look up the entry for the name.
383   auto IterBool =
384       getContext().pImpl->NamedStructTypes.insert(std::make_pair(Name, this));
385 
386   // While we have a name collision, try a random rename.
387   if (!IterBool.second) {
388     SmallString<64> TempStr(Name);
389     TempStr.push_back('.');
390     raw_svector_ostream TmpStream(TempStr);
391     unsigned NameSize = Name.size();
392 
393     do {
394       TempStr.resize(NameSize + 1);
395       TmpStream << getContext().pImpl->NamedStructTypesUniqueID++;
396 
397       IterBool = getContext().pImpl->NamedStructTypes.insert(
398           std::make_pair(TmpStream.str(), this));
399     } while (!IterBool.second);
400   }
401 
402   // Delete the old string data.
403   if (SymbolTableEntry)
404     ((EntryTy *)SymbolTableEntry)->Destroy(SymbolTable.getAllocator());
405   SymbolTableEntry = &*IterBool.first;
406 }
407 
408 //===----------------------------------------------------------------------===//
409 // StructType Helper functions.
410 
411 StructType *StructType::create(LLVMContext &Context, StringRef Name) {
412   StructType *ST = new (Context.pImpl->TypeAllocator) StructType(Context);
413   if (!Name.empty())
414     ST->setName(Name);
415   return ST;
416 }
417 
418 StructType *StructType::get(LLVMContext &Context, bool isPacked) {
419   return get(Context, None, isPacked);
420 }
421 
422 StructType *StructType::get(Type *type, ...) {
423   assert(type && "Cannot create a struct type with no elements with this");
424   LLVMContext &Ctx = type->getContext();
425   va_list ap;
426   SmallVector<llvm::Type*, 8> StructFields;
427   va_start(ap, type);
428   while (type) {
429     StructFields.push_back(type);
430     type = va_arg(ap, llvm::Type*);
431   }
432   auto *Ret = llvm::StructType::get(Ctx, StructFields);
433   va_end(ap);
434   return Ret;
435 }
436 
437 StructType *StructType::create(LLVMContext &Context, ArrayRef<Type*> Elements,
438                                StringRef Name, bool isPacked) {
439   StructType *ST = create(Context, Name);
440   ST->setBody(Elements, isPacked);
441   return ST;
442 }
443 
444 StructType *StructType::create(LLVMContext &Context, ArrayRef<Type*> Elements) {
445   return create(Context, Elements, StringRef());
446 }
447 
448 StructType *StructType::create(LLVMContext &Context) {
449   return create(Context, StringRef());
450 }
451 
452 StructType *StructType::create(ArrayRef<Type*> Elements, StringRef Name,
453                                bool isPacked) {
454   assert(!Elements.empty() &&
455          "This method may not be invoked with an empty list");
456   return create(Elements[0]->getContext(), Elements, Name, isPacked);
457 }
458 
459 StructType *StructType::create(ArrayRef<Type*> Elements) {
460   assert(!Elements.empty() &&
461          "This method may not be invoked with an empty list");
462   return create(Elements[0]->getContext(), Elements, StringRef());
463 }
464 
465 StructType *StructType::create(StringRef Name, Type *type, ...) {
466   assert(type && "Cannot create a struct type with no elements with this");
467   LLVMContext &Ctx = type->getContext();
468   va_list ap;
469   SmallVector<llvm::Type*, 8> StructFields;
470   va_start(ap, type);
471   while (type) {
472     StructFields.push_back(type);
473     type = va_arg(ap, llvm::Type*);
474   }
475   auto *Ret = llvm::StructType::create(Ctx, StructFields, Name);
476   va_end(ap);
477   return Ret;
478 }
479 
480 bool StructType::isSized(SmallPtrSetImpl<Type*> *Visited) const {
481   if ((getSubclassData() & SCDB_IsSized) != 0)
482     return true;
483   if (isOpaque())
484     return false;
485 
486   if (Visited && !Visited->insert(const_cast<StructType*>(this)).second)
487     return false;
488 
489   // Okay, our struct is sized if all of the elements are, but if one of the
490   // elements is opaque, the struct isn't sized *yet*, but may become sized in
491   // the future, so just bail out without caching.
492   for (element_iterator I = element_begin(), E = element_end(); I != E; ++I)
493     if (!(*I)->isSized(Visited))
494       return false;
495 
496   // Here we cheat a bit and cast away const-ness. The goal is to memoize when
497   // we find a sized type, as types can only move from opaque to sized, not the
498   // other way.
499   const_cast<StructType*>(this)->setSubclassData(
500     getSubclassData() | SCDB_IsSized);
501   return true;
502 }
503 
504 StringRef StructType::getName() const {
505   assert(!isLiteral() && "Literal structs never have names");
506   if (!SymbolTableEntry) return StringRef();
507 
508   return ((StringMapEntry<StructType*> *)SymbolTableEntry)->getKey();
509 }
510 
511 void StructType::setBody(Type *type, ...) {
512   assert(type && "Cannot create a struct type with no elements with this");
513   va_list ap;
514   SmallVector<llvm::Type*, 8> StructFields;
515   va_start(ap, type);
516   while (type) {
517     StructFields.push_back(type);
518     type = va_arg(ap, llvm::Type*);
519   }
520   setBody(StructFields);
521   va_end(ap);
522 }
523 
524 bool StructType::isValidElementType(Type *ElemTy) {
525   return !ElemTy->isVoidTy() && !ElemTy->isLabelTy() &&
526          !ElemTy->isMetadataTy() && !ElemTy->isFunctionTy() &&
527          !ElemTy->isTokenTy();
528 }
529 
530 bool StructType::isLayoutIdentical(StructType *Other) const {
531   if (this == Other) return true;
532 
533   if (isPacked() != Other->isPacked())
534     return false;
535 
536   return elements() == Other->elements();
537 }
538 
539 StructType *Module::getTypeByName(StringRef Name) const {
540   return getContext().pImpl->NamedStructTypes.lookup(Name);
541 }
542 
543 
544 //===----------------------------------------------------------------------===//
545 //                       CompositeType Implementation
546 //===----------------------------------------------------------------------===//
547 
548 Type *CompositeType::getTypeAtIndex(const Value *V) const {
549   if (auto *STy = dyn_cast<StructType>(this)) {
550     unsigned Idx =
551       (unsigned)cast<Constant>(V)->getUniqueInteger().getZExtValue();
552     assert(indexValid(Idx) && "Invalid structure index!");
553     return STy->getElementType(Idx);
554   }
555 
556   return cast<SequentialType>(this)->getElementType();
557 }
558 
559 Type *CompositeType::getTypeAtIndex(unsigned Idx) const{
560   if (auto *STy = dyn_cast<StructType>(this)) {
561     assert(indexValid(Idx) && "Invalid structure index!");
562     return STy->getElementType(Idx);
563   }
564 
565   return cast<SequentialType>(this)->getElementType();
566 }
567 
568 bool CompositeType::indexValid(const Value *V) const {
569   if (auto *STy = dyn_cast<StructType>(this)) {
570     // Structure indexes require (vectors of) 32-bit integer constants.  In the
571     // vector case all of the indices must be equal.
572     if (!V->getType()->getScalarType()->isIntegerTy(32))
573       return false;
574     const Constant *C = dyn_cast<Constant>(V);
575     if (C && V->getType()->isVectorTy())
576       C = C->getSplatValue();
577     const ConstantInt *CU = dyn_cast_or_null<ConstantInt>(C);
578     return CU && CU->getZExtValue() < STy->getNumElements();
579   }
580 
581   // Sequential types can be indexed by any integer.
582   return V->getType()->isIntOrIntVectorTy();
583 }
584 
585 bool CompositeType::indexValid(unsigned Idx) const {
586   if (auto *STy = dyn_cast<StructType>(this))
587     return Idx < STy->getNumElements();
588   // Sequential types can be indexed by any integer.
589   return true;
590 }
591 
592 
593 //===----------------------------------------------------------------------===//
594 //                           ArrayType Implementation
595 //===----------------------------------------------------------------------===//
596 
597 ArrayType::ArrayType(Type *ElType, uint64_t NumEl)
598   : SequentialType(ArrayTyID, ElType, NumEl) {}
599 
600 ArrayType *ArrayType::get(Type *ElementType, uint64_t NumElements) {
601   assert(isValidElementType(ElementType) && "Invalid type for array element!");
602 
603   LLVMContextImpl *pImpl = ElementType->getContext().pImpl;
604   ArrayType *&Entry =
605     pImpl->ArrayTypes[std::make_pair(ElementType, NumElements)];
606 
607   if (!Entry)
608     Entry = new (pImpl->TypeAllocator) ArrayType(ElementType, NumElements);
609   return Entry;
610 }
611 
612 bool ArrayType::isValidElementType(Type *ElemTy) {
613   return !ElemTy->isVoidTy() && !ElemTy->isLabelTy() &&
614          !ElemTy->isMetadataTy() && !ElemTy->isFunctionTy() &&
615          !ElemTy->isTokenTy();
616 }
617 
618 //===----------------------------------------------------------------------===//
619 //                          VectorType Implementation
620 //===----------------------------------------------------------------------===//
621 
622 VectorType::VectorType(Type *ElType, unsigned NumEl)
623   : SequentialType(VectorTyID, ElType, NumEl) {}
624 
625 VectorType *VectorType::get(Type *ElementType, unsigned NumElements) {
626   assert(NumElements > 0 && "#Elements of a VectorType must be greater than 0");
627   assert(isValidElementType(ElementType) && "Element type of a VectorType must "
628                                             "be an integer, floating point, or "
629                                             "pointer type.");
630 
631   LLVMContextImpl *pImpl = ElementType->getContext().pImpl;
632   VectorType *&Entry = ElementType->getContext().pImpl
633     ->VectorTypes[std::make_pair(ElementType, NumElements)];
634 
635   if (!Entry)
636     Entry = new (pImpl->TypeAllocator) VectorType(ElementType, NumElements);
637   return Entry;
638 }
639 
640 bool VectorType::isValidElementType(Type *ElemTy) {
641   return ElemTy->isIntegerTy() || ElemTy->isFloatingPointTy() ||
642     ElemTy->isPointerTy();
643 }
644 
645 //===----------------------------------------------------------------------===//
646 //                         PointerType Implementation
647 //===----------------------------------------------------------------------===//
648 
649 PointerType *PointerType::get(Type *EltTy, unsigned AddressSpace) {
650   assert(EltTy && "Can't get a pointer to <null> type!");
651   assert(isValidElementType(EltTy) && "Invalid type for pointer element!");
652 
653   LLVMContextImpl *CImpl = EltTy->getContext().pImpl;
654 
655   // Since AddressSpace #0 is the common case, we special case it.
656   PointerType *&Entry = AddressSpace == 0 ? CImpl->PointerTypes[EltTy]
657      : CImpl->ASPointerTypes[std::make_pair(EltTy, AddressSpace)];
658 
659   if (!Entry)
660     Entry = new (CImpl->TypeAllocator) PointerType(EltTy, AddressSpace);
661   return Entry;
662 }
663 
664 
665 PointerType::PointerType(Type *E, unsigned AddrSpace)
666   : Type(E->getContext(), PointerTyID), PointeeTy(E) {
667   ContainedTys = &PointeeTy;
668   NumContainedTys = 1;
669   setSubclassData(AddrSpace);
670 }
671 
672 PointerType *Type::getPointerTo(unsigned addrs) const {
673   return PointerType::get(const_cast<Type*>(this), addrs);
674 }
675 
676 bool PointerType::isValidElementType(Type *ElemTy) {
677   return !ElemTy->isVoidTy() && !ElemTy->isLabelTy() &&
678          !ElemTy->isMetadataTy() && !ElemTy->isTokenTy();
679 }
680 
681 bool PointerType::isLoadableOrStorableType(Type *ElemTy) {
682   return isValidElementType(ElemTy) && !ElemTy->isFunctionTy();
683 }
684