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