1 //== llvm/Support/LowLevelTypeImpl.h --------------------------- -*- C++ -*-==//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 /// Implement a low-level type suitable for MachineInstr level instruction
11 /// selection.
12 ///
13 /// For a type attached to a MachineInstr, we only care about 2 details: total
14 /// size and the number of vector lanes (if any). Accordingly, there are 4
15 /// possible valid type-kinds:
16 ///
17 ///    * `sN` for scalars and aggregates
18 ///    * `<N x sM>` for vectors, which must have at least 2 elements.
19 ///    * `pN` for pointers
20 ///
21 /// Other information required for correct selection is expected to be carried
22 /// by the opcode, or non-type flags. For example the distinction between G_ADD
23 /// and G_FADD for int/float or fast-math flags.
24 //
25 //===----------------------------------------------------------------------===//
26 
27 #ifndef LLVM_SUPPORT_LOWLEVELTYPEIMPL_H
28 #define LLVM_SUPPORT_LOWLEVELTYPEIMPL_H
29 
30 #include "llvm/ADT/DenseMapInfo.h"
31 #include "llvm/Support/MachineValueType.h"
32 #include <cassert>
33 
34 namespace llvm {
35 
36 class DataLayout;
37 class Type;
38 class raw_ostream;
39 
40 class LLT {
41 public:
42   /// Get a low-level scalar or aggregate "bag of bits".
scalar(unsigned SizeInBits)43   static LLT scalar(unsigned SizeInBits) {
44     assert(SizeInBits > 0 && "invalid scalar size");
45     return LLT{/*isPointer=*/false, /*isVector=*/false, /*NumElements=*/0,
46                SizeInBits, /*AddressSpace=*/0};
47   }
48 
49   /// Get a low-level pointer in the given address space (defaulting to 0).
pointer(uint16_t AddressSpace,unsigned SizeInBits)50   static LLT pointer(uint16_t AddressSpace, unsigned SizeInBits) {
51     assert(SizeInBits > 0 && "invalid pointer size");
52     return LLT{/*isPointer=*/true, /*isVector=*/false, /*NumElements=*/0,
53                SizeInBits, AddressSpace};
54   }
55 
56   /// Get a low-level vector of some number of elements and element width.
57   /// \p NumElements must be at least 2.
vector(uint16_t NumElements,unsigned ScalarSizeInBits)58   static LLT vector(uint16_t NumElements, unsigned ScalarSizeInBits) {
59     assert(NumElements > 1 && "invalid number of vector elements");
60     assert(ScalarSizeInBits > 0 && "invalid vector element size");
61     return LLT{/*isPointer=*/false, /*isVector=*/true, NumElements,
62                ScalarSizeInBits, /*AddressSpace=*/0};
63   }
64 
65   /// Get a low-level vector of some number of elements and element type.
vector(uint16_t NumElements,LLT ScalarTy)66   static LLT vector(uint16_t NumElements, LLT ScalarTy) {
67     assert(NumElements > 1 && "invalid number of vector elements");
68     assert(!ScalarTy.isVector() && "invalid vector element type");
69     return LLT{ScalarTy.isPointer(), /*isVector=*/true, NumElements,
70                ScalarTy.getSizeInBits(),
71                ScalarTy.isPointer() ? ScalarTy.getAddressSpace() : 0};
72   }
73 
LLT(bool isPointer,bool isVector,uint16_t NumElements,unsigned SizeInBits,unsigned AddressSpace)74   explicit LLT(bool isPointer, bool isVector, uint16_t NumElements,
75                unsigned SizeInBits, unsigned AddressSpace) {
76     init(isPointer, isVector, NumElements, SizeInBits, AddressSpace);
77   }
LLT()78   explicit LLT() : IsPointer(false), IsVector(false), RawData(0) {}
79 
80   explicit LLT(MVT VT);
81 
isValid()82   bool isValid() const { return RawData != 0; }
83 
isScalar()84   bool isScalar() const { return isValid() && !IsPointer && !IsVector; }
85 
isPointer()86   bool isPointer() const { return isValid() && IsPointer && !IsVector; }
87 
isVector()88   bool isVector() const { return isValid() && IsVector; }
89 
90   /// Returns the number of elements in a vector LLT. Must only be called on
91   /// vector types.
getNumElements()92   uint16_t getNumElements() const {
93     assert(IsVector && "cannot get number of elements on scalar/aggregate");
94     if (!IsPointer)
95       return getFieldValue(VectorElementsFieldInfo);
96     else
97       return getFieldValue(PointerVectorElementsFieldInfo);
98   }
99 
100   /// Returns the total size of the type. Must only be called on sized types.
getSizeInBits()101   unsigned getSizeInBits() const {
102     if (isPointer() || isScalar())
103       return getScalarSizeInBits();
104     return getScalarSizeInBits() * getNumElements();
105   }
106 
getScalarSizeInBits()107   unsigned getScalarSizeInBits() const {
108     assert(RawData != 0 && "Invalid Type");
109     if (!IsVector) {
110       if (!IsPointer)
111         return getFieldValue(ScalarSizeFieldInfo);
112       else
113         return getFieldValue(PointerSizeFieldInfo);
114     } else {
115       if (!IsPointer)
116         return getFieldValue(VectorSizeFieldInfo);
117       else
118         return getFieldValue(PointerVectorSizeFieldInfo);
119     }
120   }
121 
getAddressSpace()122   unsigned getAddressSpace() const {
123     assert(RawData != 0 && "Invalid Type");
124     assert(IsPointer && "cannot get address space of non-pointer type");
125     if (!IsVector)
126       return getFieldValue(PointerAddressSpaceFieldInfo);
127     else
128       return getFieldValue(PointerVectorAddressSpaceFieldInfo);
129   }
130 
131   /// Returns the vector's element type. Only valid for vector types.
getElementType()132   LLT getElementType() const {
133     assert(isVector() && "cannot get element type of scalar/aggregate");
134     if (IsPointer)
135       return pointer(getAddressSpace(), getScalarSizeInBits());
136     else
137       return scalar(getScalarSizeInBits());
138   }
139 
140   void print(raw_ostream &OS) const;
141 
142   bool operator==(const LLT &RHS) const {
143     return IsPointer == RHS.IsPointer && IsVector == RHS.IsVector &&
144            RHS.RawData == RawData;
145   }
146 
147   bool operator!=(const LLT &RHS) const { return !(*this == RHS); }
148 
149   friend struct DenseMapInfo<LLT>;
150   friend class GISelInstProfileBuilder;
151 
152 private:
153   /// LLT is packed into 64 bits as follows:
154   /// isPointer : 1
155   /// isVector  : 1
156   /// with 62 bits remaining for Kind-specific data, packed in bitfields
157   /// as described below. As there isn't a simple portable way to pack bits
158   /// into bitfields, here the different fields in the packed structure is
159   /// described in static const *Field variables. Each of these variables
160   /// is a 2-element array, with the first element describing the bitfield size
161   /// and the second element describing the bitfield offset.
162   typedef int BitFieldInfo[2];
163   ///
164   /// This is how the bitfields are packed per Kind:
165   /// * Invalid:
166   ///   gets encoded as RawData == 0, as that is an invalid encoding, since for
167   ///   valid encodings, SizeInBits/SizeOfElement must be larger than 0.
168   /// * Non-pointer scalar (isPointer == 0 && isVector == 0):
169   ///   SizeInBits: 32;
170   static const constexpr BitFieldInfo ScalarSizeFieldInfo{32, 0};
171   /// * Pointer (isPointer == 1 && isVector == 0):
172   ///   SizeInBits: 16;
173   ///   AddressSpace: 23;
174   static const constexpr BitFieldInfo PointerSizeFieldInfo{16, 0};
175   static const constexpr BitFieldInfo PointerAddressSpaceFieldInfo{
176       23, PointerSizeFieldInfo[0] + PointerSizeFieldInfo[1]};
177   /// * Vector-of-non-pointer (isPointer == 0 && isVector == 1):
178   ///   NumElements: 16;
179   ///   SizeOfElement: 32;
180   static const constexpr BitFieldInfo VectorElementsFieldInfo{16, 0};
181   static const constexpr BitFieldInfo VectorSizeFieldInfo{
182       32, VectorElementsFieldInfo[0] + VectorElementsFieldInfo[1]};
183   /// * Vector-of-pointer (isPointer == 1 && isVector == 1):
184   ///   NumElements: 16;
185   ///   SizeOfElement: 16;
186   ///   AddressSpace: 23;
187   static const constexpr BitFieldInfo PointerVectorElementsFieldInfo{16, 0};
188   static const constexpr BitFieldInfo PointerVectorSizeFieldInfo{
189       16,
190       PointerVectorElementsFieldInfo[1] + PointerVectorElementsFieldInfo[0]};
191   static const constexpr BitFieldInfo PointerVectorAddressSpaceFieldInfo{
192       23, PointerVectorSizeFieldInfo[1] + PointerVectorSizeFieldInfo[0]};
193 
194   uint64_t IsPointer : 1;
195   uint64_t IsVector : 1;
196   uint64_t RawData : 62;
197 
198   static uint64_t getMask(const BitFieldInfo FieldInfo) {
199     const int FieldSizeInBits = FieldInfo[0];
200     return (((uint64_t)1) << FieldSizeInBits) - 1;
201   }
202   static uint64_t maskAndShift(uint64_t Val, uint64_t Mask, uint8_t Shift) {
203     assert(Val <= Mask && "Value too large for field");
204     return (Val & Mask) << Shift;
205   }
206   static uint64_t maskAndShift(uint64_t Val, const BitFieldInfo FieldInfo) {
207     return maskAndShift(Val, getMask(FieldInfo), FieldInfo[1]);
208   }
209   uint64_t getFieldValue(const BitFieldInfo FieldInfo) const {
210     return getMask(FieldInfo) & (RawData >> FieldInfo[1]);
211   }
212 
213   void init(bool IsPointer, bool IsVector, uint16_t NumElements,
214             unsigned SizeInBits, unsigned AddressSpace) {
215     this->IsPointer = IsPointer;
216     this->IsVector = IsVector;
217     if (!IsVector) {
218       if (!IsPointer)
219         RawData = maskAndShift(SizeInBits, ScalarSizeFieldInfo);
220       else
221         RawData = maskAndShift(SizeInBits, PointerSizeFieldInfo) |
222                   maskAndShift(AddressSpace, PointerAddressSpaceFieldInfo);
223     } else {
224       assert(NumElements > 1 && "invalid number of vector elements");
225       if (!IsPointer)
226         RawData = maskAndShift(NumElements, VectorElementsFieldInfo) |
227                   maskAndShift(SizeInBits, VectorSizeFieldInfo);
228       else
229         RawData =
230             maskAndShift(NumElements, PointerVectorElementsFieldInfo) |
231             maskAndShift(SizeInBits, PointerVectorSizeFieldInfo) |
232             maskAndShift(AddressSpace, PointerVectorAddressSpaceFieldInfo);
233     }
234   }
235 
236   uint64_t getUniqueRAWLLTData() const {
237     return ((uint64_t)RawData) << 2 | ((uint64_t)IsPointer) << 1 |
238            ((uint64_t)IsVector);
239   }
240 };
241 
242 inline raw_ostream& operator<<(raw_ostream &OS, const LLT &Ty) {
243   Ty.print(OS);
244   return OS;
245 }
246 
247 template<> struct DenseMapInfo<LLT> {
248   static inline LLT getEmptyKey() {
249     LLT Invalid;
250     Invalid.IsPointer = true;
251     return Invalid;
252   }
253   static inline LLT getTombstoneKey() {
254     LLT Invalid;
255     Invalid.IsVector = true;
256     return Invalid;
257   }
258   static inline unsigned getHashValue(const LLT &Ty) {
259     uint64_t Val = Ty.getUniqueRAWLLTData();
260     return DenseMapInfo<uint64_t>::getHashValue(Val);
261   }
262   static bool isEqual(const LLT &LHS, const LLT &RHS) {
263     return LHS == RHS;
264   }
265 };
266 
267 }
268 
269 #endif // LLVM_SUPPORT_LOWLEVELTYPEIMPL_H
270