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