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