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