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