LLVM  9.0.0svn
LowLevelTypeImpl.h
Go to the documentation of this file.
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"
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".
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.
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.
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.
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 
73  static LLT scalarOrVector(uint16_t NumElements, LLT ScalarTy) {
74  return NumElements == 1 ? ScalarTy : LLT::vector(NumElements, ScalarTy);
75  }
76 
77  static LLT scalarOrVector(uint16_t NumElements, unsigned ScalarSize) {
78  return scalarOrVector(NumElements, LLT::scalar(ScalarSize));
79  }
80 
81  explicit LLT(bool isPointer, bool isVector, uint16_t NumElements,
82  unsigned SizeInBits, unsigned AddressSpace) {
83  init(isPointer, isVector, NumElements, SizeInBits, AddressSpace);
84  }
85  explicit LLT() : IsPointer(false), IsVector(false), RawData(0) {}
86 
87  explicit LLT(MVT VT);
88 
89  bool isValid() const { return RawData != 0; }
90 
91  bool isScalar() const { return isValid() && !IsPointer && !IsVector; }
92 
93  bool isPointer() const { return isValid() && IsPointer && !IsVector; }
94 
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.
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.
108  unsigned getSizeInBits() const {
109  if (isPointer() || isScalar())
110  return getScalarSizeInBits();
111  return getScalarSizeInBits() * getNumElements();
112  }
113 
114  LLT getScalarType() const {
115  return isVector() ? getElementType() : *this;
116  }
117 
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;
122  }
123 
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 {
129  "invalid to directly change element size for pointers");
130  return isVector() ? LLT::vector(getNumElements(), NewEltSize)
131  : LLT::scalar(NewEltSize);
132  }
133 
134  unsigned getScalarSizeInBits() const {
135  assert(RawData != 0 && "Invalid Type");
136  if (!IsVector) {
137  if (!IsPointer)
138  return getFieldValue(ScalarSizeFieldInfo);
139  else
140  return getFieldValue(PointerSizeFieldInfo);
141  } else {
142  if (!IsPointer)
143  return getFieldValue(VectorSizeFieldInfo);
144  else
145  return getFieldValue(PointerVectorSizeFieldInfo);
146  }
147  }
148 
149  unsigned getAddressSpace() const {
150  assert(RawData != 0 && "Invalid Type");
151  assert(IsPointer && "cannot get address space of non-pointer type");
152  if (!IsVector)
153  return getFieldValue(PointerAddressSpaceFieldInfo);
154  else
155  return getFieldValue(PointerVectorAddressSpaceFieldInfo);
156  }
157 
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");
161  if (IsPointer)
163  else
164  return scalar(getScalarSizeInBits());
165  }
166 
167  void print(raw_ostream &OS) const;
168 
169  bool operator==(const LLT &RHS) const {
170  return IsPointer == RHS.IsPointer && IsVector == RHS.IsVector &&
171  RHS.RawData == RawData;
172  }
173 
174  bool operator!=(const LLT &RHS) const { return !(*this == RHS); }
175 
176  friend struct DenseMapInfo<LLT>;
178 
179 private:
180  /// LLT is packed into 64 bits as follows:
181  /// isPointer : 1
182  /// isVector : 1
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];
190  ///
191  /// This is how the bitfields are packed per Kind:
192  /// * Invalid:
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):
196  /// SizeInBits: 32;
197  static const constexpr BitFieldInfo ScalarSizeFieldInfo{32, 0};
198  /// * Pointer (isPointer == 1 && isVector == 0):
199  /// SizeInBits: 16;
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):
205  /// NumElements: 16;
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):
211  /// NumElements: 16;
212  /// SizeOfElement: 16;
213  /// AddressSpace: 24;
214  static const constexpr BitFieldInfo PointerVectorElementsFieldInfo{16, 0};
215  static const constexpr BitFieldInfo PointerVectorSizeFieldInfo{
216  16,
217  PointerVectorElementsFieldInfo[1] + PointerVectorElementsFieldInfo[0]};
218  static const constexpr BitFieldInfo PointerVectorAddressSpaceFieldInfo{
219  24, PointerVectorSizeFieldInfo[1] + PointerVectorSizeFieldInfo[0]};
220 
221  uint64_t IsPointer : 1;
222  uint64_t IsVector : 1;
223  uint64_t RawData : 62;
224 
225  static uint64_t getMask(const BitFieldInfo FieldInfo) {
226  const int FieldSizeInBits = FieldInfo[0];
227  return (((uint64_t)1) << FieldSizeInBits) - 1;
228  }
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;
232  }
233  static uint64_t maskAndShift(uint64_t Val, const BitFieldInfo FieldInfo) {
234  return maskAndShift(Val, getMask(FieldInfo), FieldInfo[1]);
235  }
236  uint64_t getFieldValue(const BitFieldInfo FieldInfo) const {
237  return getMask(FieldInfo) & (RawData >> FieldInfo[1]);
238  }
239 
240  void init(bool IsPointer, bool IsVector, uint16_t NumElements,
241  unsigned SizeInBits, unsigned AddressSpace) {
242  this->IsPointer = IsPointer;
243  this->IsVector = IsVector;
244  if (!IsVector) {
245  if (!IsPointer)
246  RawData = maskAndShift(SizeInBits, ScalarSizeFieldInfo);
247  else
248  RawData = maskAndShift(SizeInBits, PointerSizeFieldInfo) |
249  maskAndShift(AddressSpace, PointerAddressSpaceFieldInfo);
250  } else {
251  assert(NumElements > 1 && "invalid number of vector elements");
252  if (!IsPointer)
253  RawData = maskAndShift(NumElements, VectorElementsFieldInfo) |
254  maskAndShift(SizeInBits, VectorSizeFieldInfo);
255  else
256  RawData =
257  maskAndShift(NumElements, PointerVectorElementsFieldInfo) |
258  maskAndShift(SizeInBits, PointerVectorSizeFieldInfo) |
259  maskAndShift(AddressSpace, PointerVectorAddressSpaceFieldInfo);
260  }
261  }
262 
263  uint64_t getUniqueRAWLLTData() const {
264  return ((uint64_t)RawData) << 2 | ((uint64_t)IsPointer) << 1 |
265  ((uint64_t)IsVector);
266  }
267 };
268 
269 inline raw_ostream& operator<<(raw_ostream &OS, const LLT &Ty) {
270  Ty.print(OS);
271  return OS;
272 }
273 
274 template<> struct DenseMapInfo<LLT> {
275  static inline LLT getEmptyKey() {
276  LLT Invalid;
277  Invalid.IsPointer = true;
278  return Invalid;
279  }
280  static inline LLT getTombstoneKey() {
281  LLT Invalid;
282  Invalid.IsVector = true;
283  return Invalid;
284  }
285  static inline unsigned getHashValue(const LLT &Ty) {
286  uint64_t Val = Ty.getUniqueRAWLLTData();
288  }
289  static bool isEqual(const LLT &LHS, const LLT &RHS) {
290  return LHS == RHS;
291  }
292 };
293 
294 }
295 
296 #endif // LLVM_SUPPORT_LOWLEVELTYPEIMPL_H
Type
MessagePack types as defined in the standard, with the exception of Integer being divided into a sign...
Definition: MsgPackReader.h:48
static LLT vector(uint16_t NumElements, LLT ScalarTy)
Get a low-level vector of some number of elements and element type.
static LLT pointer(unsigned AddressSpace, unsigned SizeInBits)
Get a low-level pointer in the given address space.
This class represents lattice values for constants.
Definition: AllocatorList.h:23
unsigned getScalarSizeInBits() const
bool isScalar() const
LLT getScalarType() const
static LLT scalarOrVector(uint16_t NumElements, unsigned ScalarSize)
bool operator!=(const LLT &RHS) const
LLT(bool isPointer, bool isVector, uint16_t NumElements, unsigned SizeInBits, unsigned AddressSpace)
bool isVector() const
LLT changeElementType(LLT NewEltTy) const
If this type is a vector, return a vector with the same number of elements but the new element type...
LLT getElementType() const
Returns the vector&#39;s element type. Only valid for vector types.
static LLT scalar(unsigned SizeInBits)
Get a low-level scalar or aggregate "bag of bits".
initializer< Ty > init(const Ty &Val)
Definition: CommandLine.h:423
static LLT scalarOrVector(uint16_t NumElements, LLT ScalarTy)
Machine Value Type.
bool isValid() const
unsigned getAddressSpace() const
static bool isEqual(const LLT &LHS, const LLT &RHS)
void print(raw_ostream &OS) const
AddressSpace
Definition: NVPTXBaseInfo.h:21
unsigned getSizeInBits() const
Returns the total size of the type. Must only be called on sized types.
static unsigned getHashValue(const LLT &Ty)
bool isPointer() const
bool operator==(const LLT &RHS) const
raw_ostream & operator<<(raw_ostream &OS, const APInt &I)
Definition: APInt.h:2038
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
LLT changeElementSize(unsigned NewEltSize) const
If this type is a vector, return a vector with the same number of elements but the new element size...
uint16_t getNumElements() const
Returns the number of elements in a vector LLT.
std::underlying_type< E >::type Mask()
Get a bitmask with 1s in all places up to the high-order bit of E&#39;s largest value.
Definition: BitmaskEnum.h:80
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:45
static LLT vector(uint16_t NumElements, unsigned ScalarSizeInBits)
Get a low-level vector of some number of elements and element width.