| 1 | //===-- llvm/CodeGen/LowLevelTypeUtils.cpp --------------------------------===// |
| 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 | /// \file This file implements the more header-heavy bits of the LLT class to |
| 10 | /// avoid polluting users' namespaces. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "llvm/CodeGen/LowLevelTypeUtils.h" |
| 15 | #include "llvm/ADT/APFloat.h" |
| 16 | #include "llvm/IR/DataLayout.h" |
| 17 | #include "llvm/IR/DerivedTypes.h" |
| 18 | using namespace llvm; |
| 19 | |
| 20 | LLT llvm::getLLTForType(Type &Ty, const DataLayout &DL) { |
| 21 | if (auto *VTy = dyn_cast<VectorType>(Val: &Ty)) { |
| 22 | auto EC = VTy->getElementCount(); |
| 23 | LLT ScalarTy = getLLTForType(Ty&: *VTy->getElementType(), DL); |
| 24 | if (EC.isScalar()) |
| 25 | return ScalarTy; |
| 26 | return LLT::vector(EC, ScalarTy); |
| 27 | } |
| 28 | |
| 29 | if (auto *PTy = dyn_cast<PointerType>(Val: &Ty)) { |
| 30 | unsigned AddrSpace = PTy->getAddressSpace(); |
| 31 | return LLT::pointer(AddressSpace: AddrSpace, SizeInBits: DL.getPointerSizeInBits(AS: AddrSpace)); |
| 32 | } |
| 33 | |
| 34 | if (Ty.isSized() && !Ty.isScalableTargetExtTy()) { |
| 35 | // Aggregates are no different from real scalars as far as GlobalISel is |
| 36 | // concerned. |
| 37 | auto SizeInBits = DL.getTypeSizeInBits(Ty: &Ty); |
| 38 | assert(SizeInBits != 0 && "invalid zero-sized type" ); |
| 39 | |
| 40 | // Return simple scalar |
| 41 | if (!LLT::getUseExtended()) |
| 42 | return LLT::scalar(SizeInBits); |
| 43 | |
| 44 | // Choose more precise LLT variant |
| 45 | if (Ty.isFloatingPointTy()) |
| 46 | switch (Ty.getTypeID()) { |
| 47 | default: |
| 48 | llvm_unreachable("Unhandled LLVM IR floating point type" ); |
| 49 | case Type::HalfTyID: |
| 50 | return LLT::float16(); |
| 51 | case Type::BFloatTyID: |
| 52 | return LLT::bfloat16(); |
| 53 | case Type::FloatTyID: |
| 54 | return LLT::float32(); |
| 55 | case Type::DoubleTyID: |
| 56 | return LLT::float64(); |
| 57 | case Type::X86_FP80TyID: |
| 58 | return LLT::x86fp80(); |
| 59 | case Type::FP128TyID: |
| 60 | return LLT::float128(); |
| 61 | case Type::PPC_FP128TyID: |
| 62 | return LLT::ppcf128(); |
| 63 | } |
| 64 | |
| 65 | if (Ty.isIntegerTy()) |
| 66 | return LLT::integer(SizeInBits); |
| 67 | |
| 68 | // Byte values share an LLT with same-sized integers. The byte type's |
| 69 | // per-bit poison and pointer-provenance properties are mid-end concepts |
| 70 | // that have no MIR-level representation; matching SDAG (which lowers |
| 71 | // byte EVT to integer EVT) keeps verifier and target rules unified. |
| 72 | if (Ty.isByteTy()) |
| 73 | return LLT::integer(SizeInBits); |
| 74 | |
| 75 | return LLT::scalar(SizeInBits); |
| 76 | } |
| 77 | |
| 78 | if (Ty.isTokenTy()) |
| 79 | return LLT::token(); |
| 80 | |
| 81 | return LLT(); |
| 82 | } |
| 83 | |
| 84 | MVT llvm::getMVTForLLT(LLT Ty) { |
| 85 | if (Ty.isVector()) |
| 86 | return MVT::getVectorVT(VT: getMVTForLLT(Ty: Ty.getElementType()), |
| 87 | EC: Ty.getElementCount()); |
| 88 | |
| 89 | if (Ty.isFloat()) { |
| 90 | if (Ty.isBFloat16()) |
| 91 | return MVT::bf16; |
| 92 | |
| 93 | if (Ty.isX86FP80()) |
| 94 | return MVT::f80; |
| 95 | |
| 96 | if (Ty.isPPCF128()) |
| 97 | return MVT::ppcf128; |
| 98 | |
| 99 | return MVT::getFloatingPointVT(BitWidth: Ty.getSizeInBits()); |
| 100 | } |
| 101 | |
| 102 | return MVT::getIntegerVT(BitWidth: Ty.getSizeInBits()); |
| 103 | } |
| 104 | |
| 105 | EVT llvm::getApproximateEVTForLLT(LLT Ty, LLVMContext &Ctx) { |
| 106 | if (Ty.isVector()) { |
| 107 | EVT EltVT = getApproximateEVTForLLT(Ty: Ty.getElementType(), Ctx); |
| 108 | return EVT::getVectorVT(Context&: Ctx, VT: EltVT, EC: Ty.getElementCount()); |
| 109 | } |
| 110 | |
| 111 | return EVT::getIntegerVT(Context&: Ctx, BitWidth: Ty.getSizeInBits()); |
| 112 | } |
| 113 | |
| 114 | LLT llvm::getLLTForMVT(MVT VT) { return LLT(VT); } |
| 115 | |
| 116 | const llvm::fltSemantics &llvm::getFltSemanticForLLT(LLT Ty) { |
| 117 | assert((Ty.isAnyScalar() || Ty.isFloat()) && |
| 118 | "Expected a any scalar or float type." ); |
| 119 | |
| 120 | // Any scalar type always matches IEEE format |
| 121 | // FIXME: Remove this handling |
| 122 | if (Ty.isAnyScalar()) { |
| 123 | switch (Ty.getSizeInBits()) { |
| 124 | default: |
| 125 | llvm_unreachable("Invalid FP type size." ); |
| 126 | case 16: |
| 127 | return APFloat::IEEEhalf(); |
| 128 | case 32: |
| 129 | return APFloat::IEEEsingle(); |
| 130 | case 64: |
| 131 | return APFloat::IEEEdouble(); |
| 132 | case 128: |
| 133 | return APFloat::IEEEquad(); |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | return APFloat::EnumToSemantics(S: Ty.getFpSemantics()); |
| 138 | } |
| 139 | |