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