LLVM API Documentation

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