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"
18using namespace llvm;
19
20LLT 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 return LLT::scalar(SizeInBits);
40 }
41
42 if (Ty.isTokenTy())
43 return LLT::token();
44
45 return LLT();
46}
47
48MVT llvm::getMVTForLLT(LLT Ty) {
49 if (!Ty.isVector())
50 return MVT::getIntegerVT(BitWidth: Ty.getSizeInBits());
51
52 return MVT::getVectorVT(
53 VT: MVT::getIntegerVT(BitWidth: Ty.getElementType().getSizeInBits()),
54 EC: Ty.getElementCount());
55}
56
57EVT llvm::getApproximateEVTForLLT(LLT Ty, const DataLayout &DL,
58 LLVMContext &Ctx) {
59 if (Ty.isVector()) {
60 EVT EltVT = getApproximateEVTForLLT(Ty: Ty.getElementType(), DL, Ctx);
61 return EVT::getVectorVT(Context&: Ctx, VT: EltVT, EC: Ty.getElementCount());
62 }
63
64 return EVT::getIntegerVT(Context&: Ctx, BitWidth: Ty.getSizeInBits());
65}
66
67LLT llvm::getLLTForMVT(MVT Ty) {
68 if (!Ty.isVector())
69 return LLT::scalar(SizeInBits: Ty.getSizeInBits());
70
71 return LLT::scalarOrVector(EC: Ty.getVectorElementCount(),
72 ScalarSize: Ty.getVectorElementType().getSizeInBits());
73}
74
75const llvm::fltSemantics &llvm::getFltSemanticForLLT(LLT Ty) {
76 assert(Ty.isScalar() && "Expected a scalar type.");
77 switch (Ty.getSizeInBits()) {
78 case 16:
79 return APFloat::IEEEhalf();
80 case 32:
81 return APFloat::IEEEsingle();
82 case 64:
83 return APFloat::IEEEdouble();
84 case 128:
85 return APFloat::IEEEquad();
86 }
87 llvm_unreachable("Invalid FP type size.");
88}
89