| 1 | //===----------- ValueTypes.cpp - Implementation of EVT methods -----------===// |
| 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 | #include "llvm/CodeGen/ValueTypes.h" |
| 10 | #include "llvm/ADT/APFloat.h" |
| 11 | #include "llvm/ADT/StringExtras.h" |
| 12 | #include "llvm/IR/DerivedTypes.h" |
| 13 | #include "llvm/IR/Type.h" |
| 14 | #include "llvm/Support/Debug.h" |
| 15 | #include "llvm/Support/ErrorHandling.h" |
| 16 | #include "llvm/Support/TypeSize.h" |
| 17 | #include "llvm/Support/WithColor.h" |
| 18 | using namespace llvm; |
| 19 | |
| 20 | EVT EVT::changeExtendedTypeToInteger() const { |
| 21 | assert(isExtended() && "Type is not extended!" ); |
| 22 | LLVMContext &Context = LLVMTy->getContext(); |
| 23 | return getIntegerVT(Context, BitWidth: getSizeInBits()); |
| 24 | } |
| 25 | |
| 26 | EVT EVT::changeExtendedVectorElementTypeToInteger() const { |
| 27 | assert(isExtended() && "Type is not extended!" ); |
| 28 | LLVMContext &Context = LLVMTy->getContext(); |
| 29 | EVT IntTy = getIntegerVT(Context, BitWidth: getScalarSizeInBits()); |
| 30 | return getVectorVT(Context, VT: IntTy, EC: getVectorElementCount()); |
| 31 | } |
| 32 | |
| 33 | EVT EVT::changeExtendedVectorElementType(EVT EltVT) const { |
| 34 | assert(isExtended() && "Type is not extended!" ); |
| 35 | LLVMContext &Context = LLVMTy->getContext(); |
| 36 | return getVectorVT(Context, VT: EltVT, EC: getVectorElementCount()); |
| 37 | } |
| 38 | |
| 39 | EVT EVT::getExtendedIntegerVT(LLVMContext &Context, unsigned BitWidth) { |
| 40 | EVT VT; |
| 41 | VT.LLVMTy = IntegerType::get(C&: Context, NumBits: BitWidth); |
| 42 | assert(VT.isExtended() && "Type is not extended!" ); |
| 43 | return VT; |
| 44 | } |
| 45 | |
| 46 | EVT EVT::getExtendedVectorVT(LLVMContext &Context, EVT VT, unsigned NumElements, |
| 47 | bool IsScalable) { |
| 48 | EVT ResultVT; |
| 49 | ResultVT.LLVMTy = |
| 50 | VectorType::get(ElementType: VT.getTypeForEVT(Context), NumElements, Scalable: IsScalable); |
| 51 | assert(ResultVT.isExtended() && "Type is not extended!" ); |
| 52 | return ResultVT; |
| 53 | } |
| 54 | |
| 55 | EVT EVT::getExtendedVectorVT(LLVMContext &Context, EVT VT, ElementCount EC) { |
| 56 | EVT ResultVT; |
| 57 | ResultVT.LLVMTy = VectorType::get(ElementType: VT.getTypeForEVT(Context), EC); |
| 58 | assert(ResultVT.isExtended() && "Type is not extended!" ); |
| 59 | return ResultVT; |
| 60 | } |
| 61 | |
| 62 | bool EVT::isExtendedFloatingPoint() const { |
| 63 | assert(isExtended() && "Type is not extended!" ); |
| 64 | return LLVMTy->isFPOrFPVectorTy(); |
| 65 | } |
| 66 | |
| 67 | bool EVT::isExtendedInteger() const { |
| 68 | assert(isExtended() && "Type is not extended!" ); |
| 69 | return LLVMTy->isIntOrIntVectorTy(); |
| 70 | } |
| 71 | |
| 72 | bool EVT::isExtendedScalarInteger() const { |
| 73 | assert(isExtended() && "Type is not extended!" ); |
| 74 | return LLVMTy->isIntegerTy(); |
| 75 | } |
| 76 | |
| 77 | bool EVT::isExtendedVector() const { |
| 78 | assert(isExtended() && "Type is not extended!" ); |
| 79 | return LLVMTy->isVectorTy(); |
| 80 | } |
| 81 | |
| 82 | bool EVT::isExtended16BitVector() const { |
| 83 | return isExtendedVector() && |
| 84 | getExtendedSizeInBits() == TypeSize::getFixed(ExactSize: 16); |
| 85 | } |
| 86 | |
| 87 | bool EVT::isExtended32BitVector() const { |
| 88 | return isExtendedVector() && |
| 89 | getExtendedSizeInBits() == TypeSize::getFixed(ExactSize: 32); |
| 90 | } |
| 91 | |
| 92 | bool EVT::isExtended64BitVector() const { |
| 93 | return isExtendedVector() && |
| 94 | getExtendedSizeInBits() == TypeSize::getFixed(ExactSize: 64); |
| 95 | } |
| 96 | |
| 97 | bool EVT::isExtended128BitVector() const { |
| 98 | return isExtendedVector() && |
| 99 | getExtendedSizeInBits() == TypeSize::getFixed(ExactSize: 128); |
| 100 | } |
| 101 | |
| 102 | bool EVT::isExtended256BitVector() const { |
| 103 | return isExtendedVector() && |
| 104 | getExtendedSizeInBits() == TypeSize::getFixed(ExactSize: 256); |
| 105 | } |
| 106 | |
| 107 | bool EVT::isExtended512BitVector() const { |
| 108 | return isExtendedVector() && |
| 109 | getExtendedSizeInBits() == TypeSize::getFixed(ExactSize: 512); |
| 110 | } |
| 111 | |
| 112 | bool EVT::isExtended1024BitVector() const { |
| 113 | return isExtendedVector() && |
| 114 | getExtendedSizeInBits() == TypeSize::getFixed(ExactSize: 1024); |
| 115 | } |
| 116 | |
| 117 | bool EVT::isExtended2048BitVector() const { |
| 118 | return isExtendedVector() && |
| 119 | getExtendedSizeInBits() == TypeSize::getFixed(ExactSize: 2048); |
| 120 | } |
| 121 | |
| 122 | bool EVT::isExtendedFixedLengthVector() const { |
| 123 | return isExtendedVector() && isa<FixedVectorType>(Val: LLVMTy); |
| 124 | } |
| 125 | |
| 126 | bool EVT::isExtendedScalableVector() const { |
| 127 | return isExtendedVector() && isa<ScalableVectorType>(Val: LLVMTy); |
| 128 | } |
| 129 | |
| 130 | EVT EVT::getExtendedVectorElementType() const { |
| 131 | assert(isExtended() && "Type is not extended!" ); |
| 132 | return EVT::getEVT(Ty: cast<VectorType>(Val: LLVMTy)->getElementType()); |
| 133 | } |
| 134 | |
| 135 | unsigned EVT::getExtendedVectorNumElements() const { |
| 136 | assert(isExtended() && "Type is not extended!" ); |
| 137 | ElementCount EC = cast<VectorType>(Val: LLVMTy)->getElementCount(); |
| 138 | if (EC.isScalable()) { |
| 139 | WithColor::warning() |
| 140 | << "The code that requested the fixed number of elements has made the " |
| 141 | "assumption that this vector is not scalable. This assumption was " |
| 142 | "not correct, and this may lead to broken code\n" ; |
| 143 | } |
| 144 | return EC.getKnownMinValue(); |
| 145 | } |
| 146 | |
| 147 | ElementCount EVT::getExtendedVectorElementCount() const { |
| 148 | assert(isExtended() && "Type is not extended!" ); |
| 149 | return cast<VectorType>(Val: LLVMTy)->getElementCount(); |
| 150 | } |
| 151 | |
| 152 | TypeSize EVT::getExtendedSizeInBits() const { |
| 153 | assert(isExtended() && "Type is not extended!" ); |
| 154 | if (IntegerType *ITy = dyn_cast<IntegerType>(Val: LLVMTy)) |
| 155 | return TypeSize::getFixed(ExactSize: ITy->getBitWidth()); |
| 156 | if (VectorType *VTy = dyn_cast<VectorType>(Val: LLVMTy)) |
| 157 | return VTy->getPrimitiveSizeInBits(); |
| 158 | llvm_unreachable("Unrecognized extended type!" ); |
| 159 | } |
| 160 | |
| 161 | /// getEVTString - This function returns value type as a string, e.g. "i32". |
| 162 | void EVT::print(raw_ostream &OS) const { |
| 163 | switch (V.SimpleTy) { |
| 164 | default: |
| 165 | if (isRISCVVectorTuple()) { |
| 166 | unsigned Sz = getSizeInBits().getKnownMinValue(); |
| 167 | unsigned NF = getRISCVVectorTupleNumFields(); |
| 168 | unsigned MinNumElts = Sz / (NF * 8); |
| 169 | OS << "riscv_nxv" << MinNumElts << "i8x" << NF; |
| 170 | return; |
| 171 | } |
| 172 | if (isVector()) { |
| 173 | OS << (isScalableVector() ? "nxv" : "v" ) |
| 174 | << getVectorElementCount().getKnownMinValue() |
| 175 | << getVectorElementType(); |
| 176 | return; |
| 177 | } |
| 178 | if (isInteger()) { |
| 179 | OS << "i" << getSizeInBits(); |
| 180 | return; |
| 181 | } |
| 182 | if (isFloatingPoint()) { |
| 183 | OS << "f" << getSizeInBits(); |
| 184 | return; |
| 185 | } |
| 186 | if (isCheriCapability()) { |
| 187 | OS << "c" << getSizeInBits(); |
| 188 | return; |
| 189 | } |
| 190 | llvm_unreachable("Invalid EVT!" ); |
| 191 | case MVT::bf16: |
| 192 | OS << "bf16" ; |
| 193 | return; |
| 194 | case MVT::ppcf128: |
| 195 | OS << "ppcf128" ; |
| 196 | return; |
| 197 | case MVT::isVoid: |
| 198 | OS << "isVoid" ; |
| 199 | return; |
| 200 | case MVT::Other: |
| 201 | OS << "ch" ; |
| 202 | return; |
| 203 | case MVT::Glue: |
| 204 | OS << "glue" ; |
| 205 | return; |
| 206 | case MVT::x86mmx: |
| 207 | OS << "x86mmx" ; |
| 208 | return; |
| 209 | case MVT::x86amx: |
| 210 | OS << "x86amx" ; |
| 211 | return; |
| 212 | case MVT::i64x8: |
| 213 | OS << "i64x8" ; |
| 214 | return; |
| 215 | case MVT::Metadata: |
| 216 | OS << "Metadata" ; |
| 217 | return; |
| 218 | case MVT::Untyped: |
| 219 | OS << "Untyped" ; |
| 220 | return; |
| 221 | case MVT::funcref: |
| 222 | OS << "funcref" ; |
| 223 | return; |
| 224 | case MVT::exnref: |
| 225 | OS << "exnref" ; |
| 226 | return; |
| 227 | case MVT::externref: |
| 228 | OS << "externref" ; |
| 229 | return; |
| 230 | case MVT::aarch64svcount: |
| 231 | OS << "aarch64svcount" ; |
| 232 | return; |
| 233 | case MVT::spirvbuiltin: |
| 234 | OS << "spirvbuiltin" ; |
| 235 | return; |
| 236 | case MVT::amdgpuBufferFatPointer: |
| 237 | OS << "amdgpuBufferFatPointer" ; |
| 238 | return; |
| 239 | case MVT::amdgpuBufferStridedPointer: |
| 240 | OS << "amdgpuBufferStridedPointer" ; |
| 241 | return; |
| 242 | case MVT::aarch64mfp8: |
| 243 | OS << "aarch64mfp8" ; |
| 244 | return; |
| 245 | } |
| 246 | } |
| 247 | |
| 248 | #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) |
| 249 | void EVT::dump() const { |
| 250 | print(dbgs()); |
| 251 | dbgs() << "\n" ; |
| 252 | } |
| 253 | #endif |
| 254 | |
| 255 | /// getTypeForEVT - This method returns an LLVM type corresponding to the |
| 256 | /// specified EVT. For integer types, this returns an unsigned type. Note |
| 257 | /// that this will abort for types that cannot be represented. |
| 258 | Type *EVT::getTypeForEVT(LLVMContext &Context) const { |
| 259 | // clang-format off |
| 260 | switch (V.SimpleTy) { |
| 261 | default: |
| 262 | assert(isExtended() && "Type is not extended!" ); |
| 263 | return LLVMTy; |
| 264 | case MVT::isVoid: return Type::getVoidTy(C&: Context); |
| 265 | case MVT::x86mmx: return llvm::FixedVectorType::get(ElementType: llvm::IntegerType::get(C&: Context, NumBits: 64), NumElts: 1); |
| 266 | case MVT::aarch64svcount: |
| 267 | return TargetExtType::get(Context, Name: "aarch64.svcount" ); |
| 268 | case MVT::aarch64mfp8: |
| 269 | return FixedVectorType::get(ElementType: IntegerType::get(C&: Context, NumBits: 8), NumElts: 1); |
| 270 | case MVT::x86amx: return Type::getX86_AMXTy(C&: Context); |
| 271 | case MVT::i64x8: return IntegerType::get(C&: Context, NumBits: 512); |
| 272 | case MVT::amdgpuBufferFatPointer: return IntegerType::get(C&: Context, NumBits: 160); |
| 273 | case MVT::amdgpuBufferStridedPointer: return IntegerType::get(C&: Context, NumBits: 192); |
| 274 | case MVT::externref: return Type::getWasm_ExternrefTy(C&: Context); |
| 275 | case MVT::funcref: return Type::getWasm_FuncrefTy(C&: Context); |
| 276 | case MVT::Metadata: return Type::getMetadataTy(C&: Context); |
| 277 | #define GET_VT_EVT(Ty, EVT) case MVT::Ty: return EVT; |
| 278 | #include "llvm/CodeGen/GenVT.inc" |
| 279 | #undef GET_VT_EVT |
| 280 | } |
| 281 | // clang-format on |
| 282 | } |
| 283 | |
| 284 | /// Return the value type corresponding to the specified type. |
| 285 | /// If HandleUnknown is true, unknown types are returned as Other, otherwise |
| 286 | /// they are invalid. |
| 287 | /// NB: This includes pointer types, which require a DataLayout to convert |
| 288 | /// to a concrete value type. |
| 289 | MVT MVT::getVT(Type *Ty, bool HandleUnknown){ |
| 290 | assert(Ty != nullptr && "Invalid type" ); |
| 291 | switch (Ty->getTypeID()) { |
| 292 | default: |
| 293 | if (HandleUnknown) return MVT(MVT::Other); |
| 294 | llvm_unreachable("Unknown type!" ); |
| 295 | case Type::VoidTyID: |
| 296 | return MVT::isVoid; |
| 297 | case Type::ByteTyID: |
| 298 | return getIntegerVT(BitWidth: cast<ByteType>(Val: Ty)->getBitWidth()); |
| 299 | case Type::IntegerTyID: |
| 300 | return getIntegerVT(BitWidth: cast<IntegerType>(Val: Ty)->getBitWidth()); |
| 301 | case Type::HalfTyID: return MVT(MVT::f16); |
| 302 | case Type::BFloatTyID: return MVT(MVT::bf16); |
| 303 | case Type::FloatTyID: return MVT(MVT::f32); |
| 304 | case Type::DoubleTyID: return MVT(MVT::f64); |
| 305 | case Type::X86_FP80TyID: |
| 306 | return MVT(MVT::f80); |
| 307 | case Type::TargetExtTyID: { |
| 308 | TargetExtType *TargetExtTy = cast<TargetExtType>(Val: Ty); |
| 309 | if (TargetExtTy->getName() == "aarch64.svcount" ) |
| 310 | return MVT(MVT::aarch64svcount); |
| 311 | else if (TargetExtTy->getName() == "wasm.externref" ) |
| 312 | return MVT(MVT::externref); |
| 313 | else if (TargetExtTy->getName() == "wasm.funcref" ) |
| 314 | return MVT(MVT::funcref); |
| 315 | else if (TargetExtTy->getName().starts_with(Prefix: "spirv." )) |
| 316 | return MVT(MVT::spirvbuiltin); |
| 317 | if (TargetExtTy->getName() == "riscv.vector.tuple" ) { |
| 318 | unsigned Sz = cast<ScalableVectorType>(Val: TargetExtTy->getTypeParameter(i: 0)) |
| 319 | ->getMinNumElements() * |
| 320 | 8; |
| 321 | unsigned NF = TargetExtTy->getIntParameter(i: 0); |
| 322 | |
| 323 | return MVT::getRISCVVectorTupleVT(Sz: Sz * NF, NFields: NF); |
| 324 | } |
| 325 | if (HandleUnknown) |
| 326 | return MVT(MVT::Other); |
| 327 | llvm_unreachable("Unknown target ext type!" ); |
| 328 | } |
| 329 | case Type::X86_AMXTyID: return MVT(MVT::x86amx); |
| 330 | case Type::FP128TyID: return MVT(MVT::f128); |
| 331 | case Type::PPC_FP128TyID: return MVT(MVT::ppcf128); |
| 332 | case Type::FixedVectorTyID: |
| 333 | case Type::ScalableVectorTyID: { |
| 334 | VectorType *VTy = cast<VectorType>(Val: Ty); |
| 335 | return getVectorVT( |
| 336 | VT: getVT(Ty: VTy->getElementType(), /*HandleUnknown=*/ false), |
| 337 | EC: VTy->getElementCount()); |
| 338 | } |
| 339 | } |
| 340 | } |
| 341 | |
| 342 | /// getEVT - Return the value type corresponding to the specified type. |
| 343 | /// If HandleUnknown is true, unknown types are returned as Other, otherwise |
| 344 | /// they are invalid. |
| 345 | /// NB: This includes pointer types, which require a DataLayout to convert |
| 346 | /// to a concrete value type. |
| 347 | EVT EVT::getEVT(Type *Ty, bool HandleUnknown){ |
| 348 | switch (Ty->getTypeID()) { |
| 349 | default: |
| 350 | return MVT::getVT(Ty, HandleUnknown); |
| 351 | case Type::TokenTyID: |
| 352 | return MVT::Untyped; |
| 353 | case Type::ByteTyID: |
| 354 | return getIntegerVT(Context&: Ty->getContext(), BitWidth: cast<ByteType>(Val: Ty)->getBitWidth()); |
| 355 | case Type::IntegerTyID: |
| 356 | return getIntegerVT(Context&: Ty->getContext(), BitWidth: cast<IntegerType>(Val: Ty)->getBitWidth()); |
| 357 | case Type::FixedVectorTyID: |
| 358 | case Type::ScalableVectorTyID: { |
| 359 | VectorType *VTy = cast<VectorType>(Val: Ty); |
| 360 | return getVectorVT(Context&: Ty->getContext(), |
| 361 | VT: getEVT(Ty: VTy->getElementType(), /*HandleUnknown=*/ false), |
| 362 | EC: VTy->getElementCount()); |
| 363 | } |
| 364 | } |
| 365 | } |
| 366 | |
| 367 | const fltSemantics &MVT::getFltSemantics() const { |
| 368 | switch (getScalarType().SimpleTy) { |
| 369 | default: llvm_unreachable("Unknown FP format" ); |
| 370 | case MVT::f16: return APFloat::IEEEhalf(); |
| 371 | case MVT::bf16: return APFloat::BFloat(); |
| 372 | case MVT::f32: return APFloat::IEEEsingle(); |
| 373 | case MVT::f64: return APFloat::IEEEdouble(); |
| 374 | case MVT::f80: return APFloat::x87DoubleExtended(); |
| 375 | case MVT::f128: return APFloat::IEEEquad(); |
| 376 | case MVT::ppcf128: return APFloat::PPCDoubleDouble(); |
| 377 | } |
| 378 | } |
| 379 | |
| 380 | const fltSemantics &EVT::getFltSemantics() const { |
| 381 | return getScalarType().getSimpleVT().getFltSemantics(); |
| 382 | } |
| 383 | |
| 384 | #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) |
| 385 | void MVT::dump() const { |
| 386 | print(dbgs()); |
| 387 | dbgs() << "\n" ; |
| 388 | } |
| 389 | #endif |
| 390 | |
| 391 | void MVT::print(raw_ostream &OS) const { |
| 392 | if (SimpleTy == INVALID_SIMPLE_VALUE_TYPE) |
| 393 | OS << "invalid" ; |
| 394 | else |
| 395 | OS << EVT(*this); |
| 396 | } |
| 397 | |