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