Coverage Report

Created: 2019-02-21 13:17

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/include/llvm/Support/LowLevelTypeImpl.h
Line
Count
Source (jump to first uncovered line)
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
0
class LLT {
Unexecuted instantiation: llvm::LLT::operator=(llvm::LLT const&)
Unexecuted instantiation: llvm::LLT::operator=(llvm::LLT&&)
40
public:
41
  /// Get a low-level scalar or aggregate "bag of bits".
42
19.5M
  static LLT scalar(unsigned SizeInBits) {
43
19.5M
    assert(SizeInBits > 0 && "invalid scalar size");
44
19.5M
    return LLT{/*isPointer=*/false, /*isVector=*/false, /*NumElements=*/0,
45
19.5M
               SizeInBits, /*AddressSpace=*/0};
46
19.5M
  }
47
48
  /// Get a low-level pointer in the given address space.
49
13.3M
  static LLT pointer(unsigned AddressSpace, unsigned SizeInBits) {
50
13.3M
    assert(SizeInBits > 0 && "invalid pointer size");
51
13.3M
    return LLT{/*isPointer=*/true, /*isVector=*/false, /*NumElements=*/0,
52
13.3M
               SizeInBits, AddressSpace};
53
13.3M
  }
54
55
  /// Get a low-level vector of some number of elements and element width.
56
  /// \p NumElements must be at least 2.
57
4.80M
  static LLT vector(uint16_t NumElements, unsigned ScalarSizeInBits) {
58
4.80M
    assert(NumElements > 1 && "invalid number of vector elements");
59
4.80M
    assert(ScalarSizeInBits > 0 && "invalid vector element size");
60
4.80M
    return LLT{/*isPointer=*/false, /*isVector=*/true, NumElements,
61
4.80M
               ScalarSizeInBits, /*AddressSpace=*/0};
62
4.80M
  }
63
64
  /// Get a low-level vector of some number of elements and element type.
65
337k
  static LLT vector(uint16_t NumElements, LLT ScalarTy) {
66
337k
    assert(NumElements > 1 && "invalid number of vector elements");
67
337k
    assert(!ScalarTy.isVector() && "invalid vector element type");
68
337k
    return LLT{ScalarTy.isPointer(), /*isVector=*/true, NumElements,
69
337k
               ScalarTy.getSizeInBits(),
70
337k
               ScalarTy.isPointer() ? 
ScalarTy.getAddressSpace()18.2k
:
0319k
};
71
337k
  }
72
73
54.0k
  static LLT scalarOrVector(uint16_t NumElements, LLT ScalarTy) {
74
54.0k
    return NumElements == 1 ? 
ScalarTy41.1k
:
LLT::vector(NumElements, ScalarTy)12.9k
;
75
54.0k
  }
76
77
14
  static LLT scalarOrVector(uint16_t NumElements, unsigned ScalarSize) {
78
14
    return scalarOrVector(NumElements, LLT::scalar(ScalarSize));
79
14
  }
80
81
  explicit LLT(bool isPointer, bool isVector, uint16_t NumElements,
82
38.0M
               unsigned SizeInBits, unsigned AddressSpace) {
83
38.0M
    init(isPointer, isVector, NumElements, SizeInBits, AddressSpace);
84
38.0M
  }
85
58.0M
  explicit LLT() : IsPointer(false), IsVector(false), RawData(0) {}
86
87
  explicit LLT(MVT VT);
88
89
215M
  bool isValid() const { return RawData != 0; }
90
91
54.2M
  bool isScalar() const 
{ return 54.2M
isValid()54.2M
&& !IsPointer &&
!IsVector53.5M
; }
92
93
73.3M
  bool isPointer() const 
{ return 73.3M
isValid()73.3M
&& IsPointer &&
!IsVector20.3M
; }
94
95
26.6M
  bool isVector() const { return isValid() && 
IsVector26.6M
; }
96
97
  /// Returns the number of elements in a vector LLT. Must only be called on
98
  /// vector types.
99
2.10M
  uint16_t getNumElements() const {
100
2.10M
    assert(IsVector && "cannot get number of elements on scalar/aggregate");
101
2.10M
    if (!IsPointer)
102
2.10M
      return getFieldValue(VectorElementsFieldInfo);
103
1.09k
    else
104
1.09k
      return getFieldValue(PointerVectorElementsFieldInfo);
105
2.10M
  }
106
107
  /// Returns the total size of the type. Must only be called on sized types.
108
63.8M
  unsigned getSizeInBits() const {
109
63.8M
    if (isPointer() || 
isScalar()44.6M
)
110
62.3M
      return getScalarSizeInBits();
111
1.47M
    return getScalarSizeInBits() * getNumElements();
112
1.47M
  }
113
114
1.94M
  LLT getScalarType() const {
115
1.94M
    return isVector() ? 
getElementType()772
:
*this1.94M
;
116
1.94M
  }
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
78
  LLT changeElementType(LLT NewEltTy) const {
121
78
    return isVector() ? 
LLT::vector(getNumElements(), NewEltTy)62
:
NewEltTy16
;
122
78
  }
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
94
  LLT changeElementSize(unsigned NewEltSize) const {
128
94
    assert(!getScalarType().isPointer() &&
129
94
           "invalid to directly change element size for pointers");
130
94
    return isVector() ? 
LLT::vector(getNumElements(), NewEltSize)24
131
94
                      : 
LLT::scalar(NewEltSize)70
;
132
94
  }
133
134
65.5M
  unsigned getScalarSizeInBits() const {
135
65.5M
    assert(RawData != 0 && "Invalid Type");
136
65.5M
    if (!IsVector) {
137
62.9M
      if (!IsPointer)
138
43.7M
        return getFieldValue(ScalarSizeFieldInfo);
139
19.1M
      else
140
19.1M
        return getFieldValue(PointerSizeFieldInfo);
141
2.54M
    } else {
142
2.54M
      if (!IsPointer)
143
2.54M
        return getFieldValue(VectorSizeFieldInfo);
144
1.81k
      else
145
1.81k
        return getFieldValue(PointerVectorSizeFieldInfo);
146
2.54M
    }
147
65.5M
  }
148
149
192k
  unsigned getAddressSpace() const {
150
192k
    assert(RawData != 0 && "Invalid Type");
151
192k
    assert(IsPointer && "cannot get address space of non-pointer type");
152
192k
    if (!IsVector)
153
191k
      return getFieldValue(PointerAddressSpaceFieldInfo);
154
1.00k
    else
155
1.00k
      return getFieldValue(PointerVectorAddressSpaceFieldInfo);
156
192k
  }
157
158
  /// Returns the vector's element type. Only valid for vector types.
159
1.06M
  LLT getElementType() const {
160
1.06M
    assert(isVector() && "cannot get element type of scalar/aggregate");
161
1.06M
    if (IsPointer)
162
1.00k
      return pointer(getAddressSpace(), getScalarSizeInBits());
163
1.06M
    else
164
1.06M
      return scalar(getScalarSizeInBits());
165
1.06M
  }
166
167
  void print(raw_ostream &OS) const;
168
169
148M
  bool operator==(const LLT &RHS) const {
170
148M
    return IsPointer == RHS.IsPointer && 
IsVector == RHS.IsVector127M
&&
171
148M
           
RHS.RawData == RawData119M
;
172
148M
  }
173
174
3.67M
  bool operator!=(const LLT &RHS) const { return !(*this == RHS); }
175
176
  friend struct DenseMapInfo<LLT>;
177
  friend class GISelInstProfileBuilder;
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
127M
  static uint64_t getMask(const BitFieldInfo FieldInfo) {
226
127M
    const int FieldSizeInBits = FieldInfo[0];
227
127M
    return (((uint64_t)1) << FieldSizeInBits) - 1;
228
127M
  }
229
59.5M
  static uint64_t maskAndShift(uint64_t Val, uint64_t Mask, uint8_t Shift) {
230
59.5M
    assert(Val <= Mask && "Value too large for field");
231
59.5M
    return (Val & Mask) << Shift;
232
59.5M
  }
233
59.5M
  static uint64_t maskAndShift(uint64_t Val, const BitFieldInfo FieldInfo) {
234
59.5M
    return maskAndShift(Val, getMask(FieldInfo), FieldInfo[1]);
235
59.5M
  }
236
67.8M
  uint64_t getFieldValue(const BitFieldInfo FieldInfo) const {
237
67.8M
    return getMask(FieldInfo) & (RawData >> FieldInfo[1]);
238
67.8M
  }
239
240
  void init(bool IsPointer, bool IsVector, uint16_t NumElements,
241
41.1M
            unsigned SizeInBits, unsigned AddressSpace) {
242
41.1M
    this->IsPointer = IsPointer;
243
41.1M
    this->IsVector = IsVector;
244
41.1M
    if (!IsVector) {
245
35.9M
      if (!IsPointer)
246
22.6M
        RawData = maskAndShift(SizeInBits, ScalarSizeFieldInfo);
247
13.3M
      else
248
13.3M
        RawData = maskAndShift(SizeInBits, PointerSizeFieldInfo) |
249
13.3M
                  maskAndShift(AddressSpace, PointerAddressSpaceFieldInfo);
250
35.9M
    } else {
251
5.14M
      assert(NumElements > 1 && "invalid number of vector elements");
252
5.14M
      if (!IsPointer)
253
5.12M
        RawData = maskAndShift(NumElements, VectorElementsFieldInfo) |
254
5.12M
                  maskAndShift(SizeInBits, VectorSizeFieldInfo);
255
18.2k
      else
256
18.2k
        RawData =
257
18.2k
            maskAndShift(NumElements, PointerVectorElementsFieldInfo) |
258
18.2k
            maskAndShift(SizeInBits, PointerVectorSizeFieldInfo) |
259
18.2k
            maskAndShift(AddressSpace, PointerVectorAddressSpaceFieldInfo);
260
5.14M
    }
261
41.1M
  }
262
263
34.8M
  uint64_t getUniqueRAWLLTData() const {
264
34.8M
    return ((uint64_t)RawData) << 2 | ((uint64_t)IsPointer) << 1 |
265
34.8M
           ((uint64_t)IsVector);
266
34.8M
  }
267
};
268
269
51.7k
inline raw_ostream& operator<<(raw_ostream &OS, const LLT &Ty) {
270
51.7k
  Ty.print(OS);
271
51.7k
  return OS;
272
51.7k
}
273
274
template<> struct DenseMapInfo<LLT> {
275
14.4M
  static inline LLT getEmptyKey() {
276
14.4M
    LLT Invalid;
277
14.4M
    Invalid.IsPointer = true;
278
14.4M
    return Invalid;
279
14.4M
  }
280
10.7M
  static inline LLT getTombstoneKey() {
281
10.7M
    LLT Invalid;
282
10.7M
    Invalid.IsVector = true;
283
10.7M
    return Invalid;
284
10.7M
  }
285
7.23M
  static inline unsigned getHashValue(const LLT &Ty) {
286
7.23M
    uint64_t Val = Ty.getUniqueRAWLLTData();
287
7.23M
    return DenseMapInfo<uint64_t>::getHashValue(Val);
288
7.23M
  }
289
109M
  static bool isEqual(const LLT &LHS, const LLT &RHS) {
290
109M
    return LHS == RHS;
291
109M
  }
292
};
293
294
}
295
296
#endif // LLVM_SUPPORT_LOWLEVELTYPEIMPL_H