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