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