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