LLVM  mainline
Type.cpp
Go to the documentation of this file.
00001 //===-- Type.cpp - Implement the Type class -------------------------------===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file implements the Type class for the IR library.
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #include "llvm/IR/Type.h"
00015 #include "LLVMContextImpl.h"
00016 #include "llvm/ADT/SmallString.h"
00017 #include "llvm/IR/Module.h"
00018 #include <algorithm>
00019 #include <cstdarg>
00020 using namespace llvm;
00021 
00022 //===----------------------------------------------------------------------===//
00023 //                         Type Class Implementation
00024 //===----------------------------------------------------------------------===//
00025 
00026 Type *Type::getPrimitiveType(LLVMContext &C, TypeID IDNumber) {
00027   switch (IDNumber) {
00028   case VoidTyID      : return getVoidTy(C);
00029   case HalfTyID      : return getHalfTy(C);
00030   case FloatTyID     : return getFloatTy(C);
00031   case DoubleTyID    : return getDoubleTy(C);
00032   case X86_FP80TyID  : return getX86_FP80Ty(C);
00033   case FP128TyID     : return getFP128Ty(C);
00034   case PPC_FP128TyID : return getPPC_FP128Ty(C);
00035   case LabelTyID     : return getLabelTy(C);
00036   case MetadataTyID  : return getMetadataTy(C);
00037   case X86_MMXTyID   : return getX86_MMXTy(C);
00038   default:
00039     return nullptr;
00040   }
00041 }
00042 
00043 /// getScalarType - If this is a vector type, return the element type,
00044 /// otherwise return this.
00045 Type *Type::getScalarType() {
00046   if (VectorType *VTy = dyn_cast<VectorType>(this))
00047     return VTy->getElementType();
00048   return this;
00049 }
00050 
00051 const Type *Type::getScalarType() const {
00052   if (const VectorType *VTy = dyn_cast<VectorType>(this))
00053     return VTy->getElementType();
00054   return this;
00055 }
00056 
00057 /// isIntegerTy - Return true if this is an IntegerType of the specified width.
00058 bool Type::isIntegerTy(unsigned Bitwidth) const {
00059   return isIntegerTy() && cast<IntegerType>(this)->getBitWidth() == Bitwidth;
00060 }
00061 
00062 // canLosslesslyBitCastTo - Return true if this type can be converted to
00063 // 'Ty' without any reinterpretation of bits.  For example, i8* to i32*.
00064 //
00065 bool Type::canLosslesslyBitCastTo(Type *Ty) const {
00066   // Identity cast means no change so return true
00067   if (this == Ty) 
00068     return true;
00069   
00070   // They are not convertible unless they are at least first class types
00071   if (!this->isFirstClassType() || !Ty->isFirstClassType())
00072     return false;
00073 
00074   // Vector -> Vector conversions are always lossless if the two vector types
00075   // have the same size, otherwise not.  Also, 64-bit vector types can be
00076   // converted to x86mmx.
00077   if (const VectorType *thisPTy = dyn_cast<VectorType>(this)) {
00078     if (const VectorType *thatPTy = dyn_cast<VectorType>(Ty))
00079       return thisPTy->getBitWidth() == thatPTy->getBitWidth();
00080     if (Ty->getTypeID() == Type::X86_MMXTyID &&
00081         thisPTy->getBitWidth() == 64)
00082       return true;
00083   }
00084 
00085   if (this->getTypeID() == Type::X86_MMXTyID)
00086     if (const VectorType *thatPTy = dyn_cast<VectorType>(Ty))
00087       if (thatPTy->getBitWidth() == 64)
00088         return true;
00089 
00090   // At this point we have only various mismatches of the first class types
00091   // remaining and ptr->ptr. Just select the lossless conversions. Everything
00092   // else is not lossless. Conservatively assume we can't losslessly convert
00093   // between pointers with different address spaces.
00094   if (const PointerType *PTy = dyn_cast<PointerType>(this)) {
00095     if (const PointerType *OtherPTy = dyn_cast<PointerType>(Ty))
00096       return PTy->getAddressSpace() == OtherPTy->getAddressSpace();
00097     return false;
00098   }
00099   return false;  // Other types have no identity values
00100 }
00101 
00102 bool Type::isEmptyTy() const {
00103   const ArrayType *ATy = dyn_cast<ArrayType>(this);
00104   if (ATy) {
00105     unsigned NumElements = ATy->getNumElements();
00106     return NumElements == 0 || ATy->getElementType()->isEmptyTy();
00107   }
00108 
00109   const StructType *STy = dyn_cast<StructType>(this);
00110   if (STy) {
00111     unsigned NumElements = STy->getNumElements();
00112     for (unsigned i = 0; i < NumElements; ++i)
00113       if (!STy->getElementType(i)->isEmptyTy())
00114         return false;
00115     return true;
00116   }
00117 
00118   return false;
00119 }
00120 
00121 unsigned Type::getPrimitiveSizeInBits() const {
00122   switch (getTypeID()) {
00123   case Type::HalfTyID: return 16;
00124   case Type::FloatTyID: return 32;
00125   case Type::DoubleTyID: return 64;
00126   case Type::X86_FP80TyID: return 80;
00127   case Type::FP128TyID: return 128;
00128   case Type::PPC_FP128TyID: return 128;
00129   case Type::X86_MMXTyID: return 64;
00130   case Type::IntegerTyID: return cast<IntegerType>(this)->getBitWidth();
00131   case Type::VectorTyID:  return cast<VectorType>(this)->getBitWidth();
00132   default: return 0;
00133   }
00134 }
00135 
00136 /// getScalarSizeInBits - If this is a vector type, return the
00137 /// getPrimitiveSizeInBits value for the element type. Otherwise return the
00138 /// getPrimitiveSizeInBits value for this type.
00139 unsigned Type::getScalarSizeInBits() const {
00140   return getScalarType()->getPrimitiveSizeInBits();
00141 }
00142 
00143 /// getFPMantissaWidth - Return the width of the mantissa of this type.  This
00144 /// is only valid on floating point types.  If the FP type does not
00145 /// have a stable mantissa (e.g. ppc long double), this method returns -1.
00146 int Type::getFPMantissaWidth() const {
00147   if (const VectorType *VTy = dyn_cast<VectorType>(this))
00148     return VTy->getElementType()->getFPMantissaWidth();
00149   assert(isFloatingPointTy() && "Not a floating point type!");
00150   if (getTypeID() == HalfTyID) return 11;
00151   if (getTypeID() == FloatTyID) return 24;
00152   if (getTypeID() == DoubleTyID) return 53;
00153   if (getTypeID() == X86_FP80TyID) return 64;
00154   if (getTypeID() == FP128TyID) return 113;
00155   assert(getTypeID() == PPC_FP128TyID && "unknown fp type");
00156   return -1;
00157 }
00158 
00159 /// isSizedDerivedType - Derived types like structures and arrays are sized
00160 /// iff all of the members of the type are sized as well.  Since asking for
00161 /// their size is relatively uncommon, move this operation out of line.
00162 bool Type::isSizedDerivedType(SmallPtrSetImpl<const Type*> *Visited) const {
00163   if (const ArrayType *ATy = dyn_cast<ArrayType>(this))
00164     return ATy->getElementType()->isSized(Visited);
00165 
00166   if (const VectorType *VTy = dyn_cast<VectorType>(this))
00167     return VTy->getElementType()->isSized(Visited);
00168 
00169   return cast<StructType>(this)->isSized(Visited);
00170 }
00171 
00172 //===----------------------------------------------------------------------===//
00173 //                         Subclass Helper Methods
00174 //===----------------------------------------------------------------------===//
00175 
00176 unsigned Type::getIntegerBitWidth() const {
00177   return cast<IntegerType>(this)->getBitWidth();
00178 }
00179 
00180 bool Type::isFunctionVarArg() const {
00181   return cast<FunctionType>(this)->isVarArg();
00182 }
00183 
00184 Type *Type::getFunctionParamType(unsigned i) const {
00185   return cast<FunctionType>(this)->getParamType(i);
00186 }
00187 
00188 unsigned Type::getFunctionNumParams() const {
00189   return cast<FunctionType>(this)->getNumParams();
00190 }
00191 
00192 StringRef Type::getStructName() const {
00193   return cast<StructType>(this)->getName();
00194 }
00195 
00196 unsigned Type::getStructNumElements() const {
00197   return cast<StructType>(this)->getNumElements();
00198 }
00199 
00200 Type *Type::getStructElementType(unsigned N) const {
00201   return cast<StructType>(this)->getElementType(N);
00202 }
00203 
00204 Type *Type::getSequentialElementType() const {
00205   return cast<SequentialType>(this)->getElementType();
00206 }
00207 
00208 uint64_t Type::getArrayNumElements() const {
00209   return cast<ArrayType>(this)->getNumElements();
00210 }
00211 
00212 unsigned Type::getVectorNumElements() const {
00213   return cast<VectorType>(this)->getNumElements();
00214 }
00215 
00216 unsigned Type::getPointerAddressSpace() const {
00217   return cast<PointerType>(getScalarType())->getAddressSpace();
00218 }
00219 
00220 
00221 //===----------------------------------------------------------------------===//
00222 //                          Primitive 'Type' data
00223 //===----------------------------------------------------------------------===//
00224 
00225 Type *Type::getVoidTy(LLVMContext &C) { return &C.pImpl->VoidTy; }
00226 Type *Type::getLabelTy(LLVMContext &C) { return &C.pImpl->LabelTy; }
00227 Type *Type::getHalfTy(LLVMContext &C) { return &C.pImpl->HalfTy; }
00228 Type *Type::getFloatTy(LLVMContext &C) { return &C.pImpl->FloatTy; }
00229 Type *Type::getDoubleTy(LLVMContext &C) { return &C.pImpl->DoubleTy; }
00230 Type *Type::getMetadataTy(LLVMContext &C) { return &C.pImpl->MetadataTy; }
00231 Type *Type::getX86_FP80Ty(LLVMContext &C) { return &C.pImpl->X86_FP80Ty; }
00232 Type *Type::getFP128Ty(LLVMContext &C) { return &C.pImpl->FP128Ty; }
00233 Type *Type::getPPC_FP128Ty(LLVMContext &C) { return &C.pImpl->PPC_FP128Ty; }
00234 Type *Type::getX86_MMXTy(LLVMContext &C) { return &C.pImpl->X86_MMXTy; }
00235 
00236 IntegerType *Type::getInt1Ty(LLVMContext &C) { return &C.pImpl->Int1Ty; }
00237 IntegerType *Type::getInt8Ty(LLVMContext &C) { return &C.pImpl->Int8Ty; }
00238 IntegerType *Type::getInt16Ty(LLVMContext &C) { return &C.pImpl->Int16Ty; }
00239 IntegerType *Type::getInt32Ty(LLVMContext &C) { return &C.pImpl->Int32Ty; }
00240 IntegerType *Type::getInt64Ty(LLVMContext &C) { return &C.pImpl->Int64Ty; }
00241 IntegerType *Type::getInt128Ty(LLVMContext &C) { return &C.pImpl->Int128Ty; }
00242 
00243 IntegerType *Type::getIntNTy(LLVMContext &C, unsigned N) {
00244   return IntegerType::get(C, N);
00245 }
00246 
00247 PointerType *Type::getHalfPtrTy(LLVMContext &C, unsigned AS) {
00248   return getHalfTy(C)->getPointerTo(AS);
00249 }
00250 
00251 PointerType *Type::getFloatPtrTy(LLVMContext &C, unsigned AS) {
00252   return getFloatTy(C)->getPointerTo(AS);
00253 }
00254 
00255 PointerType *Type::getDoublePtrTy(LLVMContext &C, unsigned AS) {
00256   return getDoubleTy(C)->getPointerTo(AS);
00257 }
00258 
00259 PointerType *Type::getX86_FP80PtrTy(LLVMContext &C, unsigned AS) {
00260   return getX86_FP80Ty(C)->getPointerTo(AS);
00261 }
00262 
00263 PointerType *Type::getFP128PtrTy(LLVMContext &C, unsigned AS) {
00264   return getFP128Ty(C)->getPointerTo(AS);
00265 }
00266 
00267 PointerType *Type::getPPC_FP128PtrTy(LLVMContext &C, unsigned AS) {
00268   return getPPC_FP128Ty(C)->getPointerTo(AS);
00269 }
00270 
00271 PointerType *Type::getX86_MMXPtrTy(LLVMContext &C, unsigned AS) {
00272   return getX86_MMXTy(C)->getPointerTo(AS);
00273 }
00274 
00275 PointerType *Type::getIntNPtrTy(LLVMContext &C, unsigned N, unsigned AS) {
00276   return getIntNTy(C, N)->getPointerTo(AS);
00277 }
00278 
00279 PointerType *Type::getInt1PtrTy(LLVMContext &C, unsigned AS) {
00280   return getInt1Ty(C)->getPointerTo(AS);
00281 }
00282 
00283 PointerType *Type::getInt8PtrTy(LLVMContext &C, unsigned AS) {
00284   return getInt8Ty(C)->getPointerTo(AS);
00285 }
00286 
00287 PointerType *Type::getInt16PtrTy(LLVMContext &C, unsigned AS) {
00288   return getInt16Ty(C)->getPointerTo(AS);
00289 }
00290 
00291 PointerType *Type::getInt32PtrTy(LLVMContext &C, unsigned AS) {
00292   return getInt32Ty(C)->getPointerTo(AS);
00293 }
00294 
00295 PointerType *Type::getInt64PtrTy(LLVMContext &C, unsigned AS) {
00296   return getInt64Ty(C)->getPointerTo(AS);
00297 }
00298 
00299 
00300 //===----------------------------------------------------------------------===//
00301 //                       IntegerType Implementation
00302 //===----------------------------------------------------------------------===//
00303 
00304 IntegerType *IntegerType::get(LLVMContext &C, unsigned NumBits) {
00305   assert(NumBits >= MIN_INT_BITS && "bitwidth too small");
00306   assert(NumBits <= MAX_INT_BITS && "bitwidth too large");
00307   
00308   // Check for the built-in integer types
00309   switch (NumBits) {
00310   case  1: return cast<IntegerType>(Type::getInt1Ty(C));
00311   case  8: return cast<IntegerType>(Type::getInt8Ty(C));
00312   case 16: return cast<IntegerType>(Type::getInt16Ty(C));
00313   case 32: return cast<IntegerType>(Type::getInt32Ty(C));
00314   case 64: return cast<IntegerType>(Type::getInt64Ty(C));
00315   default: 
00316     break;
00317   }
00318   
00319   IntegerType *&Entry = C.pImpl->IntegerTypes[NumBits];
00320 
00321   if (!Entry)
00322     Entry = new (C.pImpl->TypeAllocator) IntegerType(C, NumBits);
00323   
00324   return Entry;
00325 }
00326 
00327 bool IntegerType::isPowerOf2ByteWidth() const {
00328   unsigned BitWidth = getBitWidth();
00329   return (BitWidth > 7) && isPowerOf2_32(BitWidth);
00330 }
00331 
00332 APInt IntegerType::getMask() const {
00333   return APInt::getAllOnesValue(getBitWidth());
00334 }
00335 
00336 //===----------------------------------------------------------------------===//
00337 //                       FunctionType Implementation
00338 //===----------------------------------------------------------------------===//
00339 
00340 FunctionType::FunctionType(Type *Result, ArrayRef<Type*> Params,
00341                            bool IsVarArgs)
00342   : Type(Result->getContext(), FunctionTyID) {
00343   Type **SubTys = reinterpret_cast<Type**>(this+1);
00344   assert(isValidReturnType(Result) && "invalid return type for function");
00345   setSubclassData(IsVarArgs);
00346 
00347   SubTys[0] = const_cast<Type*>(Result);
00348 
00349   for (unsigned i = 0, e = Params.size(); i != e; ++i) {
00350     assert(isValidArgumentType(Params[i]) &&
00351            "Not a valid type for function argument!");
00352     SubTys[i+1] = Params[i];
00353   }
00354 
00355   ContainedTys = SubTys;
00356   NumContainedTys = Params.size() + 1; // + 1 for result type
00357 }
00358 
00359 // FunctionType::get - The factory function for the FunctionType class.
00360 FunctionType *FunctionType::get(Type *ReturnType,
00361                                 ArrayRef<Type*> Params, bool isVarArg) {
00362   LLVMContextImpl *pImpl = ReturnType->getContext().pImpl;
00363   FunctionTypeKeyInfo::KeyTy Key(ReturnType, Params, isVarArg);
00364   auto I = pImpl->FunctionTypes.find_as(Key);
00365   FunctionType *FT;
00366 
00367   if (I == pImpl->FunctionTypes.end()) {
00368     FT = (FunctionType*) pImpl->TypeAllocator.
00369       Allocate(sizeof(FunctionType) + sizeof(Type*) * (Params.size() + 1),
00370                AlignOf<FunctionType>::Alignment);
00371     new (FT) FunctionType(ReturnType, Params, isVarArg);
00372     pImpl->FunctionTypes.insert(FT);
00373   } else {
00374     FT = *I;
00375   }
00376 
00377   return FT;
00378 }
00379 
00380 FunctionType *FunctionType::get(Type *Result, bool isVarArg) {
00381   return get(Result, None, isVarArg);
00382 }
00383 
00384 /// isValidReturnType - Return true if the specified type is valid as a return
00385 /// type.
00386 bool FunctionType::isValidReturnType(Type *RetTy) {
00387   return !RetTy->isFunctionTy() && !RetTy->isLabelTy() &&
00388   !RetTy->isMetadataTy();
00389 }
00390 
00391 /// isValidArgumentType - Return true if the specified type is valid as an
00392 /// argument type.
00393 bool FunctionType::isValidArgumentType(Type *ArgTy) {
00394   return ArgTy->isFirstClassType();
00395 }
00396 
00397 //===----------------------------------------------------------------------===//
00398 //                       StructType Implementation
00399 //===----------------------------------------------------------------------===//
00400 
00401 // Primitive Constructors.
00402 
00403 StructType *StructType::get(LLVMContext &Context, ArrayRef<Type*> ETypes, 
00404                             bool isPacked) {
00405   LLVMContextImpl *pImpl = Context.pImpl;
00406   AnonStructTypeKeyInfo::KeyTy Key(ETypes, isPacked);
00407   auto I = pImpl->AnonStructTypes.find_as(Key);
00408   StructType *ST;
00409 
00410   if (I == pImpl->AnonStructTypes.end()) {
00411     // Value not found.  Create a new type!
00412     ST = new (Context.pImpl->TypeAllocator) StructType(Context);
00413     ST->setSubclassData(SCDB_IsLiteral);  // Literal struct.
00414     ST->setBody(ETypes, isPacked);
00415     Context.pImpl->AnonStructTypes.insert(ST);
00416   } else {
00417     ST = *I;
00418   }
00419 
00420   return ST;
00421 }
00422 
00423 void StructType::setBody(ArrayRef<Type*> Elements, bool isPacked) {
00424   assert(isOpaque() && "Struct body already set!");
00425   
00426   setSubclassData(getSubclassData() | SCDB_HasBody);
00427   if (isPacked)
00428     setSubclassData(getSubclassData() | SCDB_Packed);
00429 
00430   unsigned NumElements = Elements.size();
00431   Type **Elts = getContext().pImpl->TypeAllocator.Allocate<Type*>(NumElements);
00432   memcpy(Elts, Elements.data(), sizeof(Elements[0]) * NumElements);
00433   
00434   ContainedTys = Elts;
00435   NumContainedTys = NumElements;
00436 }
00437 
00438 void StructType::setName(StringRef Name) {
00439   if (Name == getName()) return;
00440 
00441   StringMap<StructType *> &SymbolTable = getContext().pImpl->NamedStructTypes;
00442   typedef StringMap<StructType *>::MapEntryTy EntryTy;
00443 
00444   // If this struct already had a name, remove its symbol table entry. Don't
00445   // delete the data yet because it may be part of the new name.
00446   if (SymbolTableEntry)
00447     SymbolTable.remove((EntryTy *)SymbolTableEntry);
00448 
00449   // If this is just removing the name, we're done.
00450   if (Name.empty()) {
00451     if (SymbolTableEntry) {
00452       // Delete the old string data.
00453       ((EntryTy *)SymbolTableEntry)->Destroy(SymbolTable.getAllocator());
00454       SymbolTableEntry = nullptr;
00455     }
00456     return;
00457   }
00458   
00459   // Look up the entry for the name.
00460   auto IterBool =
00461       getContext().pImpl->NamedStructTypes.insert(std::make_pair(Name, this));
00462 
00463   // While we have a name collision, try a random rename.
00464   if (!IterBool.second) {
00465     SmallString<64> TempStr(Name);
00466     TempStr.push_back('.');
00467     raw_svector_ostream TmpStream(TempStr);
00468     unsigned NameSize = Name.size();
00469    
00470     do {
00471       TempStr.resize(NameSize + 1);
00472       TmpStream.resync();
00473       TmpStream << getContext().pImpl->NamedStructTypesUniqueID++;
00474 
00475       IterBool = getContext().pImpl->NamedStructTypes.insert(
00476           std::make_pair(TmpStream.str(), this));
00477     } while (!IterBool.second);
00478   }
00479 
00480   // Delete the old string data.
00481   if (SymbolTableEntry)
00482     ((EntryTy *)SymbolTableEntry)->Destroy(SymbolTable.getAllocator());
00483   SymbolTableEntry = &*IterBool.first;
00484 }
00485 
00486 //===----------------------------------------------------------------------===//
00487 // StructType Helper functions.
00488 
00489 StructType *StructType::create(LLVMContext &Context, StringRef Name) {
00490   StructType *ST = new (Context.pImpl->TypeAllocator) StructType(Context);
00491   if (!Name.empty())
00492     ST->setName(Name);
00493   return ST;
00494 }
00495 
00496 StructType *StructType::get(LLVMContext &Context, bool isPacked) {
00497   return get(Context, None, isPacked);
00498 }
00499 
00500 StructType *StructType::get(Type *type, ...) {
00501   assert(type && "Cannot create a struct type with no elements with this");
00502   LLVMContext &Ctx = type->getContext();
00503   va_list ap;
00504   SmallVector<llvm::Type*, 8> StructFields;
00505   va_start(ap, type);
00506   while (type) {
00507     StructFields.push_back(type);
00508     type = va_arg(ap, llvm::Type*);
00509   }
00510   auto *Ret = llvm::StructType::get(Ctx, StructFields);
00511   va_end(ap);
00512   return Ret;
00513 }
00514 
00515 StructType *StructType::create(LLVMContext &Context, ArrayRef<Type*> Elements,
00516                                StringRef Name, bool isPacked) {
00517   StructType *ST = create(Context, Name);
00518   ST->setBody(Elements, isPacked);
00519   return ST;
00520 }
00521 
00522 StructType *StructType::create(LLVMContext &Context, ArrayRef<Type*> Elements) {
00523   return create(Context, Elements, StringRef());
00524 }
00525 
00526 StructType *StructType::create(LLVMContext &Context) {
00527   return create(Context, StringRef());
00528 }
00529 
00530 StructType *StructType::create(ArrayRef<Type*> Elements, StringRef Name,
00531                                bool isPacked) {
00532   assert(!Elements.empty() &&
00533          "This method may not be invoked with an empty list");
00534   return create(Elements[0]->getContext(), Elements, Name, isPacked);
00535 }
00536 
00537 StructType *StructType::create(ArrayRef<Type*> Elements) {
00538   assert(!Elements.empty() &&
00539          "This method may not be invoked with an empty list");
00540   return create(Elements[0]->getContext(), Elements, StringRef());
00541 }
00542 
00543 StructType *StructType::create(StringRef Name, Type *type, ...) {
00544   assert(type && "Cannot create a struct type with no elements with this");
00545   LLVMContext &Ctx = type->getContext();
00546   va_list ap;
00547   SmallVector<llvm::Type*, 8> StructFields;
00548   va_start(ap, type);
00549   while (type) {
00550     StructFields.push_back(type);
00551     type = va_arg(ap, llvm::Type*);
00552   }
00553   auto *Ret = llvm::StructType::create(Ctx, StructFields, Name);
00554   va_end(ap);
00555   return Ret;
00556 }
00557 
00558 bool StructType::isSized(SmallPtrSetImpl<const Type*> *Visited) const {
00559   if ((getSubclassData() & SCDB_IsSized) != 0)
00560     return true;
00561   if (isOpaque())
00562     return false;
00563 
00564   if (Visited && !Visited->insert(this).second)
00565     return false;
00566 
00567   // Okay, our struct is sized if all of the elements are, but if one of the
00568   // elements is opaque, the struct isn't sized *yet*, but may become sized in
00569   // the future, so just bail out without caching.
00570   for (element_iterator I = element_begin(), E = element_end(); I != E; ++I)
00571     if (!(*I)->isSized(Visited))
00572       return false;
00573 
00574   // Here we cheat a bit and cast away const-ness. The goal is to memoize when
00575   // we find a sized type, as types can only move from opaque to sized, not the
00576   // other way.
00577   const_cast<StructType*>(this)->setSubclassData(
00578     getSubclassData() | SCDB_IsSized);
00579   return true;
00580 }
00581 
00582 StringRef StructType::getName() const {
00583   assert(!isLiteral() && "Literal structs never have names");
00584   if (!SymbolTableEntry) return StringRef();
00585 
00586   return ((StringMapEntry<StructType*> *)SymbolTableEntry)->getKey();
00587 }
00588 
00589 void StructType::setBody(Type *type, ...) {
00590   assert(type && "Cannot create a struct type with no elements with this");
00591   va_list ap;
00592   SmallVector<llvm::Type*, 8> StructFields;
00593   va_start(ap, type);
00594   while (type) {
00595     StructFields.push_back(type);
00596     type = va_arg(ap, llvm::Type*);
00597   }
00598   setBody(StructFields);
00599   va_end(ap);
00600 }
00601 
00602 bool StructType::isValidElementType(Type *ElemTy) {
00603   return !ElemTy->isVoidTy() && !ElemTy->isLabelTy() &&
00604          !ElemTy->isMetadataTy() && !ElemTy->isFunctionTy();
00605 }
00606 
00607 /// isLayoutIdentical - Return true if this is layout identical to the
00608 /// specified struct.
00609 bool StructType::isLayoutIdentical(StructType *Other) const {
00610   if (this == Other) return true;
00611   
00612   if (isPacked() != Other->isPacked() ||
00613       getNumElements() != Other->getNumElements())
00614     return false;
00615   
00616   return std::equal(element_begin(), element_end(), Other->element_begin());
00617 }
00618 
00619 /// getTypeByName - Return the type with the specified name, or null if there
00620 /// is none by that name.
00621 StructType *Module::getTypeByName(StringRef Name) const {
00622   return getContext().pImpl->NamedStructTypes.lookup(Name);
00623 }
00624 
00625 
00626 //===----------------------------------------------------------------------===//
00627 //                       CompositeType Implementation
00628 //===----------------------------------------------------------------------===//
00629 
00630 Type *CompositeType::getTypeAtIndex(const Value *V) {
00631   if (StructType *STy = dyn_cast<StructType>(this)) {
00632     unsigned Idx =
00633       (unsigned)cast<Constant>(V)->getUniqueInteger().getZExtValue();
00634     assert(indexValid(Idx) && "Invalid structure index!");
00635     return STy->getElementType(Idx);
00636   }
00637 
00638   return cast<SequentialType>(this)->getElementType();
00639 }
00640 Type *CompositeType::getTypeAtIndex(unsigned Idx) {
00641   if (StructType *STy = dyn_cast<StructType>(this)) {
00642     assert(indexValid(Idx) && "Invalid structure index!");
00643     return STy->getElementType(Idx);
00644   }
00645   
00646   return cast<SequentialType>(this)->getElementType();
00647 }
00648 bool CompositeType::indexValid(const Value *V) const {
00649   if (const StructType *STy = dyn_cast<StructType>(this)) {
00650     // Structure indexes require (vectors of) 32-bit integer constants.  In the
00651     // vector case all of the indices must be equal.
00652     if (!V->getType()->getScalarType()->isIntegerTy(32))
00653       return false;
00654     const Constant *C = dyn_cast<Constant>(V);
00655     if (C && V->getType()->isVectorTy())
00656       C = C->getSplatValue();
00657     const ConstantInt *CU = dyn_cast_or_null<ConstantInt>(C);
00658     return CU && CU->getZExtValue() < STy->getNumElements();
00659   }
00660 
00661   // Sequential types can be indexed by any integer.
00662   return V->getType()->isIntOrIntVectorTy();
00663 }
00664 
00665 bool CompositeType::indexValid(unsigned Idx) const {
00666   if (const StructType *STy = dyn_cast<StructType>(this))
00667     return Idx < STy->getNumElements();
00668   // Sequential types can be indexed by any integer.
00669   return true;
00670 }
00671 
00672 
00673 //===----------------------------------------------------------------------===//
00674 //                           ArrayType Implementation
00675 //===----------------------------------------------------------------------===//
00676 
00677 ArrayType::ArrayType(Type *ElType, uint64_t NumEl)
00678   : SequentialType(ArrayTyID, ElType) {
00679   NumElements = NumEl;
00680 }
00681 
00682 ArrayType *ArrayType::get(Type *elementType, uint64_t NumElements) {
00683   Type *ElementType = const_cast<Type*>(elementType);
00684   assert(isValidElementType(ElementType) && "Invalid type for array element!");
00685     
00686   LLVMContextImpl *pImpl = ElementType->getContext().pImpl;
00687   ArrayType *&Entry = 
00688     pImpl->ArrayTypes[std::make_pair(ElementType, NumElements)];
00689 
00690   if (!Entry)
00691     Entry = new (pImpl->TypeAllocator) ArrayType(ElementType, NumElements);
00692   return Entry;
00693 }
00694 
00695 bool ArrayType::isValidElementType(Type *ElemTy) {
00696   return !ElemTy->isVoidTy() && !ElemTy->isLabelTy() &&
00697          !ElemTy->isMetadataTy() && !ElemTy->isFunctionTy();
00698 }
00699 
00700 //===----------------------------------------------------------------------===//
00701 //                          VectorType Implementation
00702 //===----------------------------------------------------------------------===//
00703 
00704 VectorType::VectorType(Type *ElType, unsigned NumEl)
00705   : SequentialType(VectorTyID, ElType) {
00706   NumElements = NumEl;
00707 }
00708 
00709 VectorType *VectorType::get(Type *elementType, unsigned NumElements) {
00710   Type *ElementType = const_cast<Type*>(elementType);
00711   assert(NumElements > 0 && "#Elements of a VectorType must be greater than 0");
00712   assert(isValidElementType(ElementType) && "Element type of a VectorType must "
00713                                             "be an integer, floating point, or "
00714                                             "pointer type.");
00715 
00716   LLVMContextImpl *pImpl = ElementType->getContext().pImpl;
00717   VectorType *&Entry = ElementType->getContext().pImpl
00718     ->VectorTypes[std::make_pair(ElementType, NumElements)];
00719 
00720   if (!Entry)
00721     Entry = new (pImpl->TypeAllocator) VectorType(ElementType, NumElements);
00722   return Entry;
00723 }
00724 
00725 bool VectorType::isValidElementType(Type *ElemTy) {
00726   return ElemTy->isIntegerTy() || ElemTy->isFloatingPointTy() ||
00727     ElemTy->isPointerTy();
00728 }
00729 
00730 //===----------------------------------------------------------------------===//
00731 //                         PointerType Implementation
00732 //===----------------------------------------------------------------------===//
00733 
00734 PointerType *PointerType::get(Type *EltTy, unsigned AddressSpace) {
00735   assert(EltTy && "Can't get a pointer to <null> type!");
00736   assert(isValidElementType(EltTy) && "Invalid type for pointer element!");
00737   
00738   LLVMContextImpl *CImpl = EltTy->getContext().pImpl;
00739   
00740   // Since AddressSpace #0 is the common case, we special case it.
00741   PointerType *&Entry = AddressSpace == 0 ? CImpl->PointerTypes[EltTy]
00742      : CImpl->ASPointerTypes[std::make_pair(EltTy, AddressSpace)];
00743 
00744   if (!Entry)
00745     Entry = new (CImpl->TypeAllocator) PointerType(EltTy, AddressSpace);
00746   return Entry;
00747 }
00748 
00749 
00750 PointerType::PointerType(Type *E, unsigned AddrSpace)
00751   : SequentialType(PointerTyID, E) {
00752 #ifndef NDEBUG
00753   const unsigned oldNCT = NumContainedTys;
00754 #endif
00755   setSubclassData(AddrSpace);
00756   // Check for miscompile. PR11652.
00757   assert(oldNCT == NumContainedTys && "bitfield written out of bounds?");
00758 }
00759 
00760 PointerType *Type::getPointerTo(unsigned addrs) {
00761   return PointerType::get(this, addrs);
00762 }
00763 
00764 bool PointerType::isValidElementType(Type *ElemTy) {
00765   return !ElemTy->isVoidTy() && !ElemTy->isLabelTy() &&
00766          !ElemTy->isMetadataTy();
00767 }
00768 
00769 bool PointerType::isLoadableOrStorableType(Type *ElemTy) {
00770   return isValidElementType(ElemTy) && !ElemTy->isFunctionTy();
00771 }