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