1 //===-- llvm/CodeGen/LowLevelType.cpp -------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 /// \file This file implements the more header-heavy bits of the LLT class to
10 /// avoid polluting users' namespaces.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/CodeGen/LowLevelType.h"
15 #include "llvm/ADT/APFloat.h"
16 #include "llvm/IR/DataLayout.h"
17 #include "llvm/IR/DerivedTypes.h"
18 #include "llvm/Support/raw_ostream.h"
21 LLT
llvm::getLLTForType(Type
&Ty
, const DataLayout
&DL
) {
22 if (auto VTy
= dyn_cast
<VectorType
>(&Ty
)) {
23 auto EC
= VTy
->getElementCount();
24 LLT ScalarTy
= getLLTForType(*VTy
->getElementType(), DL
);
27 return LLT::vector(EC
, ScalarTy
);
30 if (auto PTy
= dyn_cast
<PointerType
>(&Ty
)) {
31 unsigned AddrSpace
= PTy
->getAddressSpace();
32 return LLT::pointer(AddrSpace
, DL
.getPointerSizeInBits(AddrSpace
));
36 // Aggregates are no different from real scalars as far as GlobalISel is
38 auto SizeInBits
= DL
.getTypeSizeInBits(&Ty
);
39 assert(SizeInBits
!= 0 && "invalid zero-sized type");
40 return LLT::scalar(SizeInBits
);
46 MVT
llvm::getMVTForLLT(LLT Ty
) {
48 return MVT::getIntegerVT(Ty
.getSizeInBits());
50 return MVT::getVectorVT(
51 MVT::getIntegerVT(Ty
.getElementType().getSizeInBits()),
55 EVT
llvm::getApproximateEVTForLLT(LLT Ty
, const DataLayout
&DL
,
58 EVT EltVT
= getApproximateEVTForLLT(Ty
.getElementType(), DL
, Ctx
);
59 return EVT::getVectorVT(Ctx
, EltVT
, Ty
.getElementCount());
62 return EVT::getIntegerVT(Ctx
, Ty
.getSizeInBits());
65 LLT
llvm::getLLTForMVT(MVT Ty
) {
67 return LLT::scalar(Ty
.getSizeInBits());
69 return LLT::scalarOrVector(Ty
.getVectorElementCount(),
70 Ty
.getVectorElementType().getSizeInBits());
73 const llvm::fltSemantics
&llvm::getFltSemanticForLLT(LLT Ty
) {
74 assert(Ty
.isScalar() && "Expected a scalar type.");
75 switch (Ty
.getSizeInBits()) {
77 return APFloat::IEEEhalf();
79 return APFloat::IEEEsingle();
81 return APFloat::IEEEdouble();
83 return APFloat::IEEEquad();
85 llvm_unreachable("Invalid FP type size.");