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 APInt IntegerType::getMask() const { 279 return APInt::getAllOnesValue(getBitWidth()); 280 } 281 282 //===----------------------------------------------------------------------===// 283 // FunctionType Implementation 284 //===----------------------------------------------------------------------===// 285 286 FunctionType::FunctionType(Type *Result, ArrayRef<Type*> Params, 287 bool IsVarArgs) 288 : Type(Result->getContext(), FunctionTyID) { 289 Type **SubTys = reinterpret_cast<Type**>(this+1); 290 assert(isValidReturnType(Result) && "invalid return type for function"); 291 setSubclassData(IsVarArgs); 292 293 SubTys[0] = Result; 294 295 for (unsigned i = 0, e = Params.size(); i != e; ++i) { 296 assert(isValidArgumentType(Params[i]) && 297 "Not a valid type for function argument!"); 298 SubTys[i+1] = Params[i]; 299 } 300 301 ContainedTys = SubTys; 302 NumContainedTys = Params.size() + 1; // + 1 for result type 303 } 304 305 // This is the factory function for the FunctionType class. 306 FunctionType *FunctionType::get(Type *ReturnType, 307 ArrayRef<Type*> Params, bool isVarArg) { 308 LLVMContextImpl *pImpl = ReturnType->getContext().pImpl; 309 const FunctionTypeKeyInfo::KeyTy Key(ReturnType, Params, isVarArg); 310 FunctionType *FT; 311 // Since we only want to allocate a fresh function type in case none is found 312 // and we don't want to perform two lookups (one for checking if existent and 313 // one for inserting the newly allocated one), here we instead lookup based on 314 // Key and update the reference to the function type in-place to a newly 315 // allocated one if not found. 316 auto Insertion = pImpl->FunctionTypes.insert_as(nullptr, Key); 317 if (Insertion.second) { 318 // The function type was not found. Allocate one and update FunctionTypes 319 // in-place. 320 FT = (FunctionType *)pImpl->Alloc.Allocate( 321 sizeof(FunctionType) + sizeof(Type *) * (Params.size() + 1), 322 alignof(FunctionType)); 323 new (FT) FunctionType(ReturnType, Params, isVarArg); 324 *Insertion.first = FT; 325 } else { 326 // The function type was found. Just return it. 327 FT = *Insertion.first; 328 } 329 return FT; 330 } 331 332 FunctionType *FunctionType::get(Type *Result, bool isVarArg) { 333 return get(Result, None, isVarArg); 334 } 335 336 bool FunctionType::isValidReturnType(Type *RetTy) { 337 return !RetTy->isFunctionTy() && !RetTy->isLabelTy() && 338 !RetTy->isMetadataTy(); 339 } 340 341 bool FunctionType::isValidArgumentType(Type *ArgTy) { 342 return ArgTy->isFirstClassType(); 343 } 344 345 //===----------------------------------------------------------------------===// 346 // StructType Implementation 347 //===----------------------------------------------------------------------===// 348 349 // Primitive Constructors. 350 351 StructType *StructType::get(LLVMContext &Context, ArrayRef<Type*> ETypes, 352 bool isPacked) { 353 LLVMContextImpl *pImpl = Context.pImpl; 354 const AnonStructTypeKeyInfo::KeyTy Key(ETypes, isPacked); 355 356 StructType *ST; 357 // Since we only want to allocate a fresh struct type in case none is found 358 // and we don't want to perform two lookups (one for checking if existent and 359 // one for inserting the newly allocated one), here we instead lookup based on 360 // Key and update the reference to the struct type in-place to a newly 361 // allocated one if not found. 362 auto Insertion = pImpl->AnonStructTypes.insert_as(nullptr, Key); 363 if (Insertion.second) { 364 // The struct type was not found. Allocate one and update AnonStructTypes 365 // in-place. 366 ST = new (Context.pImpl->Alloc) StructType(Context); 367 ST->setSubclassData(SCDB_IsLiteral); // Literal struct. 368 ST->setBody(ETypes, isPacked); 369 *Insertion.first = ST; 370 } else { 371 // The struct type was found. Just return it. 372 ST = *Insertion.first; 373 } 374 375 return ST; 376 } 377 378 void StructType::setBody(ArrayRef<Type*> Elements, bool isPacked) { 379 assert(isOpaque() && "Struct body already set!"); 380 381 setSubclassData(getSubclassData() | SCDB_HasBody); 382 if (isPacked) 383 setSubclassData(getSubclassData() | SCDB_Packed); 384 385 NumContainedTys = Elements.size(); 386 387 if (Elements.empty()) { 388 ContainedTys = nullptr; 389 return; 390 } 391 392 ContainedTys = Elements.copy(getContext().pImpl->Alloc).data(); 393 } 394 395 void StructType::setName(StringRef Name) { 396 if (Name == getName()) return; 397 398 StringMap<StructType *> &SymbolTable = getContext().pImpl->NamedStructTypes; 399 400 using EntryTy = StringMap<StructType *>::MapEntryTy; 401 402 // If this struct already had a name, remove its symbol table entry. Don't 403 // delete the data yet because it may be part of the new name. 404 if (SymbolTableEntry) 405 SymbolTable.remove((EntryTy *)SymbolTableEntry); 406 407 // If this is just removing the name, we're done. 408 if (Name.empty()) { 409 if (SymbolTableEntry) { 410 // Delete the old string data. 411 ((EntryTy *)SymbolTableEntry)->Destroy(SymbolTable.getAllocator()); 412 SymbolTableEntry = nullptr; 413 } 414 return; 415 } 416 417 // Look up the entry for the name. 418 auto IterBool = 419 getContext().pImpl->NamedStructTypes.insert(std::make_pair(Name, this)); 420 421 // While we have a name collision, try a random rename. 422 if (!IterBool.second) { 423 SmallString<64> TempStr(Name); 424 TempStr.push_back('.'); 425 raw_svector_ostream TmpStream(TempStr); 426 unsigned NameSize = Name.size(); 427 428 do { 429 TempStr.resize(NameSize + 1); 430 TmpStream << getContext().pImpl->NamedStructTypesUniqueID++; 431 432 IterBool = getContext().pImpl->NamedStructTypes.insert( 433 std::make_pair(TmpStream.str(), this)); 434 } while (!IterBool.second); 435 } 436 437 // Delete the old string data. 438 if (SymbolTableEntry) 439 ((EntryTy *)SymbolTableEntry)->Destroy(SymbolTable.getAllocator()); 440 SymbolTableEntry = &*IterBool.first; 441 } 442 443 //===----------------------------------------------------------------------===// 444 // StructType Helper functions. 445 446 StructType *StructType::create(LLVMContext &Context, StringRef Name) { 447 StructType *ST = new (Context.pImpl->Alloc) StructType(Context); 448 if (!Name.empty()) 449 ST->setName(Name); 450 return ST; 451 } 452 453 StructType *StructType::get(LLVMContext &Context, bool isPacked) { 454 return get(Context, None, isPacked); 455 } 456 457 StructType *StructType::create(LLVMContext &Context, ArrayRef<Type*> Elements, 458 StringRef Name, bool isPacked) { 459 StructType *ST = create(Context, Name); 460 ST->setBody(Elements, isPacked); 461 return ST; 462 } 463 464 StructType *StructType::create(LLVMContext &Context, ArrayRef<Type*> Elements) { 465 return create(Context, Elements, StringRef()); 466 } 467 468 StructType *StructType::create(LLVMContext &Context) { 469 return create(Context, StringRef()); 470 } 471 472 StructType *StructType::create(ArrayRef<Type*> Elements, StringRef Name, 473 bool isPacked) { 474 assert(!Elements.empty() && 475 "This method may not be invoked with an empty list"); 476 return create(Elements[0]->getContext(), Elements, Name, isPacked); 477 } 478 479 StructType *StructType::create(ArrayRef<Type*> Elements) { 480 assert(!Elements.empty() && 481 "This method may not be invoked with an empty list"); 482 return create(Elements[0]->getContext(), Elements, StringRef()); 483 } 484 485 bool StructType::isSized(SmallPtrSetImpl<Type*> *Visited) const { 486 if ((getSubclassData() & SCDB_IsSized) != 0) 487 return true; 488 if (isOpaque()) 489 return false; 490 491 if (Visited && !Visited->insert(const_cast<StructType*>(this)).second) 492 return false; 493 494 // Okay, our struct is sized if all of the elements are, but if one of the 495 // elements is opaque, the struct isn't sized *yet*, but may become sized in 496 // the future, so just bail out without caching. 497 for (element_iterator I = element_begin(), E = element_end(); I != E; ++I) 498 if (!(*I)->isSized(Visited)) 499 return false; 500 501 // Here we cheat a bit and cast away const-ness. The goal is to memoize when 502 // we find a sized type, as types can only move from opaque to sized, not the 503 // other way. 504 const_cast<StructType*>(this)->setSubclassData( 505 getSubclassData() | SCDB_IsSized); 506 return true; 507 } 508 509 StringRef StructType::getName() const { 510 assert(!isLiteral() && "Literal structs never have names"); 511 if (!SymbolTableEntry) return StringRef(); 512 513 return ((StringMapEntry<StructType*> *)SymbolTableEntry)->getKey(); 514 } 515 516 bool StructType::isValidElementType(Type *ElemTy) { 517 return !ElemTy->isVoidTy() && !ElemTy->isLabelTy() && 518 !ElemTy->isMetadataTy() && !ElemTy->isFunctionTy() && 519 !ElemTy->isTokenTy() && !isa<ScalableVectorType>(ElemTy); 520 } 521 522 bool StructType::isLayoutIdentical(StructType *Other) const { 523 if (this == Other) return true; 524 525 if (isPacked() != Other->isPacked()) 526 return false; 527 528 return elements() == Other->elements(); 529 } 530 531 Type *StructType::getTypeAtIndex(const Value *V) const { 532 unsigned Idx = (unsigned)cast<Constant>(V)->getUniqueInteger().getZExtValue(); 533 assert(indexValid(Idx) && "Invalid structure index!"); 534 return getElementType(Idx); 535 } 536 537 bool StructType::indexValid(const Value *V) const { 538 // Structure indexes require (vectors of) 32-bit integer constants. In the 539 // vector case all of the indices must be equal. 540 if (!V->getType()->isIntOrIntVectorTy(32)) 541 return false; 542 if (isa<ScalableVectorType>(V->getType())) 543 return false; 544 const Constant *C = dyn_cast<Constant>(V); 545 if (C && V->getType()->isVectorTy()) 546 C = C->getSplatValue(); 547 const ConstantInt *CU = dyn_cast_or_null<ConstantInt>(C); 548 return CU && CU->getZExtValue() < getNumElements(); 549 } 550 551 StructType *StructType::getTypeByName(LLVMContext &C, StringRef Name) { 552 return C.pImpl->NamedStructTypes.lookup(Name); 553 } 554 555 //===----------------------------------------------------------------------===// 556 // ArrayType Implementation 557 //===----------------------------------------------------------------------===// 558 559 ArrayType::ArrayType(Type *ElType, uint64_t NumEl) 560 : Type(ElType->getContext(), ArrayTyID), ContainedType(ElType), 561 NumElements(NumEl) { 562 ContainedTys = &ContainedType; 563 NumContainedTys = 1; 564 } 565 566 ArrayType *ArrayType::get(Type *ElementType, uint64_t NumElements) { 567 assert(isValidElementType(ElementType) && "Invalid type for array element!"); 568 569 LLVMContextImpl *pImpl = ElementType->getContext().pImpl; 570 ArrayType *&Entry = 571 pImpl->ArrayTypes[std::make_pair(ElementType, NumElements)]; 572 573 if (!Entry) 574 Entry = new (pImpl->Alloc) ArrayType(ElementType, NumElements); 575 return Entry; 576 } 577 578 bool ArrayType::isValidElementType(Type *ElemTy) { 579 return !ElemTy->isVoidTy() && !ElemTy->isLabelTy() && 580 !ElemTy->isMetadataTy() && !ElemTy->isFunctionTy() && 581 !ElemTy->isTokenTy() && !isa<ScalableVectorType>(ElemTy); 582 } 583 584 //===----------------------------------------------------------------------===// 585 // VectorType Implementation 586 //===----------------------------------------------------------------------===// 587 588 VectorType::VectorType(Type *ElType, unsigned EQ, Type::TypeID TID) 589 : Type(ElType->getContext(), TID), ContainedType(ElType), 590 ElementQuantity(EQ) { 591 ContainedTys = &ContainedType; 592 NumContainedTys = 1; 593 } 594 595 VectorType *VectorType::get(Type *ElementType, ElementCount EC) { 596 if (EC.isScalable()) 597 return ScalableVectorType::get(ElementType, EC.getKnownMinValue()); 598 else 599 return FixedVectorType::get(ElementType, EC.getKnownMinValue()); 600 } 601 602 bool VectorType::isValidElementType(Type *ElemTy) { 603 return ElemTy->isIntegerTy() || ElemTy->isFloatingPointTy() || 604 ElemTy->isPointerTy(); 605 } 606 607 //===----------------------------------------------------------------------===// 608 // FixedVectorType Implementation 609 //===----------------------------------------------------------------------===// 610 611 FixedVectorType *FixedVectorType::get(Type *ElementType, unsigned NumElts) { 612 assert(NumElts > 0 && "#Elements of a VectorType must be greater than 0"); 613 assert(isValidElementType(ElementType) && "Element type of a VectorType must " 614 "be an integer, floating point, or " 615 "pointer type."); 616 617 auto EC = ElementCount::getFixed(NumElts); 618 619 LLVMContextImpl *pImpl = ElementType->getContext().pImpl; 620 VectorType *&Entry = ElementType->getContext() 621 .pImpl->VectorTypes[std::make_pair(ElementType, EC)]; 622 623 if (!Entry) 624 Entry = new (pImpl->Alloc) FixedVectorType(ElementType, NumElts); 625 return cast<FixedVectorType>(Entry); 626 } 627 628 //===----------------------------------------------------------------------===// 629 // ScalableVectorType Implementation 630 //===----------------------------------------------------------------------===// 631 632 ScalableVectorType *ScalableVectorType::get(Type *ElementType, 633 unsigned MinNumElts) { 634 assert(MinNumElts > 0 && "#Elements of a VectorType must be greater than 0"); 635 assert(isValidElementType(ElementType) && "Element type of a VectorType must " 636 "be an integer, floating point, or " 637 "pointer type."); 638 639 auto EC = ElementCount::getScalable(MinNumElts); 640 641 LLVMContextImpl *pImpl = ElementType->getContext().pImpl; 642 VectorType *&Entry = ElementType->getContext() 643 .pImpl->VectorTypes[std::make_pair(ElementType, EC)]; 644 645 if (!Entry) 646 Entry = new (pImpl->Alloc) ScalableVectorType(ElementType, MinNumElts); 647 return cast<ScalableVectorType>(Entry); 648 } 649 650 //===----------------------------------------------------------------------===// 651 // PointerType Implementation 652 //===----------------------------------------------------------------------===// 653 654 PointerType *PointerType::get(Type *EltTy, unsigned AddressSpace) { 655 assert(EltTy && "Can't get a pointer to <null> type!"); 656 assert(isValidElementType(EltTy) && "Invalid type for pointer element!"); 657 658 LLVMContextImpl *CImpl = EltTy->getContext().pImpl; 659 660 // Since AddressSpace #0 is the common case, we special case it. 661 PointerType *&Entry = AddressSpace == 0 ? CImpl->PointerTypes[EltTy] 662 : CImpl->ASPointerTypes[std::make_pair(EltTy, AddressSpace)]; 663 664 if (!Entry) 665 Entry = new (CImpl->Alloc) PointerType(EltTy, AddressSpace); 666 return Entry; 667 } 668 669 PointerType::PointerType(Type *E, unsigned AddrSpace) 670 : Type(E->getContext(), PointerTyID), PointeeTy(E) { 671 ContainedTys = &PointeeTy; 672 NumContainedTys = 1; 673 setSubclassData(AddrSpace); 674 } 675 676 PointerType *Type::getPointerTo(unsigned addrs) const { 677 return PointerType::get(const_cast<Type*>(this), addrs); 678 } 679 680 bool PointerType::isValidElementType(Type *ElemTy) { 681 return !ElemTy->isVoidTy() && !ElemTy->isLabelTy() && 682 !ElemTy->isMetadataTy() && !ElemTy->isTokenTy(); 683 } 684 685 bool PointerType::isLoadableOrStorableType(Type *ElemTy) { 686 return isValidElementType(ElemTy) && !ElemTy->isFunctionTy(); 687 } 688