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