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