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