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