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 LLT
getScalarType() const {
115 return isVector() ? getElementType() : *this;
118 /// If this type is a vector, return a vector with the same number of elements
119 /// but the new element type. Otherwise, return the new element type.
120 LLT
changeElementType(LLT NewEltTy
) const {
121 return isVector() ? LLT::vector(getNumElements(), NewEltTy
) : NewEltTy
;
124 /// If this type is a vector, return a vector with the same number of elements
125 /// but the new element size. Otherwise, return the new element type. Invalid
126 /// for pointer types. For pointer types, use changeElementType.
127 LLT
changeElementSize(unsigned NewEltSize
) const {
128 assert(!getScalarType().isPointer() &&
129 "invalid to directly change element size for pointers");
130 return isVector() ? LLT::vector(getNumElements(), NewEltSize
)
131 : LLT::scalar(NewEltSize
);
134 unsigned getScalarSizeInBits() const {
135 assert(RawData
!= 0 && "Invalid Type");
138 return getFieldValue(ScalarSizeFieldInfo
);
140 return getFieldValue(PointerSizeFieldInfo
);
143 return getFieldValue(VectorSizeFieldInfo
);
145 return getFieldValue(PointerVectorSizeFieldInfo
);
149 unsigned getAddressSpace() const {
150 assert(RawData
!= 0 && "Invalid Type");
151 assert(IsPointer
&& "cannot get address space of non-pointer type");
153 return getFieldValue(PointerAddressSpaceFieldInfo
);
155 return getFieldValue(PointerVectorAddressSpaceFieldInfo
);
158 /// Returns the vector's element type. Only valid for vector types.
159 LLT
getElementType() const {
160 assert(isVector() && "cannot get element type of scalar/aggregate");
162 return pointer(getAddressSpace(), getScalarSizeInBits());
164 return scalar(getScalarSizeInBits());
167 void print(raw_ostream
&OS
) const;
169 bool operator==(const LLT
&RHS
) const {
170 return IsPointer
== RHS
.IsPointer
&& IsVector
== RHS
.IsVector
&&
171 RHS
.RawData
== RawData
;
174 bool operator!=(const LLT
&RHS
) const { return !(*this == RHS
); }
176 friend struct DenseMapInfo
<LLT
>;
177 friend class GISelInstProfileBuilder
;
180 /// LLT is packed into 64 bits as follows:
183 /// with 62 bits remaining for Kind-specific data, packed in bitfields
184 /// as described below. As there isn't a simple portable way to pack bits
185 /// into bitfields, here the different fields in the packed structure is
186 /// described in static const *Field variables. Each of these variables
187 /// is a 2-element array, with the first element describing the bitfield size
188 /// and the second element describing the bitfield offset.
189 typedef int BitFieldInfo
[2];
191 /// This is how the bitfields are packed per Kind:
193 /// gets encoded as RawData == 0, as that is an invalid encoding, since for
194 /// valid encodings, SizeInBits/SizeOfElement must be larger than 0.
195 /// * Non-pointer scalar (isPointer == 0 && isVector == 0):
197 static const constexpr BitFieldInfo ScalarSizeFieldInfo
{32, 0};
198 /// * Pointer (isPointer == 1 && isVector == 0):
200 /// AddressSpace: 24;
201 static const constexpr BitFieldInfo PointerSizeFieldInfo
{16, 0};
202 static const constexpr BitFieldInfo PointerAddressSpaceFieldInfo
{
203 24, PointerSizeFieldInfo
[0] + PointerSizeFieldInfo
[1]};
204 /// * Vector-of-non-pointer (isPointer == 0 && isVector == 1):
206 /// SizeOfElement: 32;
207 static const constexpr BitFieldInfo VectorElementsFieldInfo
{16, 0};
208 static const constexpr BitFieldInfo VectorSizeFieldInfo
{
209 32, VectorElementsFieldInfo
[0] + VectorElementsFieldInfo
[1]};
210 /// * Vector-of-pointer (isPointer == 1 && isVector == 1):
212 /// SizeOfElement: 16;
213 /// AddressSpace: 24;
214 static const constexpr BitFieldInfo PointerVectorElementsFieldInfo
{16, 0};
215 static const constexpr BitFieldInfo PointerVectorSizeFieldInfo
{
217 PointerVectorElementsFieldInfo
[1] + PointerVectorElementsFieldInfo
[0]};
218 static const constexpr BitFieldInfo PointerVectorAddressSpaceFieldInfo
{
219 24, PointerVectorSizeFieldInfo
[1] + PointerVectorSizeFieldInfo
[0]};
221 uint64_t IsPointer
: 1;
222 uint64_t IsVector
: 1;
223 uint64_t RawData
: 62;
225 static uint64_t getMask(const BitFieldInfo FieldInfo
) {
226 const int FieldSizeInBits
= FieldInfo
[0];
227 return (((uint64_t)1) << FieldSizeInBits
) - 1;
229 static uint64_t maskAndShift(uint64_t Val
, uint64_t Mask
, uint8_t Shift
) {
230 assert(Val
<= Mask
&& "Value too large for field");
231 return (Val
& Mask
) << Shift
;
233 static uint64_t maskAndShift(uint64_t Val
, const BitFieldInfo FieldInfo
) {
234 return maskAndShift(Val
, getMask(FieldInfo
), FieldInfo
[1]);
236 uint64_t getFieldValue(const BitFieldInfo FieldInfo
) const {
237 return getMask(FieldInfo
) & (RawData
>> FieldInfo
[1]);
240 void init(bool IsPointer
, bool IsVector
, uint16_t NumElements
,
241 unsigned SizeInBits
, unsigned AddressSpace
) {
242 this->IsPointer
= IsPointer
;
243 this->IsVector
= IsVector
;
246 RawData
= maskAndShift(SizeInBits
, ScalarSizeFieldInfo
);
248 RawData
= maskAndShift(SizeInBits
, PointerSizeFieldInfo
) |
249 maskAndShift(AddressSpace
, PointerAddressSpaceFieldInfo
);
251 assert(NumElements
> 1 && "invalid number of vector elements");
253 RawData
= maskAndShift(NumElements
, VectorElementsFieldInfo
) |
254 maskAndShift(SizeInBits
, VectorSizeFieldInfo
);
257 maskAndShift(NumElements
, PointerVectorElementsFieldInfo
) |
258 maskAndShift(SizeInBits
, PointerVectorSizeFieldInfo
) |
259 maskAndShift(AddressSpace
, PointerVectorAddressSpaceFieldInfo
);
263 uint64_t getUniqueRAWLLTData() const {
264 return ((uint64_t)RawData
) << 2 | ((uint64_t)IsPointer
) << 1 |
265 ((uint64_t)IsVector
);
269 inline raw_ostream
& operator<<(raw_ostream
&OS
, const LLT
&Ty
) {
274 template<> struct DenseMapInfo
<LLT
> {
275 static inline LLT
getEmptyKey() {
277 Invalid
.IsPointer
= true;
280 static inline LLT
getTombstoneKey() {
282 Invalid
.IsVector
= true;
285 static inline unsigned getHashValue(const LLT
&Ty
) {
286 uint64_t Val
= Ty
.getUniqueRAWLLTData();
287 return DenseMapInfo
<uint64_t>::getHashValue(Val
);
289 static bool isEqual(const LLT
&LHS
, const LLT
&RHS
) {
296 #endif // LLVM_SUPPORT_LOWLEVELTYPEIMPL_H