• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
isByteSized()140   bool isByteSized() const { return (getSizeInBits() & 7) == 0; }
141 
getScalarSizeInBits()142   unsigned getScalarSizeInBits() const {
143     assert(RawData != 0 && "Invalid Type");
144     if (!IsVector) {
145       if (!IsPointer)
146         return getFieldValue(ScalarSizeFieldInfo);
147       else
148         return getFieldValue(PointerSizeFieldInfo);
149     } else {
150       if (!IsPointer)
151         return getFieldValue(VectorSizeFieldInfo);
152       else
153         return getFieldValue(PointerVectorSizeFieldInfo);
154     }
155   }
156 
getAddressSpace()157   unsigned getAddressSpace() const {
158     assert(RawData != 0 && "Invalid Type");
159     assert(IsPointer && "cannot get address space of non-pointer type");
160     if (!IsVector)
161       return getFieldValue(PointerAddressSpaceFieldInfo);
162     else
163       return getFieldValue(PointerVectorAddressSpaceFieldInfo);
164   }
165 
166   /// Returns the vector's element type. Only valid for vector types.
getElementType()167   LLT getElementType() const {
168     assert(isVector() && "cannot get element type of scalar/aggregate");
169     if (IsPointer)
170       return pointer(getAddressSpace(), getScalarSizeInBits());
171     else
172       return scalar(getScalarSizeInBits());
173   }
174 
175   void print(raw_ostream &OS) const;
176 
177   bool operator==(const LLT &RHS) const {
178     return IsPointer == RHS.IsPointer && IsVector == RHS.IsVector &&
179            RHS.RawData == RawData;
180   }
181 
182   bool operator!=(const LLT &RHS) const { return !(*this == RHS); }
183 
184   friend struct DenseMapInfo<LLT>;
185   friend class GISelInstProfileBuilder;
186 
187 private:
188   /// LLT is packed into 64 bits as follows:
189   /// isPointer : 1
190   /// isVector  : 1
191   /// with 62 bits remaining for Kind-specific data, packed in bitfields
192   /// as described below. As there isn't a simple portable way to pack bits
193   /// into bitfields, here the different fields in the packed structure is
194   /// described in static const *Field variables. Each of these variables
195   /// is a 2-element array, with the first element describing the bitfield size
196   /// and the second element describing the bitfield offset.
197   typedef int BitFieldInfo[2];
198   ///
199   /// This is how the bitfields are packed per Kind:
200   /// * Invalid:
201   ///   gets encoded as RawData == 0, as that is an invalid encoding, since for
202   ///   valid encodings, SizeInBits/SizeOfElement must be larger than 0.
203   /// * Non-pointer scalar (isPointer == 0 && isVector == 0):
204   ///   SizeInBits: 32;
205   static const constexpr BitFieldInfo ScalarSizeFieldInfo{32, 0};
206   /// * Pointer (isPointer == 1 && isVector == 0):
207   ///   SizeInBits: 16;
208   ///   AddressSpace: 24;
209   static const constexpr BitFieldInfo PointerSizeFieldInfo{16, 0};
210   static const constexpr BitFieldInfo PointerAddressSpaceFieldInfo{
211       24, PointerSizeFieldInfo[0] + PointerSizeFieldInfo[1]};
212   /// * Vector-of-non-pointer (isPointer == 0 && isVector == 1):
213   ///   NumElements: 16;
214   ///   SizeOfElement: 32;
215   static const constexpr BitFieldInfo VectorElementsFieldInfo{16, 0};
216   static const constexpr BitFieldInfo VectorSizeFieldInfo{
217       32, VectorElementsFieldInfo[0] + VectorElementsFieldInfo[1]};
218   /// * Vector-of-pointer (isPointer == 1 && isVector == 1):
219   ///   NumElements: 16;
220   ///   SizeOfElement: 16;
221   ///   AddressSpace: 24;
222   static const constexpr BitFieldInfo PointerVectorElementsFieldInfo{16, 0};
223   static const constexpr BitFieldInfo PointerVectorSizeFieldInfo{
224       16,
225       PointerVectorElementsFieldInfo[1] + PointerVectorElementsFieldInfo[0]};
226   static const constexpr BitFieldInfo PointerVectorAddressSpaceFieldInfo{
227       24, PointerVectorSizeFieldInfo[1] + PointerVectorSizeFieldInfo[0]};
228 
229   uint64_t IsPointer : 1;
230   uint64_t IsVector : 1;
231   uint64_t RawData : 62;
232 
233   static uint64_t getMask(const BitFieldInfo FieldInfo) {
234     const int FieldSizeInBits = FieldInfo[0];
235     return (((uint64_t)1) << FieldSizeInBits) - 1;
236   }
237   static uint64_t maskAndShift(uint64_t Val, uint64_t Mask, uint8_t Shift) {
238     assert(Val <= Mask && "Value too large for field");
239     return (Val & Mask) << Shift;
240   }
241   static uint64_t maskAndShift(uint64_t Val, const BitFieldInfo FieldInfo) {
242     return maskAndShift(Val, getMask(FieldInfo), FieldInfo[1]);
243   }
244   uint64_t getFieldValue(const BitFieldInfo FieldInfo) const {
245     return getMask(FieldInfo) & (RawData >> FieldInfo[1]);
246   }
247 
248   void init(bool IsPointer, bool IsVector, uint16_t NumElements,
249             unsigned SizeInBits, unsigned AddressSpace) {
250     this->IsPointer = IsPointer;
251     this->IsVector = IsVector;
252     if (!IsVector) {
253       if (!IsPointer)
254         RawData = maskAndShift(SizeInBits, ScalarSizeFieldInfo);
255       else
256         RawData = maskAndShift(SizeInBits, PointerSizeFieldInfo) |
257                   maskAndShift(AddressSpace, PointerAddressSpaceFieldInfo);
258     } else {
259       assert(NumElements > 1 && "invalid number of vector elements");
260       if (!IsPointer)
261         RawData = maskAndShift(NumElements, VectorElementsFieldInfo) |
262                   maskAndShift(SizeInBits, VectorSizeFieldInfo);
263       else
264         RawData =
265             maskAndShift(NumElements, PointerVectorElementsFieldInfo) |
266             maskAndShift(SizeInBits, PointerVectorSizeFieldInfo) |
267             maskAndShift(AddressSpace, PointerVectorAddressSpaceFieldInfo);
268     }
269   }
270 
271   uint64_t getUniqueRAWLLTData() const {
272     return ((uint64_t)RawData) << 2 | ((uint64_t)IsPointer) << 1 |
273            ((uint64_t)IsVector);
274   }
275 };
276 
277 inline raw_ostream& operator<<(raw_ostream &OS, const LLT &Ty) {
278   Ty.print(OS);
279   return OS;
280 }
281 
282 template<> struct DenseMapInfo<LLT> {
283   static inline LLT getEmptyKey() {
284     LLT Invalid;
285     Invalid.IsPointer = true;
286     return Invalid;
287   }
288   static inline LLT getTombstoneKey() {
289     LLT Invalid;
290     Invalid.IsVector = true;
291     return Invalid;
292   }
293   static inline unsigned getHashValue(const LLT &Ty) {
294     uint64_t Val = Ty.getUniqueRAWLLTData();
295     return DenseMapInfo<uint64_t>::getHashValue(Val);
296   }
297   static bool isEqual(const LLT &LHS, const LLT &RHS) {
298     return LHS == RHS;
299   }
300 };
301 
302 }
303 
304 #endif // LLVM_SUPPORT_LOWLEVELTYPEIMPL_H
305