LCOV - code coverage report
Current view: top level - lib/IR - Type.cpp (source / functions) Hit Total Coverage
Test: llvm-toolchain.info Lines: 265 304 87.2 %
Date: 2017-09-14 15:23:50 Functions: 67 77 87.0 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.13