Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/include/llvm/CodeGen/ValueTypes.h
Line
Count
Source (jump to first uncovered line)
1
//===- CodeGen/ValueTypes.h - Low-Level Target independ. types --*- 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
// This file defines the set of low-level target independent types which various
10
// values in the code generator are.  This allows the target specific behavior
11
// of instructions to be described to target independent passes.
12
//
13
//===----------------------------------------------------------------------===//
14
15
#ifndef LLVM_CODEGEN_VALUETYPES_H
16
#define LLVM_CODEGEN_VALUETYPES_H
17
18
#include "llvm/Support/Compiler.h"
19
#include "llvm/Support/MachineValueType.h"
20
#include "llvm/Support/MathExtras.h"
21
#include <cassert>
22
#include <cstdint>
23
#include <string>
24
25
namespace llvm {
26
27
  class LLVMContext;
28
  class Type;
29
30
  /// Extended Value Type. Capable of holding value types which are not native
31
  /// for any processor (such as the i12345 type), as well as the types an MVT
32
  /// can represent.
33
  struct EVT {
34
  private:
35
    MVT V = MVT::INVALID_SIMPLE_VALUE_TYPE;
36
    Type *LLVMTy = nullptr;
37
38
  public:
39
10.2M
    constexpr EVT() = default;
40
553M
    constexpr EVT(MVT::SimpleValueType SVT) : V(SVT) {}
41
1.06G
    constexpr EVT(MVT S) : V(S) {}
42
43
331M
    bool operator==(EVT VT) const {
44
331M
      return !(*this != VT);
45
331M
    }
46
486M
    bool operator!=(EVT VT) const {
47
486M
      if (V.SimpleTy != VT.V.SimpleTy)
48
346M
        return true;
49
139M
      if (V.SimpleTy == MVT::INVALID_SIMPLE_VALUE_TYPE)
50
52.9k
        return LLVMTy != VT.LLVMTy;
51
139M
      return false;
52
139M
    }
53
54
    /// Returns the EVT that represents a floating-point type with the given
55
    /// number of bits. There are two floating-point types with 128 bits - this
56
    /// returns f128 rather than ppcf128.
57
1.25k
    static EVT getFloatingPointVT(unsigned BitWidth) {
58
1.25k
      return MVT::getFloatingPointVT(BitWidth);
59
1.25k
    }
60
61
    /// Returns the EVT that represents an integer with the given number of
62
    /// bits.
63
48.3M
    static EVT getIntegerVT(LLVMContext &Context, unsigned BitWidth) {
64
48.3M
      MVT M = MVT::getIntegerVT(BitWidth);
65
48.3M
      if (M.SimpleTy != MVT::INVALID_SIMPLE_VALUE_TYPE)
66
47.6M
        return M;
67
714k
      return getExtendedIntegerVT(Context, BitWidth);
68
714k
    }
69
70
    /// Returns the EVT that represents a vector NumElements in length, where
71
    /// each element is of type VT.
72
    static EVT getVectorVT(LLVMContext &Context, EVT VT, unsigned NumElements,
73
9.64M
                           bool IsScalable = false) {
74
9.64M
      MVT M = MVT::getVectorVT(VT.V, NumElements, IsScalable);
75
9.64M
      if (M.SimpleTy != MVT::INVALID_SIMPLE_VALUE_TYPE)
76
9.40M
        return M;
77
237k
78
237k
      assert(!IsScalable && "We don't support extended scalable types yet");
79
237k
      return getExtendedVectorVT(Context, VT, NumElements);
80
237k
    }
81
82
    /// Returns the EVT that represents a vector EC.Min elements in length,
83
    /// where each element is of type VT.
84
189k
    static EVT getVectorVT(LLVMContext &Context, EVT VT, MVT::ElementCount EC) {
85
189k
      MVT M = MVT::getVectorVT(VT.V, EC);
86
189k
      if (M.SimpleTy != MVT::INVALID_SIMPLE_VALUE_TYPE)
87
180k
        return M;
88
8.77k
      assert (!EC.Scalable && "We don't support extended scalable types yet");
89
8.77k
      return getExtendedVectorVT(Context, VT, EC.Min);
90
8.77k
    }
91
92
    /// Return a vector with the same number of elements as this vector, but
93
    /// with the element type converted to an integer type with the same
94
    /// bitwidth.
95
114k
    EVT changeVectorElementTypeToInteger() const {
96
114k
      if (!isSimple()) {
97
203
        assert (!isScalableVector() &&
98
203
                "We don't support extended scalable types yet");
99
203
        return changeExtendedVectorElementTypeToInteger();
100
203
      }
101
114k
      MVT EltTy = getSimpleVT().getVectorElementType();
102
114k
      unsigned BitWidth = EltTy.getSizeInBits();
103
114k
      MVT IntTy = MVT::getIntegerVT(BitWidth);
104
114k
      MVT VecTy = MVT::getVectorVT(IntTy, getVectorNumElements(),
105
114k
                                   isScalableVector());
106
114k
      assert(VecTy.SimpleTy != MVT::INVALID_SIMPLE_VALUE_TYPE &&
107
114k
             "Simple vector VT not representable by simple integer vector VT!");
108
114k
      return VecTy;
109
114k
    }
110
111
    /// Return the type converted to an equivalently sized integer or vector
112
    /// with integer element type. Similar to changeVectorElementTypeToInteger,
113
    /// but also handles scalars.
114
3.02k
    EVT changeTypeToInteger() {
115
3.02k
      if (isVector())
116
306
        return changeVectorElementTypeToInteger();
117
2.71k
118
2.71k
      if (isSimple())
119
2.71k
        return MVT::getIntegerVT(getSizeInBits());
120
0
121
0
      return changeExtendedTypeToInteger();
122
0
    }
123
124
    /// Test if the given EVT is simple (as opposed to being extended).
125
1.85G
    bool isSimple() const {
126
1.85G
      return V.SimpleTy != MVT::INVALID_SIMPLE_VALUE_TYPE;
127
1.85G
    }
128
129
    /// Test if the given EVT is extended (as opposed to being simple).
130
209M
    bool isExtended() const {
131
209M
      return !isSimple();
132
209M
    }
133
134
    /// Return true if this is a FP or a vector FP type.
135
19.5M
    bool isFloatingPoint() const {
136
19.5M
      return isSimple() ? 
V.isFloatingPoint()19.4M
:
isExtendedFloatingPoint()19.3k
;
137
19.5M
    }
138
139
    /// Return true if this is an integer or a vector integer type.
140
28.7M
    bool isInteger() const {
141
28.7M
      return isSimple() ? 
V.isInteger()28.6M
:
isExtendedInteger()91.1k
;
142
28.7M
    }
143
144
    /// Return true if this is an integer, but not a vector.
145
8.38M
    bool isScalarInteger() const {
146
8.38M
      return isSimple() ? 
V.isScalarInteger()8.37M
:
isExtendedScalarInteger()5.54k
;
147
8.38M
    }
148
149
    /// Return true if this is a vector value type.
150
315M
    bool isVector() const {
151
315M
      return isSimple() ? 
V.isVector()314M
:
isExtendedVector()1.24M
;
152
315M
    }
153
154
    /// Return true if this is a vector type where the runtime
155
    /// length is machine dependent
156
121k
    bool isScalableVector() const {
157
121k
      // FIXME: We don't support extended scalable types yet, because the
158
121k
      // matching IR type doesn't exist. Once it has been added, this can
159
121k
      // be changed to call isExtendedScalableVector.
160
121k
      if (!isSimple())
161
6.71k
        return false;
162
114k
      return V.isScalableVector();
163
114k
    }
164
165
    /// Return true if this is a 16-bit vector type.
166
0
    bool is16BitVector() const {
167
0
      return isSimple() ? V.is16BitVector() : isExtended16BitVector();
168
0
    }
169
170
    /// Return true if this is a 32-bit vector type.
171
0
    bool is32BitVector() const {
172
0
      return isSimple() ? V.is32BitVector() : isExtended32BitVector();
173
0
    }
174
175
    /// Return true if this is a 64-bit vector type.
176
16.0k
    bool is64BitVector() const {
177
16.0k
      return isSimple() ? V.is64BitVector() : 
isExtended64BitVector()0
;
178
16.0k
    }
179
180
    /// Return true if this is a 128-bit vector type.
181
1.16M
    bool is128BitVector() const {
182
1.16M
      return isSimple() ? 
V.is128BitVector()1.16M
:
isExtended128BitVector()568
;
183
1.16M
    }
184
185
    /// Return true if this is a 256-bit vector type.
186
3.04M
    bool is256BitVector() const {
187
3.04M
      return isSimple() ? 
V.is256BitVector()3.03M
:
isExtended256BitVector()3.34k
;
188
3.04M
    }
189
190
    /// Return true if this is a 512-bit vector type.
191
1.45M
    bool is512BitVector() const {
192
1.45M
      return isSimple() ? 
V.is512BitVector()1.45M
:
isExtended512BitVector()538
;
193
1.45M
    }
194
195
    /// Return true if this is a 1024-bit vector type.
196
0
    bool is1024BitVector() const {
197
0
      return isSimple() ? V.is1024BitVector() : isExtended1024BitVector();
198
0
    }
199
200
    /// Return true if this is a 2048-bit vector type.
201
0
    bool is2048BitVector() const {
202
0
      return isSimple() ? V.is2048BitVector() : isExtended2048BitVector();
203
0
    }
204
205
    /// Return true if this is an overloaded type for TableGen.
206
0
    bool isOverloaded() const {
207
0
      return (V==MVT::iAny || V==MVT::fAny || V==MVT::vAny || V==MVT::iPTRAny);
208
0
    }
209
210
    /// Return true if the bit size is a multiple of 8.
211
205k
    bool isByteSized() const {
212
205k
      return (getSizeInBits() & 7) == 0;
213
205k
    }
214
215
    /// Return true if the size is a power-of-two number of bytes.
216
111k
    bool isRound() const {
217
111k
      unsigned BitSize = getSizeInBits();
218
111k
      return BitSize >= 8 && 
!(BitSize & (BitSize - 1))81.6k
;
219
111k
    }
220
221
    /// Return true if this has the same number of bits as VT.
222
2.76M
    bool bitsEq(EVT VT) const {
223
2.76M
      if (EVT::operator==(VT)) 
return true2.15M
;
224
611k
      return getSizeInBits() == VT.getSizeInBits();
225
611k
    }
226
227
    /// Return true if this has more bits than VT.
228
2.32M
    bool bitsGT(EVT VT) const {
229
2.32M
      if (EVT::operator==(VT)) 
return false1.93M
;
230
394k
      return getSizeInBits() > VT.getSizeInBits();
231
394k
    }
232
233
    /// Return true if this has no less bits than VT.
234
97.1k
    bool bitsGE(EVT VT) const {
235
97.1k
      if (EVT::operator==(VT)) 
return true46.6k
;
236
50.5k
      return getSizeInBits() >= VT.getSizeInBits();
237
50.5k
    }
238
239
    /// Return true if this has less bits than VT.
240
11.4M
    bool bitsLT(EVT VT) const {
241
11.4M
      if (EVT::operator==(VT)) 
return false7.85M
;
242
3.63M
      return getSizeInBits() < VT.getSizeInBits();
243
3.63M
    }
244
245
    /// Return true if this has no more bits than VT.
246
251k
    bool bitsLE(EVT VT) const {
247
251k
      if (EVT::operator==(VT)) 
return true30.5k
;
248
220k
      return getSizeInBits() <= VT.getSizeInBits();
249
220k
    }
250
251
    /// Return the SimpleValueType held in the specified simple EVT.
252
897M
    MVT getSimpleVT() const {
253
897M
      assert(isSimple() && "Expected a SimpleValueType!");
254
897M
      return V;
255
897M
    }
256
257
    /// If this is a vector type, return the element type, otherwise return
258
    /// this.
259
141M
    EVT getScalarType() const {
260
141M
      return isVector() ? 
getVectorElementType()28.0M
:
*this113M
;
261
141M
    }
262
263
    /// Given a vector type, return the type of each element.
264
32.6M
    EVT getVectorElementType() const {
265
32.6M
      assert(isVector() && "Invalid vector type!");
266
32.6M
      if (isSimple())
267
32.2M
        return V.getVectorElementType();
268
386k
      return getExtendedVectorElementType();
269
386k
    }
270
271
    /// Given a vector type, return the number of elements it contains.
272
18.2M
    unsigned getVectorNumElements() const {
273
18.2M
      assert(isVector() && "Invalid vector type!");
274
18.2M
      if (isSimple())
275
17.5M
        return V.getVectorNumElements();
276
621k
      return getExtendedVectorNumElements();
277
621k
    }
278
279
    // Given a (possibly scalable) vector type, return the ElementCount
280
189k
    MVT::ElementCount getVectorElementCount() const {
281
189k
      assert((isVector()) && "Invalid vector type!");
282
189k
      if (isSimple())
283
182k
        return V.getVectorElementCount();
284
7.39k
285
7.39k
      assert(!isScalableVector() &&
286
7.39k
             "We don't support extended scalable types yet");
287
7.39k
      return {getExtendedVectorNumElements(), false};
288
7.39k
    }
289
290
    /// Return the size of the specified value type in bits.
291
179M
    unsigned getSizeInBits() const {
292
179M
      if (isSimple())
293
176M
        return V.getSizeInBits();
294
2.47M
      return getExtendedSizeInBits();
295
2.47M
    }
296
297
17.7M
    unsigned getScalarSizeInBits() const {
298
17.7M
      return getScalarType().getSizeInBits();
299
17.7M
    }
300
301
    /// Return the number of bytes overwritten by a store of the specified value
302
    /// type.
303
14.5M
    unsigned getStoreSize() const {
304
14.5M
      return (getSizeInBits() + 7) / 8;
305
14.5M
    }
306
307
    /// Return the number of bits overwritten by a store of the specified value
308
    /// type.
309
689k
    unsigned getStoreSizeInBits() const {
310
689k
      return getStoreSize() * 8;
311
689k
    }
312
313
    /// Rounds the bit-width of the given integer EVT up to the nearest power of
314
    /// two (and at least to eight), and returns the integer EVT with that
315
    /// number of bits.
316
336k
    EVT getRoundIntegerType(LLVMContext &Context) const {
317
336k
      assert(isInteger() && !isVector() && "Invalid integer type!");
318
336k
      unsigned BitWidth = getSizeInBits();
319
336k
      if (BitWidth <= 8)
320
47.0k
        return EVT(MVT::i8);
321
289k
      return getIntegerVT(Context, 1 << Log2_32_Ceil(BitWidth));
322
289k
    }
323
324
    /// Finds the smallest simple value type that is greater than or equal to
325
    /// half the width of this EVT. If no simple value type can be found, an
326
    /// extended integer value type of half the size (rounded up) is returned.
327
1.82k
    EVT getHalfSizedIntegerVT(LLVMContext &Context) const {
328
1.82k
      assert(isInteger() && !isVector() && "Invalid integer type!");
329
1.82k
      unsigned EVTSize = getSizeInBits();
330
1.82k
      for (unsigned IntVT = MVT::FIRST_INTEGER_VALUETYPE;
331
6.00k
          IntVT <= MVT::LAST_INTEGER_VALUETYPE; 
++IntVT4.18k
) {
332
6.00k
        EVT HalfVT = EVT((MVT::SimpleValueType)IntVT);
333
6.00k
        if (HalfVT.getSizeInBits() * 2 >= EVTSize)
334
1.82k
          return HalfVT;
335
6.00k
      }
336
1.82k
      
return getIntegerVT(Context, (EVTSize + 1) / 2)0
;
337
1.82k
    }
338
339
    /// Return a VT for an integer vector type with the size of the
340
    /// elements doubled. The typed returned may be an extended type.
341
6.49k
    EVT widenIntegerVectorElementType(LLVMContext &Context) const {
342
6.49k
      EVT EltVT = getVectorElementType();
343
6.49k
      EltVT = EVT::getIntegerVT(Context, 2 * EltVT.getSizeInBits());
344
6.49k
      return EVT::getVectorVT(Context, EltVT, getVectorElementCount());
345
6.49k
    }
346
347
    // Return a VT for a vector type with the same element type but
348
    // half the number of elements. The type returned may be an
349
    // extended type.
350
183k
    EVT getHalfNumVectorElementsVT(LLVMContext &Context) const {
351
183k
      EVT EltVT = getVectorElementType();
352
183k
      auto EltCnt = getVectorElementCount();
353
183k
      assert(!(EltCnt.Min & 1) && "Splitting vector, but not in half!");
354
183k
      return EVT::getVectorVT(Context, EltVT, EltCnt / 2);
355
183k
    }
356
357
    /// Returns true if the given vector is a power of 2.
358
193k
    bool isPow2VectorType() const {
359
193k
      unsigned NElts = getVectorNumElements();
360
193k
      return !(NElts & (NElts - 1));
361
193k
    }
362
363
    /// Widens the length of the given vector EVT up to the nearest power of 2
364
    /// and returns that type.
365
6.78k
    EVT getPow2VectorType(LLVMContext &Context) const {
366
6.78k
      if (!isPow2VectorType()) {
367
6.78k
        unsigned NElts = getVectorNumElements();
368
6.78k
        unsigned Pow2NElts = 1 <<  Log2_32_Ceil(NElts);
369
6.78k
        return EVT::getVectorVT(Context, getVectorElementType(), Pow2NElts,
370
6.78k
                                isScalableVector());
371
6.78k
      }
372
0
      else {
373
0
        return *this;
374
0
      }
375
6.78k
    }
376
377
    /// This function returns value type as a string, e.g. "i32".
378
    std::string getEVTString() const;
379
380
    /// This method returns an LLVM type corresponding to the specified EVT.
381
    /// For integer types, this returns an unsigned type. Note that this will
382
    /// abort for types that cannot be represented.
383
    Type *getTypeForEVT(LLVMContext &Context) const;
384
385
    /// Return the value type corresponding to the specified type.
386
    /// This returns all pointers as iPTR.  If HandleUnknown is true, unknown
387
    /// types are returned as Other, otherwise they are invalid.
388
    static EVT getEVT(Type *Ty, bool HandleUnknown = false);
389
390
33.2M
    intptr_t getRawBits() const {
391
33.2M
      if (isSimple())
392
33.1M
        return V.SimpleTy;
393
56.3k
      else
394
56.3k
        return (intptr_t)(LLVMTy);
395
33.2M
    }
396
397
    /// A meaningless but well-behaved order, useful for constructing
398
    /// containers.
399
    struct compareRawBits {
400
637k
      bool operator()(EVT L, EVT R) const {
401
637k
        if (L.V.SimpleTy == R.V.SimpleTy)
402
637k
          return L.LLVMTy < R.LLVMTy;
403
0
        else
404
0
          return L.V.SimpleTy < R.V.SimpleTy;
405
637k
      }
406
    };
407
408
  private:
409
    // Methods for handling the Extended-type case in functions above.
410
    // These are all out-of-line to prevent users of this header file
411
    // from having a dependency on Type.h.
412
    EVT changeExtendedTypeToInteger() const;
413
    EVT changeExtendedVectorElementTypeToInteger() const;
414
    static EVT getExtendedIntegerVT(LLVMContext &C, unsigned BitWidth);
415
    static EVT getExtendedVectorVT(LLVMContext &C, EVT VT,
416
                                   unsigned NumElements);
417
    bool isExtendedFloatingPoint() const LLVM_READONLY;
418
    bool isExtendedInteger() const LLVM_READONLY;
419
    bool isExtendedScalarInteger() const LLVM_READONLY;
420
    bool isExtendedVector() const LLVM_READONLY;
421
    bool isExtended16BitVector() const LLVM_READONLY;
422
    bool isExtended32BitVector() const LLVM_READONLY;
423
    bool isExtended64BitVector() const LLVM_READONLY;
424
    bool isExtended128BitVector() const LLVM_READONLY;
425
    bool isExtended256BitVector() const LLVM_READONLY;
426
    bool isExtended512BitVector() const LLVM_READONLY;
427
    bool isExtended1024BitVector() const LLVM_READONLY;
428
    bool isExtended2048BitVector() const LLVM_READONLY;
429
    EVT getExtendedVectorElementType() const;
430
    unsigned getExtendedVectorNumElements() const LLVM_READONLY;
431
    unsigned getExtendedSizeInBits() const LLVM_READONLY;
432
  };
433
434
} // end namespace llvm
435
436
#endif // LLVM_CODEGEN_VALUETYPES_H