1 //== llvm/Support/LowLevelTypeImpl.h --------------------------- -*- C++ -*-==//
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 //
9 /// Implement a low-level type suitable for MachineInstr level instruction
10 /// selection.
11 ///
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:
15 ///
16 ///    * `sN` for scalars and aggregates
17 ///    * `<N x sM>` for vectors, which must have at least 2 elements.
18 ///    * `pN` for pointers
19 ///
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.
23 //
24 //===----------------------------------------------------------------------===//
25 
26 #ifndef LLVM_SUPPORT_LOWLEVELTYPEIMPL_H
27 #define LLVM_SUPPORT_LOWLEVELTYPEIMPL_H
28 
29 #include "llvm/ADT/DenseMapInfo.h"
30 #include "llvm/Support/MachineValueType.h"
31 #include <cassert>
32 
33 namespace llvm {
34 
35 class DataLayout;
36 class Type;
37 class raw_ostream;
38 
39 class LLT {
40 public:
41   /// Get a low-level scalar or aggregate "bag of bits".
scalar(unsigned SizeInBits)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};
46   }
47 
48   /// Get a low-level pointer in the given address space.
pointer(unsigned AddressSpace,unsigned SizeInBits)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};
53   }
54 
55   /// Get a low-level vector of some number of elements and element width.
56   /// \p NumElements must be at least 2.
vector(uint16_t NumElements,unsigned ScalarSizeInBits)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};
62   }
63 
64   /// Get a low-level vector of some number of elements and element type.
vector(uint16_t NumElements,LLT ScalarTy)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};
71   }
72 
scalarOrVector(uint16_t NumElements,LLT ScalarTy)73   static LLT scalarOrVector(uint16_t NumElements, LLT ScalarTy) {
74     return NumElements == 1 ? ScalarTy : LLT::vector(NumElements, ScalarTy);
75   }
76 
scalarOrVector(uint16_t NumElements,unsigned ScalarSize)77   static LLT scalarOrVector(uint16_t NumElements, unsigned ScalarSize) {
78     return scalarOrVector(NumElements, LLT::scalar(ScalarSize));
79   }
80 
LLT(bool isPointer,bool isVector,uint16_t NumElements,unsigned SizeInBits,unsigned AddressSpace)81   explicit LLT(bool isPointer, bool isVector, uint16_t NumElements,
82                unsigned SizeInBits, unsigned AddressSpace) {
83     init(isPointer, isVector, NumElements, SizeInBits, AddressSpace);
84   }
LLT()85   explicit LLT() : IsPointer(false), IsVector(false), RawData(0) {}
86 
87   explicit LLT(MVT VT);
88 
isValid()89   bool isValid() const { return RawData != 0; }
90 
isScalar()91   bool isScalar() const { return isValid() && !IsPointer && !IsVector; }
92 
isPointer()93   bool isPointer() const { return isValid() && IsPointer && !IsVector; }
94 
isVector()95   bool isVector() const { return isValid() && IsVector; }
96 
97   /// Returns the number of elements in a vector LLT. Must only be called on
98   /// vector types.
getNumElements()99   uint16_t getNumElements() const {
100     assert(IsVector && "cannot get number of elements on scalar/aggregate");
101     if (!IsPointer)
102       return getFieldValue(VectorElementsFieldInfo);
103     else
104       return getFieldValue(PointerVectorElementsFieldInfo);
105   }
106 
107   /// Returns the total size of the type. Must only be called on sized types.
getSizeInBits()108   unsigned getSizeInBits() const {
109     if (isPointer() || isScalar())
110       return getScalarSizeInBits();
111     return getScalarSizeInBits() * getNumElements();
112   }
113 
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.
getSizeInBytes()116   unsigned getSizeInBytes() const {
117     return (getSizeInBits() + 7) / 8;
118   }
119 
getScalarType()120   LLT getScalarType() const {
121     return isVector() ? getElementType() : *this;
122   }
123 
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.
changeElementType(LLT NewEltTy)126   LLT changeElementType(LLT NewEltTy) const {
127     return isVector() ? LLT::vector(getNumElements(), NewEltTy) : NewEltTy;
128   }
129 
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.
changeElementSize(unsigned NewEltSize)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);
138   }
139 
getScalarSizeInBits()140   unsigned getScalarSizeInBits() const {
141     assert(RawData != 0 && "Invalid Type");
142     if (!IsVector) {
143       if (!IsPointer)
144         return getFieldValue(ScalarSizeFieldInfo);
145       else
146         return getFieldValue(PointerSizeFieldInfo);
147     } else {
148       if (!IsPointer)
149         return getFieldValue(VectorSizeFieldInfo);
150       else
151         return getFieldValue(PointerVectorSizeFieldInfo);
152     }
153   }
154 
getAddressSpace()155   unsigned getAddressSpace() const {
156     assert(RawData != 0 && "Invalid Type");
157     assert(IsPointer && "cannot get address space of non-pointer type");
158     if (!IsVector)
159       return getFieldValue(PointerAddressSpaceFieldInfo);
160     else
161       return getFieldValue(PointerVectorAddressSpaceFieldInfo);
162   }
163 
164   /// Returns the vector's element type. Only valid for vector types.
getElementType()165   LLT getElementType() const {
166     assert(isVector() && "cannot get element type of scalar/aggregate");
167     if (IsPointer)
168       return pointer(getAddressSpace(), getScalarSizeInBits());
169     else
170       return scalar(getScalarSizeInBits());
171   }
172 
173   void print(raw_ostream &OS) const;
174 
175   bool operator==(const LLT &RHS) const {
176     return IsPointer == RHS.IsPointer && IsVector == RHS.IsVector &&
177            RHS.RawData == RawData;
178   }
179 
180   bool operator!=(const LLT &RHS) const { return !(*this == RHS); }
181 
182   friend struct DenseMapInfo<LLT>;
183   friend class GISelInstProfileBuilder;
184 
185 private:
186   /// LLT is packed into 64 bits as follows:
187   /// isPointer : 1
188   /// isVector  : 1
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];
196   ///
197   /// This is how the bitfields are packed per Kind:
198   /// * Invalid:
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):
202   ///   SizeInBits: 32;
203   static const constexpr BitFieldInfo ScalarSizeFieldInfo{32, 0};
204   /// * Pointer (isPointer == 1 && isVector == 0):
205   ///   SizeInBits: 16;
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):
211   ///   NumElements: 16;
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):
217   ///   NumElements: 16;
218   ///   SizeOfElement: 16;
219   ///   AddressSpace: 24;
220   static const constexpr BitFieldInfo PointerVectorElementsFieldInfo{16, 0};
221   static const constexpr BitFieldInfo PointerVectorSizeFieldInfo{
222       16,
223       PointerVectorElementsFieldInfo[1] + PointerVectorElementsFieldInfo[0]};
224   static const constexpr BitFieldInfo PointerVectorAddressSpaceFieldInfo{
225       24, PointerVectorSizeFieldInfo[1] + PointerVectorSizeFieldInfo[0]};
226 
227   uint64_t IsPointer : 1;
228   uint64_t IsVector : 1;
229   uint64_t RawData : 62;
230 
231   static uint64_t getMask(const BitFieldInfo FieldInfo) {
232     const int FieldSizeInBits = FieldInfo[0];
233     return (((uint64_t)1) << FieldSizeInBits) - 1;
234   }
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;
238   }
239   static uint64_t maskAndShift(uint64_t Val, const BitFieldInfo FieldInfo) {
240     return maskAndShift(Val, getMask(FieldInfo), FieldInfo[1]);
241   }
242   uint64_t getFieldValue(const BitFieldInfo FieldInfo) const {
243     return getMask(FieldInfo) & (RawData >> FieldInfo[1]);
244   }
245 
246   void init(bool IsPointer, bool IsVector, uint16_t NumElements,
247             unsigned SizeInBits, unsigned AddressSpace) {
248     this->IsPointer = IsPointer;
249     this->IsVector = IsVector;
250     if (!IsVector) {
251       if (!IsPointer)
252         RawData = maskAndShift(SizeInBits, ScalarSizeFieldInfo);
253       else
254         RawData = maskAndShift(SizeInBits, PointerSizeFieldInfo) |
255                   maskAndShift(AddressSpace, PointerAddressSpaceFieldInfo);
256     } else {
257       assert(NumElements > 1 && "invalid number of vector elements");
258       if (!IsPointer)
259         RawData = maskAndShift(NumElements, VectorElementsFieldInfo) |
260                   maskAndShift(SizeInBits, VectorSizeFieldInfo);
261       else
262         RawData =
263             maskAndShift(NumElements, PointerVectorElementsFieldInfo) |
264             maskAndShift(SizeInBits, PointerVectorSizeFieldInfo) |
265             maskAndShift(AddressSpace, PointerVectorAddressSpaceFieldInfo);
266     }
267   }
268 
269   uint64_t getUniqueRAWLLTData() const {
270     return ((uint64_t)RawData) << 2 | ((uint64_t)IsPointer) << 1 |
271            ((uint64_t)IsVector);
272   }
273 };
274 
275 inline raw_ostream& operator<<(raw_ostream &OS, const LLT &Ty) {
276   Ty.print(OS);
277   return OS;
278 }
279 
280 template<> struct DenseMapInfo<LLT> {
281   static inline LLT getEmptyKey() {
282     LLT Invalid;
283     Invalid.IsPointer = true;
284     return Invalid;
285   }
286   static inline LLT getTombstoneKey() {
287     LLT Invalid;
288     Invalid.IsVector = true;
289     return Invalid;
290   }
291   static inline unsigned getHashValue(const LLT &Ty) {
292     uint64_t Val = Ty.getUniqueRAWLLTData();
293     return DenseMapInfo<uint64_t>::getHashValue(Val);
294   }
295   static bool isEqual(const LLT &LHS, const LLT &RHS) {
296     return LHS == RHS;
297   }
298 };
299 
300 }
301 
302 #endif // LLVM_SUPPORT_LOWLEVELTYPEIMPL_H
303