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