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