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 auto *Ret = llvm::StructType::get(Ctx, StructFields); 510 va_end(ap); 511 return Ret; 512 } 513 514 StructType *StructType::create(LLVMContext &Context, ArrayRef<Type*> Elements, 515 StringRef Name, bool isPacked) { 516 StructType *ST = create(Context, Name); 517 ST->setBody(Elements, isPacked); 518 return ST; 519 } 520 521 StructType *StructType::create(LLVMContext &Context, ArrayRef<Type*> Elements) { 522 return create(Context, Elements, StringRef()); 523 } 524 525 StructType *StructType::create(LLVMContext &Context) { 526 return create(Context, StringRef()); 527 } 528 529 StructType *StructType::create(ArrayRef<Type*> Elements, StringRef Name, 530 bool isPacked) { 531 assert(!Elements.empty() && 532 "This method may not be invoked with an empty list"); 533 return create(Elements[0]->getContext(), Elements, Name, isPacked); 534 } 535 536 StructType *StructType::create(ArrayRef<Type*> Elements) { 537 assert(!Elements.empty() && 538 "This method may not be invoked with an empty list"); 539 return create(Elements[0]->getContext(), Elements, StringRef()); 540 } 541 542 StructType *StructType::create(StringRef Name, Type *type, ...) { 543 assert(type && "Cannot create a struct type with no elements with this"); 544 LLVMContext &Ctx = type->getContext(); 545 va_list ap; 546 SmallVector<llvm::Type*, 8> StructFields; 547 va_start(ap, type); 548 while (type) { 549 StructFields.push_back(type); 550 type = va_arg(ap, llvm::Type*); 551 } 552 auto *Ret = llvm::StructType::create(Ctx, StructFields, Name); 553 va_end(ap); 554 return Ret; 555 } 556 557 bool StructType::isSized(SmallPtrSet<const Type*, 4> *Visited) const { 558 if ((getSubclassData() & SCDB_IsSized) != 0) 559 return true; 560 if (isOpaque()) 561 return false; 562 563 if (Visited && !Visited->insert(this)) 564 return false; 565 566 // Okay, our struct is sized if all of the elements are, but if one of the 567 // elements is opaque, the struct isn't sized *yet*, but may become sized in 568 // the future, so just bail out without caching. 569 for (element_iterator I = element_begin(), E = element_end(); I != E; ++I) 570 if (!(*I)->isSized(Visited)) 571 return false; 572 573 // Here we cheat a bit and cast away const-ness. The goal is to memoize when 574 // we find a sized type, as types can only move from opaque to sized, not the 575 // other way. 576 const_cast<StructType*>(this)->setSubclassData( 577 getSubclassData() | SCDB_IsSized); 578 return true; 579 } 580 581 StringRef StructType::getName() const { 582 assert(!isLiteral() && "Literal structs never have names"); 583 if (!SymbolTableEntry) return StringRef(); 584 585 return ((StringMapEntry<StructType*> *)SymbolTableEntry)->getKey(); 586 } 587 588 void StructType::setBody(Type *type, ...) { 589 assert(type && "Cannot create a struct type with no elements with this"); 590 va_list ap; 591 SmallVector<llvm::Type*, 8> StructFields; 592 va_start(ap, type); 593 while (type) { 594 StructFields.push_back(type); 595 type = va_arg(ap, llvm::Type*); 596 } 597 setBody(StructFields); 598 va_end(ap); 599 } 600 601 bool StructType::isValidElementType(Type *ElemTy) { 602 return !ElemTy->isVoidTy() && !ElemTy->isLabelTy() && 603 !ElemTy->isMetadataTy() && !ElemTy->isFunctionTy(); 604 } 605 606 /// isLayoutIdentical - Return true if this is layout identical to the 607 /// specified struct. 608 bool StructType::isLayoutIdentical(StructType *Other) const { 609 if (this == Other) return true; 610 611 if (isPacked() != Other->isPacked() || 612 getNumElements() != Other->getNumElements()) 613 return false; 614 615 return std::equal(element_begin(), element_end(), Other->element_begin()); 616 } 617 618 /// getTypeByName - Return the type with the specified name, or null if there 619 /// is none by that name. 620 StructType *Module::getTypeByName(StringRef Name) const { 621 return getContext().pImpl->NamedStructTypes.lookup(Name); 622 } 623 624 625 //===----------------------------------------------------------------------===// 626 // CompositeType Implementation 627 //===----------------------------------------------------------------------===// 628 629 Type *CompositeType::getTypeAtIndex(const Value *V) { 630 if (StructType *STy = dyn_cast<StructType>(this)) { 631 unsigned Idx = 632 (unsigned)cast<Constant>(V)->getUniqueInteger().getZExtValue(); 633 assert(indexValid(Idx) && "Invalid structure index!"); 634 return STy->getElementType(Idx); 635 } 636 637 return cast<SequentialType>(this)->getElementType(); 638 } 639 Type *CompositeType::getTypeAtIndex(unsigned Idx) { 640 if (StructType *STy = dyn_cast<StructType>(this)) { 641 assert(indexValid(Idx) && "Invalid structure index!"); 642 return STy->getElementType(Idx); 643 } 644 645 return cast<SequentialType>(this)->getElementType(); 646 } 647 bool CompositeType::indexValid(const Value *V) const { 648 if (const StructType *STy = dyn_cast<StructType>(this)) { 649 // Structure indexes require (vectors of) 32-bit integer constants. In the 650 // vector case all of the indices must be equal. 651 if (!V->getType()->getScalarType()->isIntegerTy(32)) 652 return false; 653 const Constant *C = dyn_cast<Constant>(V); 654 if (C && V->getType()->isVectorTy()) 655 C = C->getSplatValue(); 656 const ConstantInt *CU = dyn_cast_or_null<ConstantInt>(C); 657 return CU && CU->getZExtValue() < STy->getNumElements(); 658 } 659 660 // Sequential types can be indexed by any integer. 661 return V->getType()->isIntOrIntVectorTy(); 662 } 663 664 bool CompositeType::indexValid(unsigned Idx) const { 665 if (const StructType *STy = dyn_cast<StructType>(this)) 666 return Idx < STy->getNumElements(); 667 // Sequential types can be indexed by any integer. 668 return true; 669 } 670 671 672 //===----------------------------------------------------------------------===// 673 // ArrayType Implementation 674 //===----------------------------------------------------------------------===// 675 676 ArrayType::ArrayType(Type *ElType, uint64_t NumEl) 677 : SequentialType(ArrayTyID, ElType) { 678 NumElements = NumEl; 679 } 680 681 ArrayType *ArrayType::get(Type *elementType, uint64_t NumElements) { 682 Type *ElementType = const_cast<Type*>(elementType); 683 assert(isValidElementType(ElementType) && "Invalid type for array element!"); 684 685 LLVMContextImpl *pImpl = ElementType->getContext().pImpl; 686 ArrayType *&Entry = 687 pImpl->ArrayTypes[std::make_pair(ElementType, NumElements)]; 688 689 if (!Entry) 690 Entry = new (pImpl->TypeAllocator) ArrayType(ElementType, NumElements); 691 return Entry; 692 } 693 694 bool ArrayType::isValidElementType(Type *ElemTy) { 695 return !ElemTy->isVoidTy() && !ElemTy->isLabelTy() && 696 !ElemTy->isMetadataTy() && !ElemTy->isFunctionTy(); 697 } 698 699 //===----------------------------------------------------------------------===// 700 // VectorType Implementation 701 //===----------------------------------------------------------------------===// 702 703 VectorType::VectorType(Type *ElType, unsigned NumEl) 704 : SequentialType(VectorTyID, ElType) { 705 NumElements = NumEl; 706 } 707 708 VectorType *VectorType::get(Type *elementType, unsigned NumElements) { 709 Type *ElementType = const_cast<Type*>(elementType); 710 assert(NumElements > 0 && "#Elements of a VectorType must be greater than 0"); 711 assert(isValidElementType(ElementType) && 712 "Elements of a VectorType must be a primitive type"); 713 714 LLVMContextImpl *pImpl = ElementType->getContext().pImpl; 715 VectorType *&Entry = ElementType->getContext().pImpl 716 ->VectorTypes[std::make_pair(ElementType, NumElements)]; 717 718 if (!Entry) 719 Entry = new (pImpl->TypeAllocator) VectorType(ElementType, NumElements); 720 return Entry; 721 } 722 723 bool VectorType::isValidElementType(Type *ElemTy) { 724 return ElemTy->isIntegerTy() || ElemTy->isFloatingPointTy() || 725 ElemTy->isPointerTy(); 726 } 727 728 //===----------------------------------------------------------------------===// 729 // PointerType Implementation 730 //===----------------------------------------------------------------------===// 731 732 PointerType *PointerType::get(Type *EltTy, unsigned AddressSpace) { 733 assert(EltTy && "Can't get a pointer to <null> type!"); 734 assert(isValidElementType(EltTy) && "Invalid type for pointer element!"); 735 736 LLVMContextImpl *CImpl = EltTy->getContext().pImpl; 737 738 // Since AddressSpace #0 is the common case, we special case it. 739 PointerType *&Entry = AddressSpace == 0 ? CImpl->PointerTypes[EltTy] 740 : CImpl->ASPointerTypes[std::make_pair(EltTy, AddressSpace)]; 741 742 if (!Entry) 743 Entry = new (CImpl->TypeAllocator) PointerType(EltTy, AddressSpace); 744 return Entry; 745 } 746 747 748 PointerType::PointerType(Type *E, unsigned AddrSpace) 749 : SequentialType(PointerTyID, E) { 750 #ifndef NDEBUG 751 const unsigned oldNCT = NumContainedTys; 752 #endif 753 setSubclassData(AddrSpace); 754 // Check for miscompile. PR11652. 755 assert(oldNCT == NumContainedTys && "bitfield written out of bounds?"); 756 } 757 758 PointerType *Type::getPointerTo(unsigned addrs) { 759 return PointerType::get(this, addrs); 760 } 761 762 bool PointerType::isValidElementType(Type *ElemTy) { 763 return !ElemTy->isVoidTy() && !ElemTy->isLabelTy() && 764 !ElemTy->isMetadataTy(); 765 } 766