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