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