LLVM 19.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 return LLT::scalar(SizeInBits);
40 }
41
42 if (Ty.isTokenTy())
43 return LLT::token();
44
45 return LLT();
46}
47
49 if (!Ty.isVector())
51
52 return MVT::getVectorVT(
54 Ty.getElementCount());
55}
56
58 LLVMContext &Ctx) {
59 if (Ty.isVector()) {
60 EVT EltVT = getApproximateEVTForLLT(Ty.getElementType(), DL, Ctx);
61 return EVT::getVectorVT(Ctx, EltVT, Ty.getElementCount());
62 }
63
64 return EVT::getIntegerVT(Ctx, Ty.getSizeInBits());
65}
66
68 if (!Ty.isVector())
69 return LLT::scalar(Ty.getSizeInBits());
70
73}
74
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}
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
This file declares a class to represent arbitrary precision floating point values and provide a varie...
Implement a low-level type suitable for MachineInstr level instruction selection.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
A parsed version of the target data layout string in and methods for querying it.
Definition: DataLayout.h:110
constexpr bool isScalar() const
Definition: LowLevelType.h:146
static constexpr LLT vector(ElementCount EC, unsigned ScalarSizeInBits)
Get a low-level vector of some number of elements and element width.
Definition: LowLevelType.h:64
static constexpr LLT scalar(unsigned SizeInBits)
Get a low-level scalar or aggregate "bag of bits".
Definition: LowLevelType.h:42
constexpr bool isVector() const
Definition: LowLevelType.h:148
static constexpr LLT pointer(unsigned AddressSpace, unsigned SizeInBits)
Get a low-level pointer in the given address space.
Definition: LowLevelType.h:57
constexpr TypeSize getSizeInBits() const
Returns the total size of the type. Must only be called on sized types.
Definition: LowLevelType.h:193
constexpr LLT getElementType() const
Returns the vector's element type. Only valid for vector types.
Definition: LowLevelType.h:290
constexpr ElementCount getElementCount() const
Definition: LowLevelType.h:184
static constexpr LLT token()
Get a low-level token; just a scalar with zero bits (or no size).
Definition: LowLevelType.h:49
static constexpr LLT scalarOrVector(ElementCount EC, LLT ScalarTy)
Definition: LowLevelType.h:124
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:67
Machine Value Type.
bool isVector() const
Return true if this is a vector value type.
TypeSize getSizeInBits() const
Returns the size of the specified MVT in bits.
ElementCount getVectorElementCount() const
static MVT getVectorVT(MVT VT, unsigned NumElements)
MVT getVectorElementType() const
static MVT getIntegerVT(unsigned BitWidth)
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
bool isScalableTargetExtTy() const
Return true if this is a target extension type with a scalable layout.
bool isSized(SmallPtrSetImpl< Type * > *Visited=nullptr) const
Return true if it makes sense to take the size of this type.
Definition: Type.h:302
bool isTokenTy() const
Return true if this is 'token'.
Definition: Type.h:225
#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.
Definition: AddressRanges.h:18
EVT getApproximateEVTForLLT(LLT Ty, const DataLayout &DL, LLVMContext &Ctx)
const llvm::fltSemantics & getFltSemanticForLLT(LLT Ty)
Get the appropriate floating point arithmetic semantic based on the bit size of the given scalar LLT.
MVT getMVTForLLT(LLT Ty)
Get a rough equivalent of an MVT for a given LLT.
LLT getLLTForMVT(MVT Ty)
Get a rough equivalent of an LLT for a given MVT.
LLT getLLTForType(Type &Ty, const DataLayout &DL)
Construct a low-level type based on an LLVM type.
Extended Value Type.
Definition: ValueTypes.h:34
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:73
static EVT getIntegerVT(LLVMContext &Context, unsigned BitWidth)
Returns the EVT that represents an integer with the given number of bits.
Definition: ValueTypes.h:64