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