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