LLVM 23.0.0git
LowLevelTypeUtils.cpp
Go to the documentation of this file.
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
15#include "llvm/ADT/APFloat.h"
16#include "llvm/IR/DataLayout.h"
18using namespace llvm;
19
21 if (auto *VTy = dyn_cast<VectorType>(&Ty)) {
22 auto EC = VTy->getElementCount();
23 LLT ScalarTy = getLLTForType(*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>(&Ty)) {
30 unsigned AddrSpace = PTy->getAddressSpace();
31 return LLT::pointer(AddrSpace, DL.getPointerSizeInBits(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);
38 assert(SizeInBits != 0 && "invalid zero-sized type");
39
40 // Return simple scalar
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();
52 return LLT::bfloat16();
53 case Type::FloatTyID:
54 return LLT::float32();
56 return LLT::float64();
58 return LLT::x86fp80();
59 case Type::FP128TyID:
60 return LLT::float128();
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
78 if (Ty.isVector())
79 return MVT::getVectorVT(getMVTForLLT(Ty.getElementType()),
80 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(Ty.getSizeInBits());
93 }
94
95 return MVT::getIntegerVT(Ty.getSizeInBits());
96}
97
99 if (Ty.isVector()) {
100 EVT EltVT = getApproximateEVTForLLT(Ty.getElementType(), Ctx);
101 return EVT::getVectorVT(Ctx, EltVT, Ty.getElementCount());
102 }
103
104 return EVT::getIntegerVT(Ctx, Ty.getSizeInBits());
105}
106
107LLT llvm::getLLTForMVT(MVT VT) { return LLT(VT); }
108
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(Ty.getFpSemantics());
131}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
This file declares a class to represent arbitrary precision floating point values and provide a varie...
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
Implement a low-level type suitable for MachineInstr level instruction selection.
static const fltSemantics & IEEEsingle()
Definition APFloat.h:296
static LLVM_ABI const llvm::fltSemantics & EnumToSemantics(Semantics S)
Definition APFloat.cpp:98
static const fltSemantics & IEEEquad()
Definition APFloat.h:298
static const fltSemantics & IEEEdouble()
Definition APFloat.h:297
static const fltSemantics & IEEEhalf()
Definition APFloat.h:294
A parsed version of the target data layout string in and methods for querying it.
Definition DataLayout.h:64
static constexpr LLT float64()
Get a 64-bit IEEE double value.
static constexpr LLT x86fp80()
Get a 80-bit X86 floating point value.
static bool getUseExtended()
static constexpr LLT vector(ElementCount EC, unsigned ScalarSizeInBits)
Get a low-level vector of some number of elements and element width.
static constexpr LLT scalar(unsigned SizeInBits)
Get a low-level scalar or aggregate "bag of bits".
static constexpr LLT float128()
Get a 128-bit IEEE quad value.
static constexpr LLT pointer(unsigned AddressSpace, unsigned SizeInBits)
Get a low-level pointer in the given address space.
static constexpr LLT ppcf128()
Get a 128-bit PowerPC double double value.
static constexpr LLT float16()
Get a 16-bit IEEE half value.
static constexpr LLT token()
Get a low-level token; just a scalar with zero bits (or no size).
static LLT integer(unsigned SizeInBits)
static constexpr LLT bfloat16()
static constexpr LLT float32()
Get a 32-bit IEEE float value.
This is an important class for using LLVM in a threaded context.
Definition LLVMContext.h:68
Machine Value Type.
static MVT getFloatingPointVT(unsigned BitWidth)
static MVT getVectorVT(MVT VT, unsigned NumElements)
static MVT getIntegerVT(unsigned BitWidth)
The instances of the Type class are immutable: once they are created, they are never changed.
Definition Type.h:46
@ HalfTyID
16-bit floating point type
Definition Type.h:57
@ FloatTyID
32-bit floating point type
Definition Type.h:59
@ BFloatTyID
16-bit floating point type (7-bit significand)
Definition Type.h:58
@ DoubleTyID
64-bit floating point type
Definition Type.h:60
@ X86_FP80TyID
80-bit floating point type (X87)
Definition Type.h:61
@ PPC_FP128TyID
128-bit floating point type (two 64-bits, PowerPC)
Definition Type.h:63
@ FP128TyID
128-bit floating point type (112-bit significand)
Definition Type.h:62
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
This is an optimization pass for GlobalISel generic memory operations.
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:643
LLVM_ABI const llvm::fltSemantics & getFltSemanticForLLT(LLT Ty)
Get the appropriate floating point arithmetic semantic based on the bit size of the given scalar LLT.
LLVM_ABI MVT getMVTForLLT(LLT Ty)
Get a rough equivalent of an MVT for a given LLT.
LLVM_ABI LLT getLLTForMVT(MVT Ty)
Get a rough equivalent of an LLT for a given MVT.
LLVM_ABI EVT getApproximateEVTForLLT(LLT Ty, LLVMContext &Ctx)
LLVM_ABI LLT getLLTForType(Type &Ty, const DataLayout &DL)
Construct a low-level type based on an LLVM type.
Extended Value Type.
Definition ValueTypes.h:35
static EVT getVectorVT(LLVMContext &Context, EVT VT, unsigned NumElements, bool IsScalable=false)
Returns the EVT that represents a vector NumElements in length, where each element is of type VT.
Definition ValueTypes.h:70
static EVT getIntegerVT(LLVMContext &Context, unsigned BitWidth)
Returns the EVT that represents an integer with the given number of bits.
Definition ValueTypes.h:61