1081 lines · cpp
1//===- Type.cpp - Implement the Type class --------------------------------===//2//3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.4// See https://llvm.org/LICENSE.txt for license information.5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception6//7//===----------------------------------------------------------------------===//8//9// This file implements the Type class for the IR library.10//11//===----------------------------------------------------------------------===//12 13#include "llvm/IR/Type.h"14#include "LLVMContextImpl.h"15#include "llvm/ADT/APInt.h"16#include "llvm/ADT/SetVector.h"17#include "llvm/ADT/SmallString.h"18#include "llvm/ADT/StringMap.h"19#include "llvm/ADT/StringRef.h"20#include "llvm/IR/Constant.h"21#include "llvm/IR/Constants.h"22#include "llvm/IR/DerivedTypes.h"23#include "llvm/IR/LLVMContext.h"24#include "llvm/IR/Value.h"25#include "llvm/Support/Casting.h"26#include "llvm/Support/Error.h"27#include "llvm/Support/TypeSize.h"28#include "llvm/Support/raw_ostream.h"29#include "llvm/TargetParser/RISCVTargetParser.h"30#include <cassert>31 32using namespace llvm;33 34//===----------------------------------------------------------------------===//35// Type Class Implementation36//===----------------------------------------------------------------------===//37 38Type *Type::getPrimitiveType(LLVMContext &C, TypeID IDNumber) {39 switch (IDNumber) {40 case VoidTyID : return getVoidTy(C);41 case HalfTyID : return getHalfTy(C);42 case BFloatTyID : return getBFloatTy(C);43 case FloatTyID : return getFloatTy(C);44 case DoubleTyID : return getDoubleTy(C);45 case X86_FP80TyID : return getX86_FP80Ty(C);46 case FP128TyID : return getFP128Ty(C);47 case PPC_FP128TyID : return getPPC_FP128Ty(C);48 case LabelTyID : return getLabelTy(C);49 case MetadataTyID : return getMetadataTy(C);50 case X86_AMXTyID : return getX86_AMXTy(C);51 case TokenTyID : return getTokenTy(C);52 default:53 return nullptr;54 }55}56 57bool Type::isIntegerTy(unsigned Bitwidth) const {58 return isIntegerTy() && cast<IntegerType>(this)->getBitWidth() == Bitwidth;59}60 61bool Type::isScalableTy(SmallPtrSetImpl<const Type *> &Visited) const {62 if (const auto *ATy = dyn_cast<ArrayType>(this))63 return ATy->getElementType()->isScalableTy(Visited);64 if (const auto *STy = dyn_cast<StructType>(this))65 return STy->isScalableTy(Visited);66 return getTypeID() == ScalableVectorTyID || isScalableTargetExtTy();67}68 69bool Type::isScalableTy() const {70 SmallPtrSet<const Type *, 4> Visited;71 return isScalableTy(Visited);72}73 74bool Type::containsNonGlobalTargetExtType(75 SmallPtrSetImpl<const Type *> &Visited) const {76 if (const auto *ATy = dyn_cast<ArrayType>(this))77 return ATy->getElementType()->containsNonGlobalTargetExtType(Visited);78 if (const auto *STy = dyn_cast<StructType>(this))79 return STy->containsNonGlobalTargetExtType(Visited);80 if (auto *TT = dyn_cast<TargetExtType>(this))81 return !TT->hasProperty(TargetExtType::CanBeGlobal);82 return false;83}84 85bool Type::containsNonGlobalTargetExtType() const {86 SmallPtrSet<const Type *, 4> Visited;87 return containsNonGlobalTargetExtType(Visited);88}89 90bool Type::containsNonLocalTargetExtType(91 SmallPtrSetImpl<const Type *> &Visited) const {92 if (const auto *ATy = dyn_cast<ArrayType>(this))93 return ATy->getElementType()->containsNonLocalTargetExtType(Visited);94 if (const auto *STy = dyn_cast<StructType>(this))95 return STy->containsNonLocalTargetExtType(Visited);96 if (auto *TT = dyn_cast<TargetExtType>(this))97 return !TT->hasProperty(TargetExtType::CanBeLocal);98 return false;99}100 101bool Type::containsNonLocalTargetExtType() const {102 SmallPtrSet<const Type *, 4> Visited;103 return containsNonLocalTargetExtType(Visited);104}105 106const fltSemantics &Type::getFltSemantics() const {107 switch (getTypeID()) {108 case HalfTyID: return APFloat::IEEEhalf();109 case BFloatTyID: return APFloat::BFloat();110 case FloatTyID: return APFloat::IEEEsingle();111 case DoubleTyID: return APFloat::IEEEdouble();112 case X86_FP80TyID: return APFloat::x87DoubleExtended();113 case FP128TyID: return APFloat::IEEEquad();114 case PPC_FP128TyID: return APFloat::PPCDoubleDouble();115 default: llvm_unreachable("Invalid floating type");116 }117}118 119bool Type::isScalableTargetExtTy() const {120 if (auto *TT = dyn_cast<TargetExtType>(this))121 return isa<ScalableVectorType>(TT->getLayoutType());122 return false;123}124 125Type *Type::getFloatingPointTy(LLVMContext &C, const fltSemantics &S) {126 Type *Ty;127 if (&S == &APFloat::IEEEhalf())128 Ty = Type::getHalfTy(C);129 else if (&S == &APFloat::BFloat())130 Ty = Type::getBFloatTy(C);131 else if (&S == &APFloat::IEEEsingle())132 Ty = Type::getFloatTy(C);133 else if (&S == &APFloat::IEEEdouble())134 Ty = Type::getDoubleTy(C);135 else if (&S == &APFloat::x87DoubleExtended())136 Ty = Type::getX86_FP80Ty(C);137 else if (&S == &APFloat::IEEEquad())138 Ty = Type::getFP128Ty(C);139 else {140 assert(&S == &APFloat::PPCDoubleDouble() && "Unknown FP format");141 Ty = Type::getPPC_FP128Ty(C);142 }143 return Ty;144}145 146bool Type::isRISCVVectorTupleTy() const {147 if (!isTargetExtTy())148 return false;149 150 return cast<TargetExtType>(this)->getName() == "riscv.vector.tuple";151}152 153bool Type::canLosslesslyBitCastTo(Type *Ty) const {154 // Identity cast means no change so return true155 if (this == Ty)156 return true;157 158 // They are not convertible unless they are at least first class types159 if (!this->isFirstClassType() || !Ty->isFirstClassType())160 return false;161 162 // Vector -> Vector conversions are always lossless if the two vector types163 // have the same size, otherwise not.164 if (isa<VectorType>(this) && isa<VectorType>(Ty))165 return getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits();166 167 // 8192-bit fixed width vector types can be losslessly converted to x86amx.168 if (((isa<FixedVectorType>(this)) && Ty->isX86_AMXTy()) &&169 getPrimitiveSizeInBits().getFixedValue() == 8192)170 return true;171 if ((isX86_AMXTy() && isa<FixedVectorType>(Ty)) &&172 Ty->getPrimitiveSizeInBits().getFixedValue() == 8192)173 return true;174 175 // Conservatively assume we can't losslessly convert between pointers with176 // different address spaces.177 return false;178}179 180bool Type::isEmptyTy() const {181 if (auto *ATy = dyn_cast<ArrayType>(this)) {182 unsigned NumElements = ATy->getNumElements();183 return NumElements == 0 || ATy->getElementType()->isEmptyTy();184 }185 186 if (auto *STy = dyn_cast<StructType>(this)) {187 unsigned NumElements = STy->getNumElements();188 for (unsigned i = 0; i < NumElements; ++i)189 if (!STy->getElementType(i)->isEmptyTy())190 return false;191 return true;192 }193 194 return false;195}196 197TypeSize Type::getPrimitiveSizeInBits() const {198 switch (getTypeID()) {199 case Type::HalfTyID:200 return TypeSize::getFixed(16);201 case Type::BFloatTyID:202 return TypeSize::getFixed(16);203 case Type::FloatTyID:204 return TypeSize::getFixed(32);205 case Type::DoubleTyID:206 return TypeSize::getFixed(64);207 case Type::X86_FP80TyID:208 return TypeSize::getFixed(80);209 case Type::FP128TyID:210 return TypeSize::getFixed(128);211 case Type::PPC_FP128TyID:212 return TypeSize::getFixed(128);213 case Type::X86_AMXTyID:214 return TypeSize::getFixed(8192);215 case Type::IntegerTyID:216 return TypeSize::getFixed(cast<IntegerType>(this)->getBitWidth());217 case Type::FixedVectorTyID:218 case Type::ScalableVectorTyID: {219 const VectorType *VTy = cast<VectorType>(this);220 ElementCount EC = VTy->getElementCount();221 TypeSize ETS = VTy->getElementType()->getPrimitiveSizeInBits();222 assert(!ETS.isScalable() && "Vector type should have fixed-width elements");223 return {ETS.getFixedValue() * EC.getKnownMinValue(), EC.isScalable()};224 }225 default:226 return TypeSize::getFixed(0);227 }228}229 230unsigned Type::getScalarSizeInBits() const {231 // It is safe to assume that the scalar types have a fixed size.232 return getScalarType()->getPrimitiveSizeInBits().getFixedValue();233}234 235int Type::getFPMantissaWidth() const {236 if (auto *VTy = dyn_cast<VectorType>(this))237 return VTy->getElementType()->getFPMantissaWidth();238 assert(isFloatingPointTy() && "Not a floating point type!");239 if (getTypeID() == HalfTyID) return 11;240 if (getTypeID() == BFloatTyID) return 8;241 if (getTypeID() == FloatTyID) return 24;242 if (getTypeID() == DoubleTyID) return 53;243 if (getTypeID() == X86_FP80TyID) return 64;244 if (getTypeID() == FP128TyID) return 113;245 assert(getTypeID() == PPC_FP128TyID && "unknown fp type");246 return -1;247}248 249bool Type::isFirstClassType() const {250 switch (getTypeID()) {251 default:252 return true;253 case FunctionTyID:254 case VoidTyID:255 return false;256 case StructTyID: {257 auto *ST = cast<StructType>(this);258 return !ST->isOpaque();259 }260 }261}262 263bool Type::isSizedDerivedType(SmallPtrSetImpl<Type*> *Visited) const {264 if (auto *ATy = dyn_cast<ArrayType>(this))265 return ATy->getElementType()->isSized(Visited);266 267 if (auto *VTy = dyn_cast<VectorType>(this))268 return VTy->getElementType()->isSized(Visited);269 270 if (auto *TTy = dyn_cast<TargetExtType>(this))271 return TTy->getLayoutType()->isSized(Visited);272 273 return cast<StructType>(this)->isSized(Visited);274}275 276//===----------------------------------------------------------------------===//277// Primitive 'Type' data278//===----------------------------------------------------------------------===//279 280Type *Type::getVoidTy(LLVMContext &C) { return &C.pImpl->VoidTy; }281Type *Type::getLabelTy(LLVMContext &C) { return &C.pImpl->LabelTy; }282Type *Type::getHalfTy(LLVMContext &C) { return &C.pImpl->HalfTy; }283Type *Type::getBFloatTy(LLVMContext &C) { return &C.pImpl->BFloatTy; }284Type *Type::getFloatTy(LLVMContext &C) { return &C.pImpl->FloatTy; }285Type *Type::getDoubleTy(LLVMContext &C) { return &C.pImpl->DoubleTy; }286Type *Type::getMetadataTy(LLVMContext &C) { return &C.pImpl->MetadataTy; }287Type *Type::getTokenTy(LLVMContext &C) { return &C.pImpl->TokenTy; }288Type *Type::getX86_FP80Ty(LLVMContext &C) { return &C.pImpl->X86_FP80Ty; }289Type *Type::getFP128Ty(LLVMContext &C) { return &C.pImpl->FP128Ty; }290Type *Type::getPPC_FP128Ty(LLVMContext &C) { return &C.pImpl->PPC_FP128Ty; }291Type *Type::getX86_AMXTy(LLVMContext &C) { return &C.pImpl->X86_AMXTy; }292 293IntegerType *Type::getInt1Ty(LLVMContext &C) { return &C.pImpl->Int1Ty; }294IntegerType *Type::getInt8Ty(LLVMContext &C) { return &C.pImpl->Int8Ty; }295IntegerType *Type::getInt16Ty(LLVMContext &C) { return &C.pImpl->Int16Ty; }296IntegerType *Type::getInt32Ty(LLVMContext &C) { return &C.pImpl->Int32Ty; }297IntegerType *Type::getInt64Ty(LLVMContext &C) { return &C.pImpl->Int64Ty; }298IntegerType *Type::getInt128Ty(LLVMContext &C) { return &C.pImpl->Int128Ty; }299 300IntegerType *Type::getIntNTy(LLVMContext &C, unsigned N) {301 return IntegerType::get(C, N);302}303 304Type *Type::getWasm_ExternrefTy(LLVMContext &C) {305 // opaque pointer in addrspace(10)306 return PointerType::get(C, 10);307}308 309Type *Type::getWasm_FuncrefTy(LLVMContext &C) {310 // opaque pointer in addrspace(20)311 return PointerType::get(C, 20);312}313 314//===----------------------------------------------------------------------===//315// IntegerType Implementation316//===----------------------------------------------------------------------===//317 318IntegerType *IntegerType::get(LLVMContext &C, unsigned NumBits) {319 assert(NumBits >= MIN_INT_BITS && "bitwidth too small");320 assert(NumBits <= MAX_INT_BITS && "bitwidth too large");321 322 // Check for the built-in integer types323 switch (NumBits) {324 case 1: return Type::getInt1Ty(C);325 case 8: return Type::getInt8Ty(C);326 case 16: return Type::getInt16Ty(C);327 case 32: return Type::getInt32Ty(C);328 case 64: return Type::getInt64Ty(C);329 case 128: return Type::getInt128Ty(C);330 default:331 break;332 }333 334 IntegerType *&Entry = C.pImpl->IntegerTypes[NumBits];335 336 if (!Entry)337 Entry = new (C.pImpl->Alloc) IntegerType(C, NumBits);338 339 return Entry;340}341 342APInt IntegerType::getMask() const { return APInt::getAllOnes(getBitWidth()); }343 344//===----------------------------------------------------------------------===//345// FunctionType Implementation346//===----------------------------------------------------------------------===//347 348FunctionType::FunctionType(Type *Result, ArrayRef<Type*> Params,349 bool IsVarArgs)350 : Type(Result->getContext(), FunctionTyID) {351 Type **SubTys = reinterpret_cast<Type**>(this+1);352 assert(isValidReturnType(Result) && "invalid return type for function");353 setSubclassData(IsVarArgs);354 355 SubTys[0] = Result;356 357 for (unsigned i = 0, e = Params.size(); i != e; ++i) {358 assert(isValidArgumentType(Params[i]) &&359 "Not a valid type for function argument!");360 SubTys[i+1] = Params[i];361 }362 363 ContainedTys = SubTys;364 NumContainedTys = Params.size() + 1; // + 1 for result type365}366 367// This is the factory function for the FunctionType class.368FunctionType *FunctionType::get(Type *ReturnType,369 ArrayRef<Type*> Params, bool isVarArg) {370 LLVMContextImpl *pImpl = ReturnType->getContext().pImpl;371 const FunctionTypeKeyInfo::KeyTy Key(ReturnType, Params, isVarArg);372 FunctionType *FT;373 // Since we only want to allocate a fresh function type in case none is found374 // and we don't want to perform two lookups (one for checking if existent and375 // one for inserting the newly allocated one), here we instead lookup based on376 // Key and update the reference to the function type in-place to a newly377 // allocated one if not found.378 auto Insertion = pImpl->FunctionTypes.insert_as(nullptr, Key);379 if (Insertion.second) {380 // The function type was not found. Allocate one and update FunctionTypes381 // in-place.382 FT = (FunctionType *)pImpl->Alloc.Allocate(383 sizeof(FunctionType) + sizeof(Type *) * (Params.size() + 1),384 alignof(FunctionType));385 new (FT) FunctionType(ReturnType, Params, isVarArg);386 *Insertion.first = FT;387 } else {388 // The function type was found. Just return it.389 FT = *Insertion.first;390 }391 return FT;392}393 394FunctionType *FunctionType::get(Type *Result, bool isVarArg) {395 return get(Result, {}, isVarArg);396}397 398bool FunctionType::isValidReturnType(Type *RetTy) {399 return !RetTy->isFunctionTy() && !RetTy->isLabelTy() &&400 !RetTy->isMetadataTy();401}402 403bool FunctionType::isValidArgumentType(Type *ArgTy) {404 return ArgTy->isFirstClassType() && !ArgTy->isLabelTy();405}406 407//===----------------------------------------------------------------------===//408// StructType Implementation409//===----------------------------------------------------------------------===//410 411// Primitive Constructors.412 413StructType *StructType::get(LLVMContext &Context, ArrayRef<Type*> ETypes,414 bool isPacked) {415 LLVMContextImpl *pImpl = Context.pImpl;416 const AnonStructTypeKeyInfo::KeyTy Key(ETypes, isPacked);417 418 StructType *ST;419 // Since we only want to allocate a fresh struct type in case none is found420 // and we don't want to perform two lookups (one for checking if existent and421 // one for inserting the newly allocated one), here we instead lookup based on422 // Key and update the reference to the struct type in-place to a newly423 // allocated one if not found.424 auto Insertion = pImpl->AnonStructTypes.insert_as(nullptr, Key);425 if (Insertion.second) {426 // The struct type was not found. Allocate one and update AnonStructTypes427 // in-place.428 ST = new (Context.pImpl->Alloc) StructType(Context);429 ST->setSubclassData(SCDB_IsLiteral); // Literal struct.430 ST->setBody(ETypes, isPacked);431 *Insertion.first = ST;432 } else {433 // The struct type was found. Just return it.434 ST = *Insertion.first;435 }436 437 return ST;438}439 440bool StructType::isScalableTy(SmallPtrSetImpl<const Type *> &Visited) const {441 if ((getSubclassData() & SCDB_ContainsScalableVector) != 0)442 return true;443 444 if ((getSubclassData() & SCDB_NotContainsScalableVector) != 0)445 return false;446 447 if (!Visited.insert(this).second)448 return false;449 450 for (Type *Ty : elements()) {451 if (Ty->isScalableTy(Visited)) {452 const_cast<StructType *>(this)->setSubclassData(453 getSubclassData() | SCDB_ContainsScalableVector);454 return true;455 }456 }457 458 // For structures that are opaque, return false but do not set the459 // SCDB_NotContainsScalableVector flag since it may gain scalable vector type460 // when it becomes non-opaque.461 if (!isOpaque())462 const_cast<StructType *>(this)->setSubclassData(463 getSubclassData() | SCDB_NotContainsScalableVector);464 return false;465}466 467bool StructType::containsNonGlobalTargetExtType(468 SmallPtrSetImpl<const Type *> &Visited) const {469 if ((getSubclassData() & SCDB_ContainsNonGlobalTargetExtType) != 0)470 return true;471 472 if ((getSubclassData() & SCDB_NotContainsNonGlobalTargetExtType) != 0)473 return false;474 475 if (!Visited.insert(this).second)476 return false;477 478 for (Type *Ty : elements()) {479 if (Ty->containsNonGlobalTargetExtType(Visited)) {480 const_cast<StructType *>(this)->setSubclassData(481 getSubclassData() | SCDB_ContainsNonGlobalTargetExtType);482 return true;483 }484 }485 486 // For structures that are opaque, return false but do not set the487 // SCDB_NotContainsNonGlobalTargetExtType flag since it may gain non-global488 // target extension types when it becomes non-opaque.489 if (!isOpaque())490 const_cast<StructType *>(this)->setSubclassData(491 getSubclassData() | SCDB_NotContainsNonGlobalTargetExtType);492 return false;493}494 495bool StructType::containsNonLocalTargetExtType(496 SmallPtrSetImpl<const Type *> &Visited) const {497 if ((getSubclassData() & SCDB_ContainsNonLocalTargetExtType) != 0)498 return true;499 500 if ((getSubclassData() & SCDB_NotContainsNonLocalTargetExtType) != 0)501 return false;502 503 if (!Visited.insert(this).second)504 return false;505 506 for (Type *Ty : elements()) {507 if (Ty->containsNonLocalTargetExtType(Visited)) {508 const_cast<StructType *>(this)->setSubclassData(509 getSubclassData() | SCDB_ContainsNonLocalTargetExtType);510 return true;511 }512 }513 514 // For structures that are opaque, return false but do not set the515 // SCDB_NotContainsNonLocalTargetExtType flag since it may gain non-local516 // target extension types when it becomes non-opaque.517 if (!isOpaque())518 const_cast<StructType *>(this)->setSubclassData(519 getSubclassData() | SCDB_NotContainsNonLocalTargetExtType);520 return false;521}522 523bool StructType::containsHomogeneousScalableVectorTypes() const {524 if (getNumElements() <= 0 || !isa<ScalableVectorType>(elements().front()))525 return false;526 return containsHomogeneousTypes();527}528 529bool StructType::containsHomogeneousTypes() const {530 ArrayRef<Type *> ElementTys = elements();531 return !ElementTys.empty() && all_equal(ElementTys);532}533 534void StructType::setBody(ArrayRef<Type*> Elements, bool isPacked) {535 cantFail(setBodyOrError(Elements, isPacked));536}537 538Error StructType::setBodyOrError(ArrayRef<Type *> Elements, bool isPacked) {539 assert(isOpaque() && "Struct body already set!");540 541 if (auto E = checkBody(Elements))542 return E;543 544 setSubclassData(getSubclassData() | SCDB_HasBody);545 if (isPacked)546 setSubclassData(getSubclassData() | SCDB_Packed);547 548 NumContainedTys = Elements.size();549 ContainedTys = Elements.empty()550 ? nullptr551 : Elements.copy(getContext().pImpl->Alloc).data();552 553 return Error::success();554}555 556Error StructType::checkBody(ArrayRef<Type *> Elements) {557 SmallSetVector<Type *, 4> Worklist(Elements.begin(), Elements.end());558 for (unsigned I = 0; I < Worklist.size(); ++I) {559 Type *Ty = Worklist[I];560 if (Ty == this)561 return createStringError(Twine("identified structure type '") +562 getName() + "' is recursive");563 Worklist.insert_range(Ty->subtypes());564 }565 return Error::success();566}567 568void StructType::setName(StringRef Name) {569 if (Name == getName()) return;570 571 StringMap<StructType *> &SymbolTable = getContext().pImpl->NamedStructTypes;572 573 using EntryTy = StringMap<StructType *>::MapEntryTy;574 575 // If this struct already had a name, remove its symbol table entry. Don't576 // delete the data yet because it may be part of the new name.577 if (SymbolTableEntry)578 SymbolTable.remove((EntryTy *)SymbolTableEntry);579 580 // If this is just removing the name, we're done.581 if (Name.empty()) {582 if (SymbolTableEntry) {583 // Delete the old string data.584 ((EntryTy *)SymbolTableEntry)->Destroy(SymbolTable.getAllocator());585 SymbolTableEntry = nullptr;586 }587 return;588 }589 590 // Look up the entry for the name.591 auto IterBool =592 getContext().pImpl->NamedStructTypes.insert(std::make_pair(Name, this));593 594 // While we have a name collision, try a random rename.595 if (!IterBool.second) {596 SmallString<64> TempStr(Name);597 TempStr.push_back('.');598 raw_svector_ostream TmpStream(TempStr);599 unsigned NameSize = Name.size();600 601 do {602 TempStr.resize(NameSize + 1);603 TmpStream << getContext().pImpl->NamedStructTypesUniqueID++;604 605 IterBool = getContext().pImpl->NamedStructTypes.insert(606 std::make_pair(TmpStream.str(), this));607 } while (!IterBool.second);608 }609 610 // Delete the old string data.611 if (SymbolTableEntry)612 ((EntryTy *)SymbolTableEntry)->Destroy(SymbolTable.getAllocator());613 SymbolTableEntry = &*IterBool.first;614}615 616//===----------------------------------------------------------------------===//617// StructType Helper functions.618 619StructType *StructType::create(LLVMContext &Context, StringRef Name) {620 StructType *ST = new (Context.pImpl->Alloc) StructType(Context);621 if (!Name.empty())622 ST->setName(Name);623 return ST;624}625 626StructType *StructType::get(LLVMContext &Context, bool isPacked) {627 return get(Context, {}, isPacked);628}629 630StructType *StructType::create(LLVMContext &Context, ArrayRef<Type*> Elements,631 StringRef Name, bool isPacked) {632 StructType *ST = create(Context, Name);633 ST->setBody(Elements, isPacked);634 return ST;635}636 637StructType *StructType::create(LLVMContext &Context, ArrayRef<Type*> Elements) {638 return create(Context, Elements, StringRef());639}640 641StructType *StructType::create(LLVMContext &Context) {642 return create(Context, StringRef());643}644 645StructType *StructType::create(ArrayRef<Type*> Elements, StringRef Name,646 bool isPacked) {647 assert(!Elements.empty() &&648 "This method may not be invoked with an empty list");649 return create(Elements[0]->getContext(), Elements, Name, isPacked);650}651 652StructType *StructType::create(ArrayRef<Type*> Elements) {653 assert(!Elements.empty() &&654 "This method may not be invoked with an empty list");655 return create(Elements[0]->getContext(), Elements, StringRef());656}657 658bool StructType::isSized(SmallPtrSetImpl<Type*> *Visited) const {659 if ((getSubclassData() & SCDB_IsSized) != 0)660 return true;661 if (isOpaque())662 return false;663 664 if (Visited && !Visited->insert(const_cast<StructType*>(this)).second)665 return false;666 667 // Okay, our struct is sized if all of the elements are, but if one of the668 // elements is opaque, the struct isn't sized *yet*, but may become sized in669 // the future, so just bail out without caching.670 // The ONLY special case inside a struct that is considered sized is when the671 // elements are homogeneous of a scalable vector type.672 if (containsHomogeneousScalableVectorTypes()) {673 const_cast<StructType *>(this)->setSubclassData(getSubclassData() |674 SCDB_IsSized);675 return true;676 }677 for (Type *Ty : elements()) {678 // If the struct contains a scalable vector type, don't consider it sized.679 // This prevents it from being used in loads/stores/allocas/GEPs. The ONLY680 // special case right now is a structure of homogenous scalable vector681 // types and is handled by the if-statement before this for-loop.682 if (Ty->isScalableTy())683 return false;684 if (!Ty->isSized(Visited))685 return false;686 }687 688 // Here we cheat a bit and cast away const-ness. The goal is to memoize when689 // we find a sized type, as types can only move from opaque to sized, not the690 // other way.691 const_cast<StructType*>(this)->setSubclassData(692 getSubclassData() | SCDB_IsSized);693 return true;694}695 696StringRef StructType::getName() const {697 assert(!isLiteral() && "Literal structs never have names");698 if (!SymbolTableEntry) return StringRef();699 700 return ((StringMapEntry<StructType*> *)SymbolTableEntry)->getKey();701}702 703bool StructType::isValidElementType(Type *ElemTy) {704 return !ElemTy->isVoidTy() && !ElemTy->isLabelTy() &&705 !ElemTy->isMetadataTy() && !ElemTy->isFunctionTy() &&706 !ElemTy->isTokenTy();707}708 709bool StructType::isLayoutIdentical(StructType *Other) const {710 if (this == Other) return true;711 712 if (isPacked() != Other->isPacked())713 return false;714 715 return elements() == Other->elements();716}717 718Type *StructType::getTypeAtIndex(const Value *V) const {719 unsigned Idx = (unsigned)cast<Constant>(V)->getUniqueInteger().getZExtValue();720 assert(indexValid(Idx) && "Invalid structure index!");721 return getElementType(Idx);722}723 724bool StructType::indexValid(const Value *V) const {725 // Structure indexes require (vectors of) 32-bit integer constants. In the726 // vector case all of the indices must be equal.727 if (!V->getType()->isIntOrIntVectorTy(32))728 return false;729 if (isa<ScalableVectorType>(V->getType()))730 return false;731 const Constant *C = dyn_cast<Constant>(V);732 if (C && V->getType()->isVectorTy())733 C = C->getSplatValue();734 const ConstantInt *CU = dyn_cast_or_null<ConstantInt>(C);735 return CU && CU->getZExtValue() < getNumElements();736}737 738StructType *StructType::getTypeByName(LLVMContext &C, StringRef Name) {739 return C.pImpl->NamedStructTypes.lookup(Name);740}741 742//===----------------------------------------------------------------------===//743// ArrayType Implementation744//===----------------------------------------------------------------------===//745 746ArrayType::ArrayType(Type *ElType, uint64_t NumEl)747 : Type(ElType->getContext(), ArrayTyID), ContainedType(ElType),748 NumElements(NumEl) {749 ContainedTys = &ContainedType;750 NumContainedTys = 1;751}752 753ArrayType *ArrayType::get(Type *ElementType, uint64_t NumElements) {754 assert(isValidElementType(ElementType) && "Invalid type for array element!");755 756 LLVMContextImpl *pImpl = ElementType->getContext().pImpl;757 ArrayType *&Entry =758 pImpl->ArrayTypes[std::make_pair(ElementType, NumElements)];759 760 if (!Entry)761 Entry = new (pImpl->Alloc) ArrayType(ElementType, NumElements);762 return Entry;763}764 765bool ArrayType::isValidElementType(Type *ElemTy) {766 return !ElemTy->isVoidTy() && !ElemTy->isLabelTy() &&767 !ElemTy->isMetadataTy() && !ElemTy->isFunctionTy() &&768 !ElemTy->isTokenTy() && !ElemTy->isX86_AMXTy();769}770 771//===----------------------------------------------------------------------===//772// VectorType Implementation773//===----------------------------------------------------------------------===//774 775VectorType::VectorType(Type *ElType, unsigned EQ, Type::TypeID TID)776 : Type(ElType->getContext(), TID), ContainedType(ElType),777 ElementQuantity(EQ) {778 ContainedTys = &ContainedType;779 NumContainedTys = 1;780}781 782VectorType *VectorType::get(Type *ElementType, ElementCount EC) {783 if (EC.isScalable())784 return ScalableVectorType::get(ElementType, EC.getKnownMinValue());785 else786 return FixedVectorType::get(ElementType, EC.getKnownMinValue());787}788 789bool VectorType::isValidElementType(Type *ElemTy) {790 if (ElemTy->isIntegerTy() || ElemTy->isFloatingPointTy() ||791 ElemTy->isPointerTy() || ElemTy->getTypeID() == TypedPointerTyID)792 return true;793 if (auto *TTy = dyn_cast<TargetExtType>(ElemTy))794 return TTy->hasProperty(TargetExtType::CanBeVectorElement);795 return false;796}797 798//===----------------------------------------------------------------------===//799// FixedVectorType Implementation800//===----------------------------------------------------------------------===//801 802FixedVectorType *FixedVectorType::get(Type *ElementType, unsigned NumElts) {803 assert(NumElts > 0 && "#Elements of a VectorType must be greater than 0");804 assert(isValidElementType(ElementType) && "Element type of a VectorType must "805 "be an integer, floating point, "806 "pointer type, or a valid target "807 "extension type.");808 809 auto EC = ElementCount::getFixed(NumElts);810 811 LLVMContextImpl *pImpl = ElementType->getContext().pImpl;812 VectorType *&Entry = ElementType->getContext()813 .pImpl->VectorTypes[std::make_pair(ElementType, EC)];814 815 if (!Entry)816 Entry = new (pImpl->Alloc) FixedVectorType(ElementType, NumElts);817 return cast<FixedVectorType>(Entry);818}819 820//===----------------------------------------------------------------------===//821// ScalableVectorType Implementation822//===----------------------------------------------------------------------===//823 824ScalableVectorType *ScalableVectorType::get(Type *ElementType,825 unsigned MinNumElts) {826 assert(MinNumElts > 0 && "#Elements of a VectorType must be greater than 0");827 assert(isValidElementType(ElementType) && "Element type of a VectorType must "828 "be an integer, floating point, or "829 "pointer type.");830 831 auto EC = ElementCount::getScalable(MinNumElts);832 833 LLVMContextImpl *pImpl = ElementType->getContext().pImpl;834 VectorType *&Entry = ElementType->getContext()835 .pImpl->VectorTypes[std::make_pair(ElementType, EC)];836 837 if (!Entry)838 Entry = new (pImpl->Alloc) ScalableVectorType(ElementType, MinNumElts);839 return cast<ScalableVectorType>(Entry);840}841 842//===----------------------------------------------------------------------===//843// PointerType Implementation844//===----------------------------------------------------------------------===//845 846PointerType *PointerType::get(Type *EltTy, unsigned AddressSpace) {847 assert(EltTy && "Can't get a pointer to <null> type!");848 assert(isValidElementType(EltTy) && "Invalid type for pointer element!");849 850 // Automatically convert typed pointers to opaque pointers.851 return get(EltTy->getContext(), AddressSpace);852}853 854PointerType *PointerType::get(LLVMContext &C, unsigned AddressSpace) {855 LLVMContextImpl *CImpl = C.pImpl;856 857 // Since AddressSpace #0 is the common case, we special case it.858 PointerType *&Entry = AddressSpace == 0 ? CImpl->AS0PointerType859 : CImpl->PointerTypes[AddressSpace];860 861 if (!Entry)862 Entry = new (CImpl->Alloc) PointerType(C, AddressSpace);863 return Entry;864}865 866PointerType::PointerType(LLVMContext &C, unsigned AddrSpace)867 : Type(C, PointerTyID) {868 setSubclassData(AddrSpace);869}870 871PointerType *Type::getPointerTo(unsigned AddrSpace) const {872 return PointerType::get(getContext(), AddrSpace);873}874 875bool PointerType::isValidElementType(Type *ElemTy) {876 return !ElemTy->isVoidTy() && !ElemTy->isLabelTy() &&877 !ElemTy->isMetadataTy() && !ElemTy->isTokenTy() &&878 !ElemTy->isX86_AMXTy();879}880 881bool PointerType::isLoadableOrStorableType(Type *ElemTy) {882 return isValidElementType(ElemTy) && !ElemTy->isFunctionTy();883}884 885//===----------------------------------------------------------------------===//886// TargetExtType Implementation887//===----------------------------------------------------------------------===//888 889TargetExtType::TargetExtType(LLVMContext &C, StringRef Name,890 ArrayRef<Type *> Types, ArrayRef<unsigned> Ints)891 : Type(C, TargetExtTyID), Name(C.pImpl->Saver.save(Name)) {892 NumContainedTys = Types.size();893 894 // Parameter storage immediately follows the class in allocation.895 Type **Params = reinterpret_cast<Type **>(this + 1);896 ContainedTys = Params;897 for (Type *T : Types)898 *Params++ = T;899 900 setSubclassData(Ints.size());901 unsigned *IntParamSpace = reinterpret_cast<unsigned *>(Params);902 IntParams = IntParamSpace;903 for (unsigned IntParam : Ints)904 *IntParamSpace++ = IntParam;905}906 907TargetExtType *TargetExtType::get(LLVMContext &C, StringRef Name,908 ArrayRef<Type *> Types,909 ArrayRef<unsigned> Ints) {910 return cantFail(getOrError(C, Name, Types, Ints));911}912 913Expected<TargetExtType *> TargetExtType::getOrError(LLVMContext &C,914 StringRef Name,915 ArrayRef<Type *> Types,916 ArrayRef<unsigned> Ints) {917 const TargetExtTypeKeyInfo::KeyTy Key(Name, Types, Ints);918 TargetExtType *TT;919 // Since we only want to allocate a fresh target type in case none is found920 // and we don't want to perform two lookups (one for checking if existent and921 // one for inserting the newly allocated one), here we instead lookup based on922 // Key and update the reference to the target type in-place to a newly923 // allocated one if not found.924 auto [Iter, Inserted] = C.pImpl->TargetExtTypes.insert_as(nullptr, Key);925 if (Inserted) {926 // The target type was not found. Allocate one and update TargetExtTypes927 // in-place.928 TT = (TargetExtType *)C.pImpl->Alloc.Allocate(929 sizeof(TargetExtType) + sizeof(Type *) * Types.size() +930 sizeof(unsigned) * Ints.size(),931 alignof(TargetExtType));932 new (TT) TargetExtType(C, Name, Types, Ints);933 *Iter = TT;934 return checkParams(TT);935 }936 937 // The target type was found. Just return it.938 return *Iter;939}940 941Expected<TargetExtType *> TargetExtType::checkParams(TargetExtType *TTy) {942 // Opaque types in the AArch64 name space.943 if (TTy->Name == "aarch64.svcount" &&944 (TTy->getNumTypeParameters() != 0 || TTy->getNumIntParameters() != 0))945 return createStringError(946 "target extension type aarch64.svcount should have no parameters");947 948 // Opaque types in the RISC-V name space.949 if (TTy->Name == "riscv.vector.tuple" &&950 (TTy->getNumTypeParameters() != 1 || TTy->getNumIntParameters() != 1))951 return createStringError(952 "target extension type riscv.vector.tuple should have one "953 "type parameter and one integer parameter");954 955 // Opaque types in the AMDGPU name space.956 if (TTy->Name == "amdgcn.named.barrier" &&957 (TTy->getNumTypeParameters() != 0 || TTy->getNumIntParameters() != 1)) {958 return createStringError("target extension type amdgcn.named.barrier "959 "should have no type parameters "960 "and one integer parameter");961 }962 963 return TTy;964}965 966namespace {967struct TargetTypeInfo {968 Type *LayoutType;969 uint64_t Properties;970 971 template <typename... ArgTys>972 TargetTypeInfo(Type *LayoutType, ArgTys... Properties)973 : LayoutType(LayoutType), Properties((0 | ... | Properties)) {974 assert((!(this->Properties & TargetExtType::CanBeVectorElement) ||975 LayoutType->isSized()) &&976 "Vector element type must be sized");977 }978};979} // anonymous namespace980 981static TargetTypeInfo getTargetTypeInfo(const TargetExtType *Ty) {982 LLVMContext &C = Ty->getContext();983 StringRef Name = Ty->getName();984 if (Name == "spirv.Image" || Name == "spirv.SignedImage")985 return TargetTypeInfo(PointerType::get(C, 0), TargetExtType::CanBeGlobal,986 TargetExtType::CanBeLocal);987 if (Name == "spirv.Type") {988 assert(Ty->getNumIntParameters() == 3 &&989 "Wrong number of parameters for spirv.Type");990 991 auto Size = Ty->getIntParameter(1);992 auto Alignment = Ty->getIntParameter(2);993 994 llvm::Type *LayoutType = nullptr;995 if (Size > 0 && Alignment > 0) {996 LayoutType =997 ArrayType::get(Type::getIntNTy(C, Alignment), Size * 8 / Alignment);998 } else {999 // LLVM expects variables that can be allocated to have an alignment and1000 // size. Default to using a 32-bit int as the layout type if none are1001 // present.1002 LayoutType = Type::getInt32Ty(C);1003 }1004 1005 return TargetTypeInfo(LayoutType, TargetExtType::CanBeGlobal,1006 TargetExtType::CanBeLocal);1007 }1008 if (Name == "spirv.IntegralConstant" || Name == "spirv.Literal")1009 return TargetTypeInfo(Type::getVoidTy(C));1010 if (Name == "spirv.Padding")1011 return TargetTypeInfo(1012 ArrayType::get(Type::getInt8Ty(C), Ty->getIntParameter(0)),1013 TargetExtType::CanBeGlobal);1014 if (Name.starts_with("spirv."))1015 return TargetTypeInfo(PointerType::get(C, 0), TargetExtType::HasZeroInit,1016 TargetExtType::CanBeGlobal,1017 TargetExtType::CanBeLocal);1018 1019 // Opaque types in the AArch64 name space.1020 if (Name == "aarch64.svcount")1021 return TargetTypeInfo(ScalableVectorType::get(Type::getInt1Ty(C), 16),1022 TargetExtType::HasZeroInit,1023 TargetExtType::CanBeLocal);1024 1025 // RISC-V vector tuple type. The layout is represented as the type that needs1026 // the same number of vector registers(VREGS) as this tuple type, represented1027 // as <vscale x (RVVBitsPerBlock * VREGS / 8) x i8>.1028 if (Name == "riscv.vector.tuple") {1029 unsigned TotalNumElts =1030 std::max(cast<ScalableVectorType>(Ty->getTypeParameter(0))1031 ->getMinNumElements(),1032 RISCV::RVVBytesPerBlock) *1033 Ty->getIntParameter(0);1034 return TargetTypeInfo(1035 ScalableVectorType::get(Type::getInt8Ty(C), TotalNumElts),1036 TargetExtType::CanBeLocal, TargetExtType::HasZeroInit);1037 }1038 1039 // DirectX resources1040 if (Name == "dx.Padding")1041 return TargetTypeInfo(1042 ArrayType::get(Type::getInt8Ty(C), Ty->getIntParameter(0)),1043 TargetExtType::CanBeGlobal);1044 if (Name.starts_with("dx."))1045 return TargetTypeInfo(PointerType::get(C, 0), TargetExtType::CanBeGlobal,1046 TargetExtType::CanBeLocal,1047 TargetExtType::IsTokenLike);1048 1049 // Opaque types in the AMDGPU name space.1050 if (Name == "amdgcn.named.barrier") {1051 return TargetTypeInfo(FixedVectorType::get(Type::getInt32Ty(C), 4),1052 TargetExtType::CanBeGlobal);1053 }1054 1055 // Type used to test vector element target extension property.1056 // Can be removed once a public target extension type uses CanBeVectorElement.1057 if (Name == "llvm.test.vectorelement") {1058 return TargetTypeInfo(Type::getInt32Ty(C), TargetExtType::CanBeLocal,1059 TargetExtType::CanBeVectorElement);1060 }1061 1062 return TargetTypeInfo(Type::getVoidTy(C));1063}1064 1065bool Type::isTokenLikeTy() const {1066 if (isTokenTy())1067 return true;1068 if (auto *TT = dyn_cast<TargetExtType>(this))1069 return TT->hasProperty(TargetExtType::Property::IsTokenLike);1070 return false;1071}1072 1073Type *TargetExtType::getLayoutType() const {1074 return getTargetTypeInfo(this).LayoutType;1075}1076 1077bool TargetExtType::hasProperty(Property Prop) const {1078 uint64_t Properties = getTargetTypeInfo(this).Properties;1079 return (Properties & Prop) == Prop;1080}1081