LLVM 24.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();
24}
25
26EVT EVT::changeExtendedVectorElementTypeToInteger() const {
27 assert(isExtended() && "Type is not extended!");
28 LLVMContext &Context = LLVMTy->getContext();
31}
32
33EVT EVT::changeExtendedVectorElementType(EVT EltVT) const {
34 assert(isExtended() && "Type is not extended!");
35 LLVMContext &Context = LLVMTy->getContext();
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".
162void EVT::print(raw_ostream &OS) 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 OS << "riscv_nxv" << MinNumElts << "i8x" << NF;
170 return;
171 }
172 if (isVector()) {
173 OS << (isScalableVector() ? "nxv" : "v")
176 return;
177 }
178 if (isInteger()) {
179 OS << "i" << getSizeInBits();
180 return;
181 }
182 if (isFloatingPoint()) {
183 OS << "f" << getSizeInBits();
184 return;
185 }
186 if (isCheriCapability()) {
187 OS << "c" << getSizeInBits();
188 return;
189 }
190 llvm_unreachable("Invalid EVT!");
191 case MVT::bf16:
192 OS << "bf16";
193 return;
194 case MVT::ppcf128:
195 OS << "ppcf128";
196 return;
197 case MVT::isVoid:
198 OS << "isVoid";
199 return;
200 case MVT::Other:
201 OS << "ch";
202 return;
203 case MVT::Glue:
204 OS << "glue";
205 return;
206 case MVT::x86mmx:
207 OS << "x86mmx";
208 return;
209 case MVT::x86amx:
210 OS << "x86amx";
211 return;
212 case MVT::i64x8:
213 OS << "i64x8";
214 return;
215 case MVT::Metadata:
216 OS << "Metadata";
217 return;
218 case MVT::Untyped:
219 OS << "Untyped";
220 return;
221 case MVT::funcref:
222 OS << "funcref";
223 return;
224 case MVT::exnref:
225 OS << "exnref";
226 return;
227 case MVT::externref:
228 OS << "externref";
229 return;
230 case MVT::aarch64svcount:
231 OS << "aarch64svcount";
232 return;
233 case MVT::spirvbuiltin:
234 OS << "spirvbuiltin";
235 return;
236 case MVT::amdgpuBufferFatPointer:
237 OS << "amdgpuBufferFatPointer";
238 return;
239 case MVT::amdgpuBufferStridedPointer:
240 OS << "amdgpuBufferStridedPointer";
241 return;
242 case MVT::aarch64mfp8:
243 OS << "aarch64mfp8";
244 return;
245 }
246}
247
248#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
249void EVT::dump() const {
250 print(dbgs());
251 dbgs() << "\n";
252}
253#endif
254
255/// getTypeForEVT - This method returns an LLVM type corresponding to the
256/// specified EVT. For integer types, this returns an unsigned type. Note
257/// that this will abort for types that cannot be represented.
259 // clang-format off
260 switch (V.SimpleTy) {
261 default:
262 assert(isExtended() && "Type is not extended!");
263 return LLVMTy;
264 case MVT::isVoid: return Type::getVoidTy(Context);
265 case MVT::x86mmx: return llvm::FixedVectorType::get(llvm::IntegerType::get(Context, 64), 1);
266 case MVT::aarch64svcount:
267 return TargetExtType::get(Context, "aarch64.svcount");
268 case MVT::aarch64mfp8:
269 return FixedVectorType::get(IntegerType::get(Context, 8), 1);
270 case MVT::x86amx: return Type::getX86_AMXTy(Context);
271 case MVT::i64x8: return IntegerType::get(Context, 512);
272 case MVT::amdgpuBufferFatPointer: return IntegerType::get(Context, 160);
273 case MVT::amdgpuBufferStridedPointer: return IntegerType::get(Context, 192);
274 case MVT::externref: return Type::getWasm_ExternrefTy(Context);
275 case MVT::funcref: return Type::getWasm_FuncrefTy(Context);
276 case MVT::Metadata: return Type::getMetadataTy(Context);
277#define GET_VT_EVT(Ty, EVT) case MVT::Ty: return EVT;
278#include "llvm/CodeGen/GenVT.inc"
279#undef GET_VT_EVT
280 }
281 // clang-format on
282}
283
284/// Return the value type corresponding to the specified type.
285/// If HandleUnknown is true, unknown types are returned as Other, otherwise
286/// they are invalid.
287/// NB: This includes pointer types, which require a DataLayout to convert
288/// to a concrete value type.
289MVT MVT::getVT(Type *Ty, bool HandleUnknown){
290 assert(Ty != nullptr && "Invalid type");
291 switch (Ty->getTypeID()) {
292 default:
293 if (HandleUnknown) return MVT(MVT::Other);
294 llvm_unreachable("Unknown type!");
295 case Type::VoidTyID:
296 return MVT::isVoid;
297 case Type::ByteTyID:
301 case Type::HalfTyID: return MVT(MVT::f16);
302 case Type::BFloatTyID: return MVT(MVT::bf16);
303 case Type::FloatTyID: return MVT(MVT::f32);
304 case Type::DoubleTyID: return MVT(MVT::f64);
306 return MVT(MVT::f80);
307 case Type::TargetExtTyID: {
308 TargetExtType *TargetExtTy = cast<TargetExtType>(Ty);
309 if (TargetExtTy->getName() == "aarch64.svcount")
310 return MVT(MVT::aarch64svcount);
311 else if (TargetExtTy->getName() == "wasm.externref")
312 return MVT(MVT::externref);
313 else if (TargetExtTy->getName() == "wasm.funcref")
314 return MVT(MVT::funcref);
315 else if (TargetExtTy->getName().starts_with("spirv."))
316 return MVT(MVT::spirvbuiltin);
317 if (TargetExtTy->getName() == "riscv.vector.tuple") {
318 unsigned Sz = cast<ScalableVectorType>(TargetExtTy->getTypeParameter(0))
319 ->getMinNumElements() *
320 8;
321 unsigned NF = TargetExtTy->getIntParameter(0);
322
323 return MVT::getRISCVVectorTupleVT(Sz * NF, NF);
324 }
325 if (HandleUnknown)
326 return MVT(MVT::Other);
327 llvm_unreachable("Unknown target ext type!");
328 }
329 case Type::X86_AMXTyID: return MVT(MVT::x86amx);
330 case Type::FP128TyID: return MVT(MVT::f128);
331 case Type::PPC_FP128TyID: return MVT(MVT::ppcf128);
334 VectorType *VTy = cast<VectorType>(Ty);
335 return getVectorVT(
336 getVT(VTy->getElementType(), /*HandleUnknown=*/ false),
337 VTy->getElementCount());
338 }
339 }
340}
341
342/// getEVT - Return the value type corresponding to the specified type.
343/// If HandleUnknown is true, unknown types are returned as Other, otherwise
344/// they are invalid.
345/// NB: This includes pointer types, which require a DataLayout to convert
346/// to a concrete value type.
347EVT EVT::getEVT(Type *Ty, bool HandleUnknown){
348 switch (Ty->getTypeID()) {
349 default:
350 return MVT::getVT(Ty, HandleUnknown);
351 case Type::TokenTyID:
352 return MVT::Untyped;
353 case Type::ByteTyID:
354 return getIntegerVT(Ty->getContext(), cast<ByteType>(Ty)->getBitWidth());
356 return getIntegerVT(Ty->getContext(), cast<IntegerType>(Ty)->getBitWidth());
359 VectorType *VTy = cast<VectorType>(Ty);
360 return getVectorVT(Ty->getContext(),
361 getEVT(VTy->getElementType(), /*HandleUnknown=*/ false),
362 VTy->getElementCount());
363 }
364 }
365}
366
368 switch (getScalarType().SimpleTy) {
369 default: llvm_unreachable("Unknown FP format");
370 case MVT::f16: return APFloat::IEEEhalf();
371 case MVT::bf16: return APFloat::BFloat();
372 case MVT::f32: return APFloat::IEEEsingle();
373 case MVT::f64: return APFloat::IEEEdouble();
374 case MVT::f80: return APFloat::x87DoubleExtended();
375 case MVT::f128: return APFloat::IEEEquad();
376 case MVT::ppcf128: return APFloat::PPCDoubleDouble();
377 }
378}
379
383
384#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
385void MVT::dump() const {
386 print(dbgs());
387 dbgs() << "\n";
388}
389#endif
390
391void MVT::print(raw_ostream &OS) const {
393 OS << "invalid";
394 else
395 OS << EVT(*this);
396}
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...
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 const fltSemantics & IEEEsingle()
Definition APFloat.h:304
static const fltSemantics & BFloat()
Definition APFloat.h:303
static const fltSemantics & IEEEquad()
Definition APFloat.h:306
static const fltSemantics & IEEEdouble()
Definition APFloat.h:305
static const fltSemantics & x87DoubleExtended()
Definition APFloat.h:307
static const fltSemantics & IEEEhalf()
Definition APFloat.h:302
static const fltSemantics & PPCDoubleDouble()
Definition APFloat.h:310
static LLVM_ABI FixedVectorType * get(Type *ElementType, unsigned NumElts)
Definition Type.cpp:843
static LLVM_ABI IntegerType * get(LLVMContext &C, unsigned NumBits)
This static method is the primary way of constructing an IntegerType.
Definition Type.cpp:338
This is an important class for using LLVM in a threaded context.
Definition LLVMContext.h:76
LLVM_ABI void dump() const
Support for debugging, callable in GDB: VT.dump()
@ 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.
LLVM_ABI const fltSemantics & getFltSemantics() const
Returns an APFloat semantics tag appropriate for the value type.
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<<.
bool starts_with(StringRef Prefix) const
Check if this string starts with the given Prefix.
Definition StringRef.h:258
Class to represent target extensions types, which are generally unintrospectable from target-independ...
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:936
Type * getTypeParameter(unsigned i) const
unsigned getIntParameter(unsigned i) const
StringRef getName() const
Return the name for this target extension type.
static constexpr TypeSize getFixed(ScalarTy ExactSize)
Definition TypeSize.h:352
The instances of the Type class are immutable: once they are created, they are never changed.
Definition Type.h:46
static LLVM_ABI Type * getX86_AMXTy(LLVMContext &C)
Definition Type.cpp:283
static LLVM_ABI Type * getWasm_ExternrefTy(LLVMContext &C)
Definition Type.cpp:326
static LLVM_ABI Type * getMetadataTy(LLVMContext &C)
Definition Type.cpp:278
@ X86_AMXTyID
AMX vectors (8192 bits, X86 specific)
Definition Type.h:67
@ HalfTyID
16-bit floating point type
Definition Type.h:57
@ TargetExtTyID
Target extension type.
Definition Type.h:80
@ VoidTyID
type with no size
Definition Type.h:64
@ ScalableVectorTyID
Scalable SIMD vector type.
Definition Type.h:78
@ FloatTyID
32-bit floating point type
Definition Type.h:59
@ IntegerTyID
Arbitrary bit width integers.
Definition Type.h:71
@ FixedVectorTyID
Fixed width SIMD vector type.
Definition Type.h:77
@ 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
@ TokenTyID
Tokens.
Definition Type.h:68
@ ByteTyID
Arbitrary bit width bytes.
Definition Type.h:72
@ FP128TyID
128-bit floating point type (112-bit significand)
Definition Type.h:62
static LLVM_ABI Type * getVoidTy(LLVMContext &C)
Definition Type.cpp:272
static LLVM_ABI Type * getWasm_FuncrefTy(LLVMContext &C)
Definition Type.cpp:330
Base class of all SIMD vector types.
ElementCount getElementCount() const
Return an ElementCount instance to represent the (possibly scalable) number of elements in the vector...
static LLVM_ABI VectorType * get(Type *ElementType, ElementCount EC)
This static method is the primary way to construct an VectorType.
Type * getElementType() const
static LLVM_ABI raw_ostream & warning()
Convenience method for printing "warning: " to stderr.
Definition WithColor.cpp:86
constexpr ScalarTy getKnownMinValue() const
Returns the minimum value this quantity can represent.
Definition TypeSize.h:166
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.
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 raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:209
bool isa(const From &Val)
isa<X> - Return true if the parameter to the template is an instance of one of the template type argu...
Definition Casting.h:547
constexpr unsigned BitWidth
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:559
Extended Value Type.
Definition ValueTypes.h:35
constexpr EVT()=default
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
bool isFloatingPoint() const
Return true if this is a FP or a vector FP type.
Definition ValueTypes.h:155
ElementCount getVectorElementCount() const
Definition ValueTypes.h:373
TypeSize getSizeInBits() const
Return the size of the specified value type in bits.
Definition ValueTypes.h:396
bool isCheriCapability() const
Return true if this is a capability type.
Definition ValueTypes.h:255
unsigned getRISCVVectorTupleNumFields() const
Given a RISCV vector tuple type, return the num_fields.
Definition ValueTypes.h:387
uint64_t getScalarSizeInBits() const
Definition ValueTypes.h:408
static LLVM_ABI EVT getEVT(Type *Ty, bool HandleUnknown=false)
Return the value type corresponding to the specified type.
MVT getSimpleVT() const
Return the SimpleValueType held in the specified simple EVT.
Definition ValueTypes.h:339
LLVM_ABI void dump() const
Support for debugging, callable in GDB: VT.dump()
static EVT getIntegerVT(LLVMContext &Context, unsigned BitWidth)
Returns the EVT that represents an integer with the given number of bits.
Definition ValueTypes.h:61
bool isRISCVVectorTuple() const
Return true if this is a vector value type.
Definition ValueTypes.h:197
bool isVector() const
Return true if this is a vector value type.
Definition ValueTypes.h:176
EVT getScalarType() const
If this is a vector type, return the element type, otherwise return this.
Definition ValueTypes.h:346
LLVM_ABI Type * getTypeForEVT(LLVMContext &Context) const
This method returns an LLVM type corresponding to the specified EVT.
bool isScalableVector() const
Return true if this is a vector type where the runtime length is machine dependent.
Definition ValueTypes.h:187
EVT getVectorElementType() const
Given a vector type, return the type of each element.
Definition ValueTypes.h:351
bool isExtended() const
Test if the given EVT is extended (as opposed to being simple).
Definition ValueTypes.h:150
LLVM_ABI const fltSemantics & getFltSemantics() const
Returns an APFloat semantics tag appropriate for the value type.
LLVM_ABI void print(raw_ostream &OS) const
Implement operator<<.
bool isInteger() const
Return true if this is an integer or a vector integer type.
Definition ValueTypes.h:160