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 // Primitive 'Type' data 167 //===----------------------------------------------------------------------===// 168 169 Type *Type::getVoidTy(LLVMContext &C) { return &C.pImpl->VoidTy; } 170 Type *Type::getLabelTy(LLVMContext &C) { return &C.pImpl->LabelTy; } 171 Type *Type::getHalfTy(LLVMContext &C) { return &C.pImpl->HalfTy; } 172 Type *Type::getFloatTy(LLVMContext &C) { return &C.pImpl->FloatTy; } 173 Type *Type::getDoubleTy(LLVMContext &C) { return &C.pImpl->DoubleTy; } 174 Type *Type::getMetadataTy(LLVMContext &C) { return &C.pImpl->MetadataTy; } 175 Type *Type::getTokenTy(LLVMContext &C) { return &C.pImpl->TokenTy; } 176 Type *Type::getX86_FP80Ty(LLVMContext &C) { return &C.pImpl->X86_FP80Ty; } 177 Type *Type::getFP128Ty(LLVMContext &C) { return &C.pImpl->FP128Ty; } 178 Type *Type::getPPC_FP128Ty(LLVMContext &C) { return &C.pImpl->PPC_FP128Ty; } 179 Type *Type::getX86_MMXTy(LLVMContext &C) { return &C.pImpl->X86_MMXTy; } 180 181 IntegerType *Type::getInt1Ty(LLVMContext &C) { return &C.pImpl->Int1Ty; } 182 IntegerType *Type::getInt8Ty(LLVMContext &C) { return &C.pImpl->Int8Ty; } 183 IntegerType *Type::getInt16Ty(LLVMContext &C) { return &C.pImpl->Int16Ty; } 184 IntegerType *Type::getInt32Ty(LLVMContext &C) { return &C.pImpl->Int32Ty; } 185 IntegerType *Type::getInt64Ty(LLVMContext &C) { return &C.pImpl->Int64Ty; } 186 IntegerType *Type::getInt128Ty(LLVMContext &C) { return &C.pImpl->Int128Ty; } 187 188 IntegerType *Type::getIntNTy(LLVMContext &C, unsigned N) { 189 return IntegerType::get(C, N); 190 } 191 192 PointerType *Type::getHalfPtrTy(LLVMContext &C, unsigned AS) { 193 return getHalfTy(C)->getPointerTo(AS); 194 } 195 196 PointerType *Type::getFloatPtrTy(LLVMContext &C, unsigned AS) { 197 return getFloatTy(C)->getPointerTo(AS); 198 } 199 200 PointerType *Type::getDoublePtrTy(LLVMContext &C, unsigned AS) { 201 return getDoubleTy(C)->getPointerTo(AS); 202 } 203 204 PointerType *Type::getX86_FP80PtrTy(LLVMContext &C, unsigned AS) { 205 return getX86_FP80Ty(C)->getPointerTo(AS); 206 } 207 208 PointerType *Type::getFP128PtrTy(LLVMContext &C, unsigned AS) { 209 return getFP128Ty(C)->getPointerTo(AS); 210 } 211 212 PointerType *Type::getPPC_FP128PtrTy(LLVMContext &C, unsigned AS) { 213 return getPPC_FP128Ty(C)->getPointerTo(AS); 214 } 215 216 PointerType *Type::getX86_MMXPtrTy(LLVMContext &C, unsigned AS) { 217 return getX86_MMXTy(C)->getPointerTo(AS); 218 } 219 220 PointerType *Type::getIntNPtrTy(LLVMContext &C, unsigned N, unsigned AS) { 221 return getIntNTy(C, N)->getPointerTo(AS); 222 } 223 224 PointerType *Type::getInt1PtrTy(LLVMContext &C, unsigned AS) { 225 return getInt1Ty(C)->getPointerTo(AS); 226 } 227 228 PointerType *Type::getInt8PtrTy(LLVMContext &C, unsigned AS) { 229 return getInt8Ty(C)->getPointerTo(AS); 230 } 231 232 PointerType *Type::getInt16PtrTy(LLVMContext &C, unsigned AS) { 233 return getInt16Ty(C)->getPointerTo(AS); 234 } 235 236 PointerType *Type::getInt32PtrTy(LLVMContext &C, unsigned AS) { 237 return getInt32Ty(C)->getPointerTo(AS); 238 } 239 240 PointerType *Type::getInt64PtrTy(LLVMContext &C, unsigned AS) { 241 return getInt64Ty(C)->getPointerTo(AS); 242 } 243 244 245 //===----------------------------------------------------------------------===// 246 // IntegerType Implementation 247 //===----------------------------------------------------------------------===// 248 249 IntegerType *IntegerType::get(LLVMContext &C, unsigned NumBits) { 250 assert(NumBits >= MIN_INT_BITS && "bitwidth too small"); 251 assert(NumBits <= MAX_INT_BITS && "bitwidth too large"); 252 253 // Check for the built-in integer types 254 switch (NumBits) { 255 case 1: return cast<IntegerType>(Type::getInt1Ty(C)); 256 case 8: return cast<IntegerType>(Type::getInt8Ty(C)); 257 case 16: return cast<IntegerType>(Type::getInt16Ty(C)); 258 case 32: return cast<IntegerType>(Type::getInt32Ty(C)); 259 case 64: return cast<IntegerType>(Type::getInt64Ty(C)); 260 case 128: return cast<IntegerType>(Type::getInt128Ty(C)); 261 default: 262 break; 263 } 264 265 IntegerType *&Entry = C.pImpl->IntegerTypes[NumBits]; 266 267 if (!Entry) 268 Entry = new (C.pImpl->TypeAllocator) IntegerType(C, NumBits); 269 270 return Entry; 271 } 272 273 bool IntegerType::isPowerOf2ByteWidth() const { 274 unsigned BitWidth = getBitWidth(); 275 return (BitWidth > 7) && isPowerOf2_32(BitWidth); 276 } 277 278 APInt IntegerType::getMask() const { 279 return APInt::getAllOnesValue(getBitWidth()); 280 } 281 282 //===----------------------------------------------------------------------===// 283 // FunctionType Implementation 284 //===----------------------------------------------------------------------===// 285 286 FunctionType::FunctionType(Type *Result, ArrayRef<Type*> Params, 287 bool IsVarArgs) 288 : Type(Result->getContext(), FunctionTyID) { 289 Type **SubTys = reinterpret_cast<Type**>(this+1); 290 assert(isValidReturnType(Result) && "invalid return type for function"); 291 setSubclassData(IsVarArgs); 292 293 SubTys[0] = Result; 294 295 for (unsigned i = 0, e = Params.size(); i != e; ++i) { 296 assert(isValidArgumentType(Params[i]) && 297 "Not a valid type for function argument!"); 298 SubTys[i+1] = Params[i]; 299 } 300 301 ContainedTys = SubTys; 302 NumContainedTys = Params.size() + 1; // + 1 for result type 303 } 304 305 // FunctionType::get - The factory function for the FunctionType class. 306 FunctionType *FunctionType::get(Type *ReturnType, 307 ArrayRef<Type*> Params, bool isVarArg) { 308 LLVMContextImpl *pImpl = ReturnType->getContext().pImpl; 309 FunctionTypeKeyInfo::KeyTy Key(ReturnType, Params, isVarArg); 310 auto I = pImpl->FunctionTypes.find_as(Key); 311 FunctionType *FT; 312 313 if (I == pImpl->FunctionTypes.end()) { 314 FT = (FunctionType*) pImpl->TypeAllocator. 315 Allocate(sizeof(FunctionType) + sizeof(Type*) * (Params.size() + 1), 316 AlignOf<FunctionType>::Alignment); 317 new (FT) FunctionType(ReturnType, Params, isVarArg); 318 pImpl->FunctionTypes.insert(FT); 319 } else { 320 FT = *I; 321 } 322 323 return FT; 324 } 325 326 FunctionType *FunctionType::get(Type *Result, bool isVarArg) { 327 return get(Result, None, isVarArg); 328 } 329 330 /// isValidReturnType - Return true if the specified type is valid as a return 331 /// type. 332 bool FunctionType::isValidReturnType(Type *RetTy) { 333 return !RetTy->isFunctionTy() && !RetTy->isLabelTy() && 334 !RetTy->isMetadataTy(); 335 } 336 337 /// isValidArgumentType - Return true if the specified type is valid as an 338 /// argument type. 339 bool FunctionType::isValidArgumentType(Type *ArgTy) { 340 return ArgTy->isFirstClassType(); 341 } 342 343 //===----------------------------------------------------------------------===// 344 // StructType Implementation 345 //===----------------------------------------------------------------------===// 346 347 // Primitive Constructors. 348 349 StructType *StructType::get(LLVMContext &Context, ArrayRef<Type*> ETypes, 350 bool isPacked) { 351 LLVMContextImpl *pImpl = Context.pImpl; 352 AnonStructTypeKeyInfo::KeyTy Key(ETypes, isPacked); 353 auto I = pImpl->AnonStructTypes.find_as(Key); 354 StructType *ST; 355 356 if (I == pImpl->AnonStructTypes.end()) { 357 // Value not found. Create a new type! 358 ST = new (Context.pImpl->TypeAllocator) StructType(Context); 359 ST->setSubclassData(SCDB_IsLiteral); // Literal struct. 360 ST->setBody(ETypes, isPacked); 361 Context.pImpl->AnonStructTypes.insert(ST); 362 } else { 363 ST = *I; 364 } 365 366 return ST; 367 } 368 369 void StructType::setBody(ArrayRef<Type*> Elements, bool isPacked) { 370 assert(isOpaque() && "Struct body already set!"); 371 372 setSubclassData(getSubclassData() | SCDB_HasBody); 373 if (isPacked) 374 setSubclassData(getSubclassData() | SCDB_Packed); 375 376 NumContainedTys = Elements.size(); 377 378 if (Elements.empty()) { 379 ContainedTys = nullptr; 380 return; 381 } 382 383 ContainedTys = Elements.copy(getContext().pImpl->TypeAllocator).data(); 384 } 385 386 void StructType::setName(StringRef Name) { 387 if (Name == getName()) return; 388 389 StringMap<StructType *> &SymbolTable = getContext().pImpl->NamedStructTypes; 390 typedef StringMap<StructType *>::MapEntryTy EntryTy; 391 392 // If this struct already had a name, remove its symbol table entry. Don't 393 // delete the data yet because it may be part of the new name. 394 if (SymbolTableEntry) 395 SymbolTable.remove((EntryTy *)SymbolTableEntry); 396 397 // If this is just removing the name, we're done. 398 if (Name.empty()) { 399 if (SymbolTableEntry) { 400 // Delete the old string data. 401 ((EntryTy *)SymbolTableEntry)->Destroy(SymbolTable.getAllocator()); 402 SymbolTableEntry = nullptr; 403 } 404 return; 405 } 406 407 // Look up the entry for the name. 408 auto IterBool = 409 getContext().pImpl->NamedStructTypes.insert(std::make_pair(Name, this)); 410 411 // While we have a name collision, try a random rename. 412 if (!IterBool.second) { 413 SmallString<64> TempStr(Name); 414 TempStr.push_back('.'); 415 raw_svector_ostream TmpStream(TempStr); 416 unsigned NameSize = Name.size(); 417 418 do { 419 TempStr.resize(NameSize + 1); 420 TmpStream << getContext().pImpl->NamedStructTypesUniqueID++; 421 422 IterBool = getContext().pImpl->NamedStructTypes.insert( 423 std::make_pair(TmpStream.str(), this)); 424 } while (!IterBool.second); 425 } 426 427 // Delete the old string data. 428 if (SymbolTableEntry) 429 ((EntryTy *)SymbolTableEntry)->Destroy(SymbolTable.getAllocator()); 430 SymbolTableEntry = &*IterBool.first; 431 } 432 433 //===----------------------------------------------------------------------===// 434 // StructType Helper functions. 435 436 StructType *StructType::create(LLVMContext &Context, StringRef Name) { 437 StructType *ST = new (Context.pImpl->TypeAllocator) StructType(Context); 438 if (!Name.empty()) 439 ST->setName(Name); 440 return ST; 441 } 442 443 StructType *StructType::get(LLVMContext &Context, bool isPacked) { 444 return get(Context, None, isPacked); 445 } 446 447 StructType *StructType::get(Type *type, ...) { 448 assert(type && "Cannot create a struct type with no elements with this"); 449 LLVMContext &Ctx = type->getContext(); 450 va_list ap; 451 SmallVector<llvm::Type*, 8> StructFields; 452 va_start(ap, type); 453 while (type) { 454 StructFields.push_back(type); 455 type = va_arg(ap, llvm::Type*); 456 } 457 auto *Ret = llvm::StructType::get(Ctx, StructFields); 458 va_end(ap); 459 return Ret; 460 } 461 462 StructType *StructType::create(LLVMContext &Context, ArrayRef<Type*> Elements, 463 StringRef Name, bool isPacked) { 464 StructType *ST = create(Context, Name); 465 ST->setBody(Elements, isPacked); 466 return ST; 467 } 468 469 StructType *StructType::create(LLVMContext &Context, ArrayRef<Type*> Elements) { 470 return create(Context, Elements, StringRef()); 471 } 472 473 StructType *StructType::create(LLVMContext &Context) { 474 return create(Context, StringRef()); 475 } 476 477 StructType *StructType::create(ArrayRef<Type*> Elements, StringRef Name, 478 bool isPacked) { 479 assert(!Elements.empty() && 480 "This method may not be invoked with an empty list"); 481 return create(Elements[0]->getContext(), Elements, Name, isPacked); 482 } 483 484 StructType *StructType::create(ArrayRef<Type*> Elements) { 485 assert(!Elements.empty() && 486 "This method may not be invoked with an empty list"); 487 return create(Elements[0]->getContext(), Elements, StringRef()); 488 } 489 490 StructType *StructType::create(StringRef Name, Type *type, ...) { 491 assert(type && "Cannot create a struct type with no elements with this"); 492 LLVMContext &Ctx = type->getContext(); 493 va_list ap; 494 SmallVector<llvm::Type*, 8> StructFields; 495 va_start(ap, type); 496 while (type) { 497 StructFields.push_back(type); 498 type = va_arg(ap, llvm::Type*); 499 } 500 auto *Ret = llvm::StructType::create(Ctx, StructFields, Name); 501 va_end(ap); 502 return Ret; 503 } 504 505 bool StructType::isSized(SmallPtrSetImpl<Type*> *Visited) const { 506 if ((getSubclassData() & SCDB_IsSized) != 0) 507 return true; 508 if (isOpaque()) 509 return false; 510 511 if (Visited && !Visited->insert(const_cast<StructType*>(this)).second) 512 return false; 513 514 // Okay, our struct is sized if all of the elements are, but if one of the 515 // elements is opaque, the struct isn't sized *yet*, but may become sized in 516 // the future, so just bail out without caching. 517 for (element_iterator I = element_begin(), E = element_end(); I != E; ++I) 518 if (!(*I)->isSized(Visited)) 519 return false; 520 521 // Here we cheat a bit and cast away const-ness. The goal is to memoize when 522 // we find a sized type, as types can only move from opaque to sized, not the 523 // other way. 524 const_cast<StructType*>(this)->setSubclassData( 525 getSubclassData() | SCDB_IsSized); 526 return true; 527 } 528 529 StringRef StructType::getName() const { 530 assert(!isLiteral() && "Literal structs never have names"); 531 if (!SymbolTableEntry) return StringRef(); 532 533 return ((StringMapEntry<StructType*> *)SymbolTableEntry)->getKey(); 534 } 535 536 void StructType::setBody(Type *type, ...) { 537 assert(type && "Cannot create a struct type with no elements with this"); 538 va_list ap; 539 SmallVector<llvm::Type*, 8> StructFields; 540 va_start(ap, type); 541 while (type) { 542 StructFields.push_back(type); 543 type = va_arg(ap, llvm::Type*); 544 } 545 setBody(StructFields); 546 va_end(ap); 547 } 548 549 bool StructType::isValidElementType(Type *ElemTy) { 550 return !ElemTy->isVoidTy() && !ElemTy->isLabelTy() && 551 !ElemTy->isMetadataTy() && !ElemTy->isFunctionTy() && 552 !ElemTy->isTokenTy(); 553 } 554 555 /// isLayoutIdentical - Return true if this is layout identical to the 556 /// specified struct. 557 bool StructType::isLayoutIdentical(StructType *Other) const { 558 if (this == Other) return true; 559 560 if (isPacked() != Other->isPacked()) 561 return false; 562 563 return elements() == Other->elements(); 564 } 565 566 /// getTypeByName - Return the type with the specified name, or null if there 567 /// is none by that name. 568 StructType *Module::getTypeByName(StringRef Name) const { 569 return getContext().pImpl->NamedStructTypes.lookup(Name); 570 } 571 572 573 //===----------------------------------------------------------------------===// 574 // CompositeType Implementation 575 //===----------------------------------------------------------------------===// 576 577 Type *CompositeType::getTypeAtIndex(const Value *V) const { 578 if (auto *STy = dyn_cast<StructType>(this)) { 579 unsigned Idx = 580 (unsigned)cast<Constant>(V)->getUniqueInteger().getZExtValue(); 581 assert(indexValid(Idx) && "Invalid structure index!"); 582 return STy->getElementType(Idx); 583 } 584 585 return cast<SequentialType>(this)->getElementType(); 586 } 587 588 Type *CompositeType::getTypeAtIndex(unsigned Idx) const{ 589 if (auto *STy = dyn_cast<StructType>(this)) { 590 assert(indexValid(Idx) && "Invalid structure index!"); 591 return STy->getElementType(Idx); 592 } 593 594 return cast<SequentialType>(this)->getElementType(); 595 } 596 597 bool CompositeType::indexValid(const Value *V) const { 598 if (auto *STy = dyn_cast<StructType>(this)) { 599 // Structure indexes require (vectors of) 32-bit integer constants. In the 600 // vector case all of the indices must be equal. 601 if (!V->getType()->getScalarType()->isIntegerTy(32)) 602 return false; 603 const Constant *C = dyn_cast<Constant>(V); 604 if (C && V->getType()->isVectorTy()) 605 C = C->getSplatValue(); 606 const ConstantInt *CU = dyn_cast_or_null<ConstantInt>(C); 607 return CU && CU->getZExtValue() < STy->getNumElements(); 608 } 609 610 // Sequential types can be indexed by any integer. 611 return V->getType()->isIntOrIntVectorTy(); 612 } 613 614 bool CompositeType::indexValid(unsigned Idx) const { 615 if (auto *STy = dyn_cast<StructType>(this)) 616 return Idx < STy->getNumElements(); 617 // Sequential types can be indexed by any integer. 618 return true; 619 } 620 621 622 //===----------------------------------------------------------------------===// 623 // ArrayType Implementation 624 //===----------------------------------------------------------------------===// 625 626 ArrayType::ArrayType(Type *ElType, uint64_t NumEl) 627 : SequentialType(ArrayTyID, ElType) { 628 NumElements = NumEl; 629 } 630 631 ArrayType *ArrayType::get(Type *ElementType, uint64_t NumElements) { 632 assert(isValidElementType(ElementType) && "Invalid type for array element!"); 633 634 LLVMContextImpl *pImpl = ElementType->getContext().pImpl; 635 ArrayType *&Entry = 636 pImpl->ArrayTypes[std::make_pair(ElementType, NumElements)]; 637 638 if (!Entry) 639 Entry = new (pImpl->TypeAllocator) ArrayType(ElementType, NumElements); 640 return Entry; 641 } 642 643 bool ArrayType::isValidElementType(Type *ElemTy) { 644 return !ElemTy->isVoidTy() && !ElemTy->isLabelTy() && 645 !ElemTy->isMetadataTy() && !ElemTy->isFunctionTy() && 646 !ElemTy->isTokenTy(); 647 } 648 649 //===----------------------------------------------------------------------===// 650 // VectorType Implementation 651 //===----------------------------------------------------------------------===// 652 653 VectorType::VectorType(Type *ElType, unsigned NumEl) 654 : SequentialType(VectorTyID, ElType) { 655 NumElements = NumEl; 656 } 657 658 VectorType *VectorType::get(Type *ElementType, unsigned NumElements) { 659 assert(NumElements > 0 && "#Elements of a VectorType must be greater than 0"); 660 assert(isValidElementType(ElementType) && "Element type of a VectorType must " 661 "be an integer, floating point, or " 662 "pointer type."); 663 664 LLVMContextImpl *pImpl = ElementType->getContext().pImpl; 665 VectorType *&Entry = ElementType->getContext().pImpl 666 ->VectorTypes[std::make_pair(ElementType, NumElements)]; 667 668 if (!Entry) 669 Entry = new (pImpl->TypeAllocator) VectorType(ElementType, NumElements); 670 return Entry; 671 } 672 673 bool VectorType::isValidElementType(Type *ElemTy) { 674 return ElemTy->isIntegerTy() || ElemTy->isFloatingPointTy() || 675 ElemTy->isPointerTy(); 676 } 677 678 //===----------------------------------------------------------------------===// 679 // PointerType Implementation 680 //===----------------------------------------------------------------------===// 681 682 PointerType *PointerType::get(Type *EltTy, unsigned AddressSpace) { 683 assert(EltTy && "Can't get a pointer to <null> type!"); 684 assert(isValidElementType(EltTy) && "Invalid type for pointer element!"); 685 686 LLVMContextImpl *CImpl = EltTy->getContext().pImpl; 687 688 // Since AddressSpace #0 is the common case, we special case it. 689 PointerType *&Entry = AddressSpace == 0 ? CImpl->PointerTypes[EltTy] 690 : CImpl->ASPointerTypes[std::make_pair(EltTy, AddressSpace)]; 691 692 if (!Entry) 693 Entry = new (CImpl->TypeAllocator) PointerType(EltTy, AddressSpace); 694 return Entry; 695 } 696 697 698 PointerType::PointerType(Type *E, unsigned AddrSpace) 699 : SequentialType(PointerTyID, E) { 700 #ifndef NDEBUG 701 const unsigned oldNCT = NumContainedTys; 702 #endif 703 setSubclassData(AddrSpace); 704 // Check for miscompile. PR11652. 705 assert(oldNCT == NumContainedTys && "bitfield written out of bounds?"); 706 } 707 708 PointerType *Type::getPointerTo(unsigned addrs) const { 709 return PointerType::get(const_cast<Type*>(this), addrs); 710 } 711 712 bool PointerType::isValidElementType(Type *ElemTy) { 713 return !ElemTy->isVoidTy() && !ElemTy->isLabelTy() && 714 !ElemTy->isMetadataTy() && !ElemTy->isTokenTy(); 715 } 716 717 bool PointerType::isLoadableOrStorableType(Type *ElemTy) { 718 return isValidElementType(ElemTy) && !ElemTy->isFunctionTy(); 719 } 720