Coverage Report

Created: 2018-09-23 16:00

/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
//                     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
0
class LLT {
Unexecuted instantiation: llvm::LLT::operator=(llvm::LLT const&)
Unexecuted instantiation: llvm::LLT::operator=(llvm::LLT&&)
41
public:
42
  /// Get a low-level scalar or aggregate "bag of bits".
43
18.1M
  static LLT scalar(unsigned SizeInBits) {
44
18.1M
    assert(SizeInBits > 0 && "invalid scalar size");
45
18.1M
    return LLT{/*isPointer=*/false, /*isVector=*/false, /*NumElements=*/0,
46
18.1M
               SizeInBits, /*AddressSpace=*/0};
47
18.1M
  }
48
49
  /// Get a low-level pointer in the given address space (defaulting to 0).
50
12.9M
  static LLT pointer(uint16_t AddressSpace, unsigned SizeInBits) {
51
12.9M
    assert(SizeInBits > 0 && "invalid pointer size");
52
12.9M
    return LLT{/*isPointer=*/true, /*isVector=*/false, /*NumElements=*/0,
53
12.9M
               SizeInBits, AddressSpace};
54
12.9M
  }
55
56
  /// Get a low-level vector of some number of elements and element width.
57
  /// \p NumElements must be at least 2.
58
4.48M
  static LLT vector(uint16_t NumElements, unsigned ScalarSizeInBits) {
59
4.48M
    assert(NumElements > 1 && "invalid number of vector elements");
60
4.48M
    assert(ScalarSizeInBits > 0 && "invalid vector element size");
61
4.48M
    return LLT{/*isPointer=*/false, /*isVector=*/true, NumElements,
62
4.48M
               ScalarSizeInBits, /*AddressSpace=*/0};
63
4.48M
  }
64
65
  /// Get a low-level vector of some number of elements and element type.
66
304k
  static LLT vector(uint16_t NumElements, LLT ScalarTy) {
67
304k
    assert(NumElements > 1 && "invalid number of vector elements");
68
304k
    assert(!ScalarTy.isVector() && "invalid vector element type");
69
304k
    return LLT{ScalarTy.isPointer(), /*isVector=*/true, NumElements,
70
304k
               ScalarTy.getSizeInBits(),
71
304k
               ScalarTy.isPointer() ? 
ScalarTy.getAddressSpace()620
:
0303k
};
72
304k
  }
73
74
  explicit LLT(bool isPointer, bool isVector, uint16_t NumElements,
75
35.8M
               unsigned SizeInBits, unsigned AddressSpace) {
76
35.8M
    init(isPointer, isVector, NumElements, SizeInBits, AddressSpace);
77
35.8M
  }
78
45.6M
  explicit LLT() : IsPointer(false), IsVector(false), RawData(0) {}
79
80
  explicit LLT(MVT VT);
81
82
156M
  bool isValid() const { return RawData != 0; }
83
84
44.9M
  bool isScalar() const { return isValid() && 
!IsPointer44.9M
&&
!IsVector44.9M
; }
85
86
64.1M
  bool isPointer() const 
{ return 64.1M
isValid()64.1M
&& IsPointer &&
!IsVector18.0M
; }
87
88
18.1M
  bool isVector() const { return isValid() && 
IsVector18.1M
; }
89
90
  /// Returns the number of elements in a vector LLT. Must only be called on
91
  /// vector types.
92
1.56M
  uint16_t getNumElements() const {
93
1.56M
    assert(IsVector && "cannot get number of elements on scalar/aggregate");
94
1.56M
    if (!IsPointer)
95
1.56M
      return getFieldValue(VectorElementsFieldInfo);
96
662
    else
97
662
      return getFieldValue(PointerVectorElementsFieldInfo);
98
1.56M
  }
99
100
  /// Returns the total size of the type. Must only be called on sized types.
101
55.9M
  unsigned getSizeInBits() const {
102
55.9M
    if (isPointer() || 
isScalar()38.7M
)
103
54.8M
      return getScalarSizeInBits();
104
1.08M
    return getScalarSizeInBits() * getNumElements();
105
1.08M
  }
106
107
56.7M
  unsigned getScalarSizeInBits() const {
108
56.7M
    assert(RawData != 0 && "Invalid Type");
109
56.7M
    if (!IsVector) {
110
54.8M
      if (!IsPointer)
111
37.7M
        return getFieldValue(ScalarSizeFieldInfo);
112
17.1M
      else
113
17.1M
        return getFieldValue(PointerSizeFieldInfo);
114
1.84M
    } else {
115
1.84M
      if (!IsPointer)
116
1.83M
        return getFieldValue(VectorSizeFieldInfo);
117
1.09k
      else
118
1.09k
        return getFieldValue(PointerVectorSizeFieldInfo);
119
1.84M
    }
120
56.7M
  }
121
122
155k
  unsigned getAddressSpace() const {
123
155k
    assert(RawData != 0 && "Invalid Type");
124
155k
    assert(IsPointer && "cannot get address space of non-pointer type");
125
155k
    if (!IsVector)
126
154k
      return getFieldValue(PointerAddressSpaceFieldInfo);
127
560
    else
128
560
      return getFieldValue(PointerVectorAddressSpaceFieldInfo);
129
155k
  }
130
131
  /// Returns the vector's element type. Only valid for vector types.
132
740k
  LLT getElementType() const {
133
740k
    assert(isVector() && "cannot get element type of scalar/aggregate");
134
740k
    if (IsPointer)
135
560
      return pointer(getAddressSpace(), getScalarSizeInBits());
136
740k
    else
137
740k
      return scalar(getScalarSizeInBits());
138
740k
  }
139
140
  void print(raw_ostream &OS) const;
141
142
130M
  bool operator==(const LLT &RHS) const {
143
130M
    return IsPointer == RHS.IsPointer && 
IsVector == RHS.IsVector113M
&&
144
130M
           
RHS.RawData == RawData106M
;
145
130M
  }
146
147
2.22M
  bool operator!=(const LLT &RHS) const { return !(*this == RHS); }
148
149
  friend struct DenseMapInfo<LLT>;
150
151
private:
152
  /// LLT is packed into 64 bits as follows:
153
  /// isPointer : 1
154
  /// isVector  : 1
155
  /// with 62 bits remaining for Kind-specific data, packed in bitfields
156
  /// as described below. As there isn't a simple portable way to pack bits
157
  /// into bitfields, here the different fields in the packed structure is
158
  /// described in static const *Field variables. Each of these variables
159
  /// is a 2-element array, with the first element describing the bitfield size
160
  /// and the second element describing the bitfield offset.
161
  typedef int BitFieldInfo[2];
162
  ///
163
  /// This is how the bitfields are packed per Kind:
164
  /// * Invalid:
165
  ///   gets encoded as RawData == 0, as that is an invalid encoding, since for
166
  ///   valid encodings, SizeInBits/SizeOfElement must be larger than 0.
167
  /// * Non-pointer scalar (isPointer == 0 && isVector == 0):
168
  ///   SizeInBits: 32;
169
  static const constexpr BitFieldInfo ScalarSizeFieldInfo{32, 0};
170
  /// * Pointer (isPointer == 1 && isVector == 0):
171
  ///   SizeInBits: 16;
172
  ///   AddressSpace: 23;
173
  static const constexpr BitFieldInfo PointerSizeFieldInfo{16, 0};
174
  static const constexpr BitFieldInfo PointerAddressSpaceFieldInfo{
175
      23, PointerSizeFieldInfo[0] + PointerSizeFieldInfo[1]};
176
  /// * Vector-of-non-pointer (isPointer == 0 && isVector == 1):
177
  ///   NumElements: 16;
178
  ///   SizeOfElement: 32;
179
  static const constexpr BitFieldInfo VectorElementsFieldInfo{16, 0};
180
  static const constexpr BitFieldInfo VectorSizeFieldInfo{
181
      32, VectorElementsFieldInfo[0] + VectorElementsFieldInfo[1]};
182
  /// * Vector-of-pointer (isPointer == 1 && isVector == 1):
183
  ///   NumElements: 16;
184
  ///   SizeOfElement: 16;
185
  ///   AddressSpace: 23;
186
  static const constexpr BitFieldInfo PointerVectorElementsFieldInfo{16, 0};
187
  static const constexpr BitFieldInfo PointerVectorSizeFieldInfo{
188
      16,
189
      PointerVectorElementsFieldInfo[1] + PointerVectorElementsFieldInfo[0]};
190
  static const constexpr BitFieldInfo PointerVectorAddressSpaceFieldInfo{
191
      23, PointerVectorSizeFieldInfo[1] + PointerVectorSizeFieldInfo[0]};
192
193
  uint64_t IsPointer : 1;
194
  uint64_t IsVector : 1;
195
  uint64_t RawData : 62;
196
197
115M
  static uint64_t getMask(const BitFieldInfo FieldInfo) {
198
115M
    const int FieldSizeInBits = FieldInfo[0];
199
115M
    return (((uint64_t)1) << FieldSizeInBits) - 1;
200
115M
  }
201
56.6M
  static uint64_t maskAndShift(uint64_t Val, uint64_t Mask, uint8_t Shift) {
202
56.6M
    assert(Val <= Mask && "Value too large for field");
203
56.6M
    return (Val & Mask) << Shift;
204
56.6M
  }
205
56.6M
  static uint64_t maskAndShift(uint64_t Val, const BitFieldInfo FieldInfo) {
206
56.6M
    return maskAndShift(Val, getMask(FieldInfo), FieldInfo[1]);
207
56.6M
  }
208
58.4M
  uint64_t getFieldValue(const BitFieldInfo FieldInfo) const {
209
58.4M
    return getMask(FieldInfo) & (RawData >> FieldInfo[1]);
210
58.4M
  }
211
212
  void init(bool IsPointer, bool IsVector, uint16_t NumElements,
213
38.9M
            unsigned SizeInBits, unsigned AddressSpace) {
214
38.9M
    this->IsPointer = IsPointer;
215
38.9M
    this->IsVector = IsVector;
216
38.9M
    if (!IsVector) {
217
34.1M
      if (!IsPointer)
218
21.2M
        RawData = maskAndShift(SizeInBits, ScalarSizeFieldInfo);
219
12.9M
      else
220
12.9M
        RawData = maskAndShift(SizeInBits, PointerSizeFieldInfo) |
221
12.9M
                  maskAndShift(AddressSpace, PointerAddressSpaceFieldInfo);
222
34.1M
    } else {
223
4.78M
      assert(NumElements > 1 && "invalid number of vector elements");
224
4.78M
      if (!IsPointer)
225
4.78M
        RawData = maskAndShift(NumElements, VectorElementsFieldInfo) |
226
4.78M
                  maskAndShift(SizeInBits, VectorSizeFieldInfo);
227
620
      else
228
620
        RawData =
229
620
            maskAndShift(NumElements, PointerVectorElementsFieldInfo) |
230
620
            maskAndShift(SizeInBits, PointerVectorSizeFieldInfo) |
231
620
            maskAndShift(AddressSpace, PointerVectorAddressSpaceFieldInfo);
232
4.78M
    }
233
38.9M
  }
234
};
235
236
18.5k
inline raw_ostream& operator<<(raw_ostream &OS, const LLT &Ty) {
237
18.5k
  Ty.print(OS);
238
18.5k
  return OS;
239
18.5k
}
240
241
template<> struct DenseMapInfo<LLT> {
242
11.9M
  static inline LLT getEmptyKey() {
243
11.9M
    LLT Invalid;
244
11.9M
    Invalid.IsPointer = true;
245
11.9M
    return Invalid;
246
11.9M
  }
247
8.64M
  static inline LLT getTombstoneKey() {
248
8.64M
    LLT Invalid;
249
8.64M
    Invalid.IsVector = true;
250
8.64M
    return Invalid;
251
8.64M
  }
252
5.49M
  static inline unsigned getHashValue(const LLT &Ty) {
253
5.49M
    uint64_t Val = ((uint64_t)Ty.RawData) << 2 | ((uint64_t)Ty.IsPointer) << 1 |
254
5.49M
                   ((uint64_t)Ty.IsVector);
255
5.49M
    return DenseMapInfo<uint64_t>::getHashValue(Val);
256
5.49M
  }
257
97.3M
  static bool isEqual(const LLT &LHS, const LLT &RHS) {
258
97.3M
    return LHS == RHS;
259
97.3M
  }
260
};
261
262
}
263
264
#endif // LLVM_SUPPORT_LOWLEVELTYPEIMPL_H