LLVM 22.0.0git
ValueTypes.cpp
Go to the documentation of this file.
1//===----------- ValueTypes.cpp - Implementation of EVT methods -----------===//
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
10#include "llvm/ADT/APFloat.h"
13#include "llvm/IR/Type.h"
14#include "llvm/Support/Debug.h"
18using namespace llvm;
19
20EVT EVT::changeExtendedTypeToInteger() const {
21 assert(isExtended() && "Type is not extended!");
22 LLVMContext &Context = LLVMTy->getContext();
23 return getIntegerVT(Context, getSizeInBits());
24}
25
26EVT EVT::changeExtendedVectorElementTypeToInteger() const {
27 assert(isExtended() && "Type is not extended!");
28 LLVMContext &Context = LLVMTy->getContext();
29 EVT IntTy = getIntegerVT(Context, getScalarSizeInBits());
30 return getVectorVT(Context, IntTy, getVectorElementCount());
31}
32
33EVT EVT::changeExtendedVectorElementType(EVT EltVT) const {
34 assert(isExtended() && "Type is not extended!");
35 LLVMContext &Context = LLVMTy->getContext();
36 return getVectorVT(Context, EltVT, getVectorElementCount());
37}
38
39EVT EVT::getExtendedIntegerVT(LLVMContext &Context, unsigned BitWidth) {
40 EVT VT;
41 VT.LLVMTy = IntegerType::get(Context, BitWidth);
42 assert(VT.isExtended() && "Type is not extended!");
43 return VT;
44}
45
46EVT EVT::getExtendedVectorVT(LLVMContext &Context, EVT VT, unsigned NumElements,
47 bool IsScalable) {
48 EVT ResultVT;
49 ResultVT.LLVMTy =
50 VectorType::get(VT.getTypeForEVT(Context), NumElements, IsScalable);
51 assert(ResultVT.isExtended() && "Type is not extended!");
52 return ResultVT;
53}
54
55EVT EVT::getExtendedVectorVT(LLVMContext &Context, EVT VT, ElementCount EC) {
56 EVT ResultVT;
57 ResultVT.LLVMTy = VectorType::get(VT.getTypeForEVT(Context), EC);
58 assert(ResultVT.isExtended() && "Type is not extended!");
59 return ResultVT;
60}
61
62bool EVT::isExtendedFloatingPoint() const {
63 assert(isExtended() && "Type is not extended!");
64 return LLVMTy->isFPOrFPVectorTy();
65}
66
67bool EVT::isExtendedInteger() const {
68 assert(isExtended() && "Type is not extended!");
69 return LLVMTy->isIntOrIntVectorTy();
70}
71
72bool EVT::isExtendedScalarInteger() const {
73 assert(isExtended() && "Type is not extended!");
74 return LLVMTy->isIntegerTy();
75}
76
77bool EVT::isExtendedVector() const {
78 assert(isExtended() && "Type is not extended!");
79 return LLVMTy->isVectorTy();
80}
81
82bool EVT::isExtended16BitVector() const {
83 return isExtendedVector() &&
84 getExtendedSizeInBits() == TypeSize::getFixed(16);
85}
86
87bool EVT::isExtended32BitVector() const {
88 return isExtendedVector() &&
89 getExtendedSizeInBits() == TypeSize::getFixed(32);
90}
91
92bool EVT::isExtended64BitVector() const {
93 return isExtendedVector() &&
94 getExtendedSizeInBits() == TypeSize::getFixed(64);
95}
96
97bool EVT::isExtended128BitVector() const {
98 return isExtendedVector() &&
99 getExtendedSizeInBits() == TypeSize::getFixed(128);
100}
101
102bool EVT::isExtended256BitVector() const {
103 return isExtendedVector() &&
104 getExtendedSizeInBits() == TypeSize::getFixed(256);
105}
106
107bool EVT::isExtended512BitVector() const {
108 return isExtendedVector() &&
109 getExtendedSizeInBits() == TypeSize::getFixed(512);
110}
111
112bool EVT::isExtended1024BitVector() const {
113 return isExtendedVector() &&
114 getExtendedSizeInBits() == TypeSize::getFixed(1024);
115}
116
117bool EVT::isExtended2048BitVector() const {
118 return isExtendedVector() &&
119 getExtendedSizeInBits() == TypeSize::getFixed(2048);
120}
121
122bool EVT::isExtendedFixedLengthVector() const {
123 return isExtendedVector() && isa<FixedVectorType>(LLVMTy);
124}
125
126bool EVT::isExtendedScalableVector() const {
127 return isExtendedVector() && isa<ScalableVectorType>(LLVMTy);
128}
129
130EVT EVT::getExtendedVectorElementType() const {
131 assert(isExtended() && "Type is not extended!");
132 return EVT::getEVT(cast<VectorType>(LLVMTy)->getElementType());
133}
134
135unsigned EVT::getExtendedVectorNumElements() const {
136 assert(isExtended() && "Type is not extended!");
137 ElementCount EC = cast<VectorType>(LLVMTy)->getElementCount();
138 if (EC.isScalable()) {
140 << "The code that requested the fixed number of elements has made the "
141 "assumption that this vector is not scalable. This assumption was "
142 "not correct, and this may lead to broken code\n";
143 }
144 return EC.getKnownMinValue();
145}
146
147ElementCount EVT::getExtendedVectorElementCount() const {
148 assert(isExtended() && "Type is not extended!");
149 return cast<VectorType>(LLVMTy)->getElementCount();
150}
151
152TypeSize EVT::getExtendedSizeInBits() const {
153 assert(isExtended() && "Type is not extended!");
154 if (IntegerType *ITy = dyn_cast<IntegerType>(LLVMTy))
155 return TypeSize::getFixed(ITy->getBitWidth());
156 if (VectorType *VTy = dyn_cast<VectorType>(LLVMTy))
157 return VTy->getPrimitiveSizeInBits();
158 llvm_unreachable("Unrecognized extended type!");
159}
160
161/// getEVTString - This function returns value type as a string, e.g. "i32".
162std::string EVT::getEVTString() const {
163 switch (V.SimpleTy) {
164 default:
165 if (isRISCVVectorTuple()) {
166 unsigned Sz = getSizeInBits().getKnownMinValue();
167 unsigned NF = getRISCVVectorTupleNumFields();
168 unsigned MinNumElts = Sz / (NF * 8);
169 return "riscv_nxv" + utostr(MinNumElts) + "i8x" + utostr(NF);
170 }
171 if (isVector())
172 return (isScalableVector() ? "nxv" : "v") +
173 utostr(getVectorElementCount().getKnownMinValue()) +
175 if (isInteger())
176 return "i" + utostr(getSizeInBits());
177 if (isFloatingPoint())
178 return "f" + utostr(getSizeInBits());
179 llvm_unreachable("Invalid EVT!");
180 case MVT::bf16: return "bf16";
181 case MVT::ppcf128: return "ppcf128";
182 case MVT::isVoid: return "isVoid";
183 case MVT::Other: return "ch";
184 case MVT::Glue: return "glue";
185 case MVT::x86mmx: return "x86mmx";
186 case MVT::x86amx: return "x86amx";
187 case MVT::i64x8: return "i64x8";
188 case MVT::Metadata: return "Metadata";
189 case MVT::Untyped: return "Untyped";
190 case MVT::funcref: return "funcref";
191 case MVT::exnref: return "exnref";
192 case MVT::externref: return "externref";
193 case MVT::aarch64svcount:
194 return "aarch64svcount";
195 case MVT::spirvbuiltin:
196 return "spirvbuiltin";
197 case MVT::amdgpuBufferFatPointer:
198 return "amdgpuBufferFatPointer";
199 case MVT::amdgpuBufferStridedPointer:
200 return "amdgpuBufferStridedPointer";
201 case MVT::aarch64mfp8:
202 return "aarch64mfp8";
203 }
204}
205
206#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
207void EVT::dump() const {
208 print(dbgs());
209 dbgs() << "\n";
210}
211#endif
212
213/// getTypeForEVT - This method returns an LLVM type corresponding to the
214/// specified EVT. For integer types, this returns an unsigned type. Note
215/// that this will abort for types that cannot be represented.
217 // clang-format off
218 switch (V.SimpleTy) {
219 default:
220 assert(isExtended() && "Type is not extended!");
221 return LLVMTy;
222 case MVT::isVoid: return Type::getVoidTy(Context);
223 case MVT::x86mmx: return llvm::FixedVectorType::get(llvm::IntegerType::get(Context, 64), 1);
224 case MVT::aarch64svcount:
225 return TargetExtType::get(Context, "aarch64.svcount");
226 case MVT::aarch64mfp8:
228 case MVT::x86amx: return Type::getX86_AMXTy(Context);
229 case MVT::i64x8: return IntegerType::get(Context, 512);
230 case MVT::amdgpuBufferFatPointer: return IntegerType::get(Context, 160);
231 case MVT::amdgpuBufferStridedPointer: return IntegerType::get(Context, 192);
232 case MVT::externref: return Type::getWasm_ExternrefTy(Context);
233 case MVT::funcref: return Type::getWasm_FuncrefTy(Context);
234 case MVT::Metadata: return Type::getMetadataTy(Context);
235#define GET_VT_EVT(Ty, EVT) case MVT::Ty: return EVT;
236#include "llvm/CodeGen/GenVT.inc"
237#undef GET_VT_EVT
238 }
239 // clang-format on
240}
241
242/// Return the value type corresponding to the specified type.
243/// If HandleUnknown is true, unknown types are returned as Other, otherwise
244/// they are invalid.
245/// NB: This includes pointer types, which require a DataLayout to convert
246/// to a concrete value type.
247MVT MVT::getVT(Type *Ty, bool HandleUnknown){
248 assert(Ty != nullptr && "Invalid type");
249 switch (Ty->getTypeID()) {
250 default:
251 if (HandleUnknown) return MVT(MVT::Other);
252 llvm_unreachable("Unknown type!");
253 case Type::VoidTyID:
254 return MVT::isVoid;
256 return getIntegerVT(cast<IntegerType>(Ty)->getBitWidth());
257 case Type::HalfTyID: return MVT(MVT::f16);
258 case Type::BFloatTyID: return MVT(MVT::bf16);
259 case Type::FloatTyID: return MVT(MVT::f32);
260 case Type::DoubleTyID: return MVT(MVT::f64);
262 return MVT(MVT::f80);
263 case Type::TargetExtTyID: {
264 TargetExtType *TargetExtTy = cast<TargetExtType>(Ty);
265 if (TargetExtTy->getName() == "aarch64.svcount")
266 return MVT(MVT::aarch64svcount);
267 else if (TargetExtTy->getName().starts_with("spirv."))
268 return MVT(MVT::spirvbuiltin);
269 if (TargetExtTy->getName() == "riscv.vector.tuple") {
270 unsigned Sz = cast<ScalableVectorType>(TargetExtTy->getTypeParameter(0))
271 ->getMinNumElements() *
272 8;
273 unsigned NF = TargetExtTy->getIntParameter(0);
274
275 return MVT::getRISCVVectorTupleVT(Sz * NF, NF);
276 }
277 if (HandleUnknown)
278 return MVT(MVT::Other);
279 llvm_unreachable("Unknown target ext type!");
280 }
281 case Type::X86_AMXTyID: return MVT(MVT::x86amx);
282 case Type::FP128TyID: return MVT(MVT::f128);
283 case Type::PPC_FP128TyID: return MVT(MVT::ppcf128);
286 VectorType *VTy = cast<VectorType>(Ty);
287 return getVectorVT(
288 getVT(VTy->getElementType(), /*HandleUnknown=*/ false),
289 VTy->getElementCount());
290 }
291 }
292}
293
294/// getEVT - Return the value type corresponding to the specified type.
295/// If HandleUnknown is true, unknown types are returned as Other, otherwise
296/// they are invalid.
297/// NB: This includes pointer types, which require a DataLayout to convert
298/// to a concrete value type.
299EVT EVT::getEVT(Type *Ty, bool HandleUnknown){
300 switch (Ty->getTypeID()) {
301 default:
302 return MVT::getVT(Ty, HandleUnknown);
303 case Type::TokenTyID:
304 return MVT::Untyped;
306 return getIntegerVT(Ty->getContext(), cast<IntegerType>(Ty)->getBitWidth());
309 VectorType *VTy = cast<VectorType>(Ty);
310 return getVectorVT(Ty->getContext(),
311 getEVT(VTy->getElementType(), /*HandleUnknown=*/ false),
312 VTy->getElementCount());
313 }
314 }
315}
316
318 switch (getScalarType().SimpleTy) {
319 default: llvm_unreachable("Unknown FP format");
320 case MVT::f16: return APFloat::IEEEhalf();
321 case MVT::bf16: return APFloat::BFloat();
322 case MVT::f32: return APFloat::IEEEsingle();
323 case MVT::f64: return APFloat::IEEEdouble();
324 case MVT::f80: return APFloat::x87DoubleExtended();
325 case MVT::f128: return APFloat::IEEEquad();
326 case MVT::ppcf128: return APFloat::PPCDoubleDouble();
327 }
328}
329
332}
333
334#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
335void MVT::dump() const {
336 print(dbgs());
337 dbgs() << "\n";
338}
339#endif
340
343 OS << "invalid";
344 else
345 OS << EVT(*this).getEVTString();
346}
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...
raw_pwrite_stream & OS
This file contains some functions that are useful when dealing with strings.
static unsigned getBitWidth(Type *Ty, const DataLayout &DL)
Returns the bitwidth of the given scalar or pointer type.
static LLVM_ABI FixedVectorType * get(Type *ElementType, unsigned NumElts)
Definition: Type.cpp:803
Class to represent integer types.
Definition: DerivedTypes.h:42
static LLVM_ABI IntegerType * get(LLVMContext &C, unsigned NumBits)
This static method is the primary way of constructing an IntegerType.
Definition: Type.cpp:319
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:68
Machine Value Type.
LLVM_ABI void dump() const
Support for debugging, callable in GDB: VT.dump()
Definition: ValueTypes.cpp:335
@ INVALID_SIMPLE_VALUE_TYPE
SimpleValueType SimpleTy
constexpr MVT()=default
static MVT getRISCVVectorTupleVT(unsigned Sz, unsigned NFields)
static LLVM_ABI MVT getVT(Type *Ty, bool HandleUnknown=false)
Return the value type corresponding to the specified type.
Definition: ValueTypes.cpp:247
LLVM_ABI const fltSemantics & getFltSemantics() const
Returns an APFloat semantics tag appropriate for the value type.
Definition: ValueTypes.cpp:317
static MVT getVectorVT(MVT VT, unsigned NumElements)
static MVT getIntegerVT(unsigned BitWidth)
MVT getScalarType() const
If this is a vector, return the element type, otherwise return this.
LLVM_ABI void print(raw_ostream &OS) const
Implement operator<<.
Definition: ValueTypes.cpp:341
bool starts_with(StringRef Prefix) const
Check if this string starts with the given Prefix.
Definition: StringRef.h:269
Class to represent target extensions types, which are generally unintrospectable from target-independ...
Definition: DerivedTypes.h:781
static LLVM_ABI TargetExtType * get(LLVMContext &Context, StringRef Name, ArrayRef< Type * > Types={}, ArrayRef< unsigned > Ints={})
Return a target extension type having the specified name and optional type and integer parameters.
Definition: Type.cpp:908
Type * getTypeParameter(unsigned i) const
Definition: DerivedTypes.h:828
unsigned getIntParameter(unsigned i) const
Definition: DerivedTypes.h:837
StringRef getName() const
Return the name for this target extension type.
Definition: DerivedTypes.h:814
static constexpr TypeSize getFixed(ScalarTy ExactSize)
Definition: TypeSize.h:346
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
bool isVectorTy() const
True if this is an instance of VectorType.
Definition: Type.h:273
bool isIntOrIntVectorTy() const
Return true if this is an integer type or a vector of integer types.
Definition: Type.h:246
static LLVM_ABI Type * getWasm_ExternrefTy(LLVMContext &C)
@ X86_AMXTyID
AMX vectors (8192 bits, X86 specific)
Definition: Type.h:66
@ HalfTyID
16-bit floating point type
Definition: Type.h:56
@ TargetExtTyID
Target extension type.
Definition: Type.h:78
@ VoidTyID
type with no size
Definition: Type.h:63
@ ScalableVectorTyID
Scalable SIMD vector type.
Definition: Type.h:76
@ FloatTyID
32-bit floating point type
Definition: Type.h:58
@ IntegerTyID
Arbitrary bit width integers.
Definition: Type.h:70
@ FixedVectorTyID
Fixed width SIMD vector type.
Definition: Type.h:75
@ BFloatTyID
16-bit floating point type (7-bit significand)
Definition: Type.h:57
@ DoubleTyID
64-bit floating point type
Definition: Type.h:59
@ X86_FP80TyID
80-bit floating point type (X87)
Definition: Type.h:60
@ PPC_FP128TyID
128-bit floating point type (two 64-bits, PowerPC)
Definition: Type.h:62
@ TokenTyID
Tokens.
Definition: Type.h:67
@ FP128TyID
128-bit floating point type (112-bit significand)
Definition: Type.h:61
static LLVM_ABI Type * getX86_AMXTy(LLVMContext &C)
static LLVM_ABI Type * getVoidTy(LLVMContext &C)
LLVMContext & getContext() const
Return the LLVMContext in which this type was uniqued.
Definition: Type.h:128
bool isIntegerTy() const
True if this is an instance of IntegerType.
Definition: Type.h:240
TypeID getTypeID() const
Return the type id for the type.
Definition: Type.h:136
static LLVM_ABI Type * getMetadataTy(LLVMContext &C)
static LLVM_ABI Type * getWasm_FuncrefTy(LLVMContext &C)
bool isFPOrFPVectorTy() const
Return true if this is a FP type or a vector of FP.
Definition: Type.h:225
Base class of all SIMD vector types.
Definition: DerivedTypes.h:430
ElementCount getElementCount() const
Return an ElementCount instance to represent the (possibly scalable) number of elements in the vector...
Definition: DerivedTypes.h:695
static LLVM_ABI VectorType * get(Type *ElementType, ElementCount EC)
This static method is the primary way to construct an VectorType.
Type * getElementType() const
Definition: DerivedTypes.h:463
static LLVM_ABI raw_ostream & warning()
Convenience method for printing "warning: " to stderr.
Definition: WithColor.cpp:85
constexpr ScalarTy getKnownMinValue() const
Returns the minimum value this quantity can represent.
Definition: TypeSize.h:169
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:53
#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
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:207
constexpr unsigned BitWidth
Definition: BitmaskEnum.h:223
static LLVM_ABI const fltSemantics & IEEEsingle() LLVM_READNONE
Definition: APFloat.cpp:266
static LLVM_ABI const fltSemantics & PPCDoubleDouble() LLVM_READNONE
Definition: APFloat.cpp:269
static LLVM_ABI const fltSemantics & x87DoubleExtended() LLVM_READNONE
Definition: APFloat.cpp:289
static LLVM_ABI const fltSemantics & IEEEquad() LLVM_READNONE
Definition: APFloat.cpp:268
static LLVM_ABI const fltSemantics & IEEEdouble() LLVM_READNONE
Definition: APFloat.cpp:267
static LLVM_ABI const fltSemantics & IEEEhalf() LLVM_READNONE
Definition: APFloat.cpp:264
static LLVM_ABI const fltSemantics & BFloat() LLVM_READNONE
Definition: APFloat.cpp:265
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:74
bool isFloatingPoint() const
Return true if this is a FP or a vector FP type.
Definition: ValueTypes.h:147
ElementCount getVectorElementCount() const
Definition: ValueTypes.h:345
TypeSize getSizeInBits() const
Return the size of the specified value type in bits.
Definition: ValueTypes.h:368
unsigned getRISCVVectorTupleNumFields() const
Given a RISCV vector tuple type, return the num_fields.
Definition: ValueTypes.h:359
uint64_t getScalarSizeInBits() const
Definition: ValueTypes.h:380
static LLVM_ABI EVT getEVT(Type *Ty, bool HandleUnknown=false)
Return the value type corresponding to the specified type.
Definition: ValueTypes.cpp:299
MVT getSimpleVT() const
Return the SimpleValueType held in the specified simple EVT.
Definition: ValueTypes.h:311
LLVM_ABI void dump() const
Support for debugging, callable in GDB: VT.dump()
Definition: ValueTypes.cpp:207
static EVT getIntegerVT(LLVMContext &Context, unsigned BitWidth)
Returns the EVT that represents an integer with the given number of bits.
Definition: ValueTypes.h:65
bool isRISCVVectorTuple() const
Return true if this is a vector value type.
Definition: ValueTypes.h:179
LLVM_ABI std::string getEVTString() const
This function returns value type as a string, e.g. "i32".
Definition: ValueTypes.cpp:162
bool isVector() const
Return true if this is a vector value type.
Definition: ValueTypes.h:168
EVT getScalarType() const
If this is a vector type, return the element type, otherwise return this.
Definition: ValueTypes.h:318
LLVM_ABI Type * getTypeForEVT(LLVMContext &Context) const
This method returns an LLVM type corresponding to the specified EVT.
Definition: ValueTypes.cpp:216
bool isScalableVector() const
Return true if this is a vector type where the runtime length is machine dependent.
Definition: ValueTypes.h:174
EVT getVectorElementType() const
Given a vector type, return the type of each element.
Definition: ValueTypes.h:323
bool isExtended() const
Test if the given EVT is extended (as opposed to being simple).
Definition: ValueTypes.h:142
void print(raw_ostream &OS) const
Implement operator<<.
Definition: ValueTypes.h:491
LLVM_ABI const fltSemantics & getFltSemantics() const
Returns an APFloat semantics tag appropriate for the value type.
Definition: ValueTypes.cpp:330
bool isInteger() const
Return true if this is an integer or a vector integer type.
Definition: ValueTypes.h:152