| 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 | return LLT::scalar(SizeInBits); |
| 69 | } |
| 70 | |
| 71 | if (Ty.isTokenTy()) |
| 72 | return LLT::token(); |
| 73 | |
| 74 | return LLT(); |
| 75 | } |
| 76 | |
| 77 | MVT llvm::getMVTForLLT(LLT Ty) { |
| 78 | if (Ty.isVector()) |
| 79 | return MVT::getVectorVT(VT: getMVTForLLT(Ty: Ty.getElementType()), |
| 80 | EC: Ty.getElementCount()); |
| 81 | |
| 82 | if (Ty.isFloat()) { |
| 83 | if (Ty.isBFloat16()) |
| 84 | return MVT::bf16; |
| 85 | |
| 86 | if (Ty.isX86FP80()) |
| 87 | return MVT::f80; |
| 88 | |
| 89 | if (Ty.isPPCF128()) |
| 90 | return MVT::ppcf128; |
| 91 | |
| 92 | return MVT::getFloatingPointVT(BitWidth: Ty.getSizeInBits()); |
| 93 | } |
| 94 | |
| 95 | return MVT::getIntegerVT(BitWidth: Ty.getSizeInBits()); |
| 96 | } |
| 97 | |
| 98 | EVT llvm::getApproximateEVTForLLT(LLT Ty, LLVMContext &Ctx) { |
| 99 | if (Ty.isVector()) { |
| 100 | EVT EltVT = getApproximateEVTForLLT(Ty: Ty.getElementType(), Ctx); |
| 101 | return EVT::getVectorVT(Context&: Ctx, VT: EltVT, EC: Ty.getElementCount()); |
| 102 | } |
| 103 | |
| 104 | return EVT::getIntegerVT(Context&: Ctx, BitWidth: Ty.getSizeInBits()); |
| 105 | } |
| 106 | |
| 107 | LLT llvm::getLLTForMVT(MVT VT) { return LLT(VT); } |
| 108 | |
| 109 | const llvm::fltSemantics &llvm::getFltSemanticForLLT(LLT Ty) { |
| 110 | assert((Ty.isAnyScalar() || Ty.isFloat()) && |
| 111 | "Expected a any scalar or float type." ); |
| 112 | |
| 113 | // Any scalar type always matches IEEE format |
| 114 | // FIXME: Remove this handling |
| 115 | if (Ty.isAnyScalar()) { |
| 116 | switch (Ty.getSizeInBits()) { |
| 117 | default: |
| 118 | llvm_unreachable("Invalid FP type size." ); |
| 119 | case 16: |
| 120 | return APFloat::IEEEhalf(); |
| 121 | case 32: |
| 122 | return APFloat::IEEEsingle(); |
| 123 | case 64: |
| 124 | return APFloat::IEEEdouble(); |
| 125 | case 128: |
| 126 | return APFloat::IEEEquad(); |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | return APFloat::EnumToSemantics(S: Ty.getFpSemantics()); |
| 131 | } |
| 132 | |