1 //== llvm/Support/LowLevelTypeImpl.h --------------------------- -*- C++ -*-==//
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
7 //===----------------------------------------------------------------------===//
9 /// Implement a low-level type suitable for MachineInstr level instruction
12 /// For a type attached to a MachineInstr, we only care about 2 details: total
13 /// size and the number of vector lanes (if any). Accordingly, there are 4
14 /// possible valid type-kinds:
16 /// * `sN` for scalars and aggregates
17 /// * `<N x sM>` for vectors, which must have at least 2 elements.
18 /// * `pN` for pointers
20 /// Other information required for correct selection is expected to be carried
21 /// by the opcode, or non-type flags. For example the distinction between G_ADD
22 /// and G_FADD for int/float or fast-math flags.
24 //===----------------------------------------------------------------------===//
26 #ifndef LLVM_SUPPORT_LOWLEVELTYPEIMPL_H
27 #define LLVM_SUPPORT_LOWLEVELTYPEIMPL_H
29 #include "llvm/ADT/DenseMapInfo.h"
30 #include "llvm/Support/MachineValueType.h"
41 /// Get a low-level scalar or aggregate "bag of bits".
42 static LLT
scalar(unsigned SizeInBits
) {
43 assert(SizeInBits
> 0 && "invalid scalar size");
44 return LLT
{/*isPointer=*/false, /*isVector=*/false, /*NumElements=*/0,
45 SizeInBits
, /*AddressSpace=*/0};
48 /// Get a low-level pointer in the given address space.
49 static LLT
pointer(unsigned AddressSpace
, unsigned SizeInBits
) {
50 assert(SizeInBits
> 0 && "invalid pointer size");
51 return LLT
{/*isPointer=*/true, /*isVector=*/false, /*NumElements=*/0,
52 SizeInBits
, AddressSpace
};
55 /// Get a low-level vector of some number of elements and element width.
56 /// \p NumElements must be at least 2.
57 static LLT
vector(uint16_t NumElements
, unsigned ScalarSizeInBits
) {
58 assert(NumElements
> 1 && "invalid number of vector elements");
59 assert(ScalarSizeInBits
> 0 && "invalid vector element size");
60 return LLT
{/*isPointer=*/false, /*isVector=*/true, NumElements
,
61 ScalarSizeInBits
, /*AddressSpace=*/0};
64 /// Get a low-level vector of some number of elements and element type.
65 static LLT
vector(uint16_t NumElements
, LLT ScalarTy
) {
66 assert(NumElements
> 1 && "invalid number of vector elements");
67 assert(!ScalarTy
.isVector() && "invalid vector element type");
68 return LLT
{ScalarTy
.isPointer(), /*isVector=*/true, NumElements
,
69 ScalarTy
.getSizeInBits(),
70 ScalarTy
.isPointer() ? ScalarTy
.getAddressSpace() : 0};
73 static LLT
scalarOrVector(uint16_t NumElements
, LLT ScalarTy
) {
74 return NumElements
== 1 ? ScalarTy
: LLT::vector(NumElements
, ScalarTy
);
77 static LLT
scalarOrVector(uint16_t NumElements
, unsigned ScalarSize
) {
78 return scalarOrVector(NumElements
, LLT::scalar(ScalarSize
));
81 explicit LLT(bool isPointer
, bool isVector
, uint16_t NumElements
,
82 unsigned SizeInBits
, unsigned AddressSpace
) {
83 init(isPointer
, isVector
, NumElements
, SizeInBits
, AddressSpace
);
85 explicit LLT() : IsPointer(false), IsVector(false), RawData(0) {}
89 bool isValid() const { return RawData
!= 0; }
91 bool isScalar() const { return isValid() && !IsPointer
&& !IsVector
; }
93 bool isPointer() const { return isValid() && IsPointer
&& !IsVector
; }
95 bool isVector() const { return isValid() && IsVector
; }
97 /// Returns the number of elements in a vector LLT. Must only be called on
99 uint16_t getNumElements() const {
100 assert(IsVector
&& "cannot get number of elements on scalar/aggregate");
102 return getFieldValue(VectorElementsFieldInfo
);
104 return getFieldValue(PointerVectorElementsFieldInfo
);
107 /// Returns the total size of the type. Must only be called on sized types.
108 unsigned getSizeInBits() const {
109 if (isPointer() || isScalar())
110 return getScalarSizeInBits();
111 return getScalarSizeInBits() * getNumElements();
114 /// Returns the total size of the type in bytes, i.e. number of whole bytes
115 /// needed to represent the size in bits. Must only be called on sized types.
116 unsigned getSizeInBytes() const {
117 return (getSizeInBits() + 7) / 8;
120 LLT
getScalarType() const {
121 return isVector() ? getElementType() : *this;
124 /// If this type is a vector, return a vector with the same number of elements
125 /// but the new element type. Otherwise, return the new element type.
126 LLT
changeElementType(LLT NewEltTy
) const {
127 return isVector() ? LLT::vector(getNumElements(), NewEltTy
) : NewEltTy
;
130 /// If this type is a vector, return a vector with the same number of elements
131 /// but the new element size. Otherwise, return the new element type. Invalid
132 /// for pointer types. For pointer types, use changeElementType.
133 LLT
changeElementSize(unsigned NewEltSize
) const {
134 assert(!getScalarType().isPointer() &&
135 "invalid to directly change element size for pointers");
136 return isVector() ? LLT::vector(getNumElements(), NewEltSize
)
137 : LLT::scalar(NewEltSize
);
140 unsigned getScalarSizeInBits() const {
141 assert(RawData
!= 0 && "Invalid Type");
144 return getFieldValue(ScalarSizeFieldInfo
);
146 return getFieldValue(PointerSizeFieldInfo
);
149 return getFieldValue(VectorSizeFieldInfo
);
151 return getFieldValue(PointerVectorSizeFieldInfo
);
155 unsigned getAddressSpace() const {
156 assert(RawData
!= 0 && "Invalid Type");
157 assert(IsPointer
&& "cannot get address space of non-pointer type");
159 return getFieldValue(PointerAddressSpaceFieldInfo
);
161 return getFieldValue(PointerVectorAddressSpaceFieldInfo
);
164 /// Returns the vector's element type. Only valid for vector types.
165 LLT
getElementType() const {
166 assert(isVector() && "cannot get element type of scalar/aggregate");
168 return pointer(getAddressSpace(), getScalarSizeInBits());
170 return scalar(getScalarSizeInBits());
173 void print(raw_ostream
&OS
) const;
175 bool operator==(const LLT
&RHS
) const {
176 return IsPointer
== RHS
.IsPointer
&& IsVector
== RHS
.IsVector
&&
177 RHS
.RawData
== RawData
;
180 bool operator!=(const LLT
&RHS
) const { return !(*this == RHS
); }
182 friend struct DenseMapInfo
<LLT
>;
183 friend class GISelInstProfileBuilder
;
186 /// LLT is packed into 64 bits as follows:
189 /// with 62 bits remaining for Kind-specific data, packed in bitfields
190 /// as described below. As there isn't a simple portable way to pack bits
191 /// into bitfields, here the different fields in the packed structure is
192 /// described in static const *Field variables. Each of these variables
193 /// is a 2-element array, with the first element describing the bitfield size
194 /// and the second element describing the bitfield offset.
195 typedef int BitFieldInfo
[2];
197 /// This is how the bitfields are packed per Kind:
199 /// gets encoded as RawData == 0, as that is an invalid encoding, since for
200 /// valid encodings, SizeInBits/SizeOfElement must be larger than 0.
201 /// * Non-pointer scalar (isPointer == 0 && isVector == 0):
203 static const constexpr BitFieldInfo ScalarSizeFieldInfo
{32, 0};
204 /// * Pointer (isPointer == 1 && isVector == 0):
206 /// AddressSpace: 24;
207 static const constexpr BitFieldInfo PointerSizeFieldInfo
{16, 0};
208 static const constexpr BitFieldInfo PointerAddressSpaceFieldInfo
{
209 24, PointerSizeFieldInfo
[0] + PointerSizeFieldInfo
[1]};
210 /// * Vector-of-non-pointer (isPointer == 0 && isVector == 1):
212 /// SizeOfElement: 32;
213 static const constexpr BitFieldInfo VectorElementsFieldInfo
{16, 0};
214 static const constexpr BitFieldInfo VectorSizeFieldInfo
{
215 32, VectorElementsFieldInfo
[0] + VectorElementsFieldInfo
[1]};
216 /// * Vector-of-pointer (isPointer == 1 && isVector == 1):
218 /// SizeOfElement: 16;
219 /// AddressSpace: 24;
220 static const constexpr BitFieldInfo PointerVectorElementsFieldInfo
{16, 0};
221 static const constexpr BitFieldInfo PointerVectorSizeFieldInfo
{
223 PointerVectorElementsFieldInfo
[1] + PointerVectorElementsFieldInfo
[0]};
224 static const constexpr BitFieldInfo PointerVectorAddressSpaceFieldInfo
{
225 24, PointerVectorSizeFieldInfo
[1] + PointerVectorSizeFieldInfo
[0]};
227 uint64_t IsPointer
: 1;
228 uint64_t IsVector
: 1;
229 uint64_t RawData
: 62;
231 static uint64_t getMask(const BitFieldInfo FieldInfo
) {
232 const int FieldSizeInBits
= FieldInfo
[0];
233 return (((uint64_t)1) << FieldSizeInBits
) - 1;
235 static uint64_t maskAndShift(uint64_t Val
, uint64_t Mask
, uint8_t Shift
) {
236 assert(Val
<= Mask
&& "Value too large for field");
237 return (Val
& Mask
) << Shift
;
239 static uint64_t maskAndShift(uint64_t Val
, const BitFieldInfo FieldInfo
) {
240 return maskAndShift(Val
, getMask(FieldInfo
), FieldInfo
[1]);
242 uint64_t getFieldValue(const BitFieldInfo FieldInfo
) const {
243 return getMask(FieldInfo
) & (RawData
>> FieldInfo
[1]);
246 void init(bool IsPointer
, bool IsVector
, uint16_t NumElements
,
247 unsigned SizeInBits
, unsigned AddressSpace
) {
248 this->IsPointer
= IsPointer
;
249 this->IsVector
= IsVector
;
252 RawData
= maskAndShift(SizeInBits
, ScalarSizeFieldInfo
);
254 RawData
= maskAndShift(SizeInBits
, PointerSizeFieldInfo
) |
255 maskAndShift(AddressSpace
, PointerAddressSpaceFieldInfo
);
257 assert(NumElements
> 1 && "invalid number of vector elements");
259 RawData
= maskAndShift(NumElements
, VectorElementsFieldInfo
) |
260 maskAndShift(SizeInBits
, VectorSizeFieldInfo
);
263 maskAndShift(NumElements
, PointerVectorElementsFieldInfo
) |
264 maskAndShift(SizeInBits
, PointerVectorSizeFieldInfo
) |
265 maskAndShift(AddressSpace
, PointerVectorAddressSpaceFieldInfo
);
269 uint64_t getUniqueRAWLLTData() const {
270 return ((uint64_t)RawData
) << 2 | ((uint64_t)IsPointer
) << 1 |
271 ((uint64_t)IsVector
);
275 inline raw_ostream
& operator<<(raw_ostream
&OS
, const LLT
&Ty
) {
280 template<> struct DenseMapInfo
<LLT
> {
281 static inline LLT
getEmptyKey() {
283 Invalid
.IsPointer
= true;
286 static inline LLT
getTombstoneKey() {
288 Invalid
.IsVector
= true;
291 static inline unsigned getHashValue(const LLT
&Ty
) {
292 uint64_t Val
= Ty
.getUniqueRAWLLTData();
293 return DenseMapInfo
<uint64_t>::getHashValue(Val
);
295 static bool isEqual(const LLT
&LHS
, const LLT
&RHS
) {
302 #endif // LLVM_SUPPORT_LOWLEVELTYPEIMPL_H