Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/include/llvm/ADT/APSInt.h
Line
Count
Source (jump to first uncovered line)
1
//===-- llvm/ADT/APSInt.h - Arbitrary Precision Signed Int -----*- 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 implements the APSInt class, which is a simple class that
10
// represents an arbitrary sized integer that knows its signedness.
11
//
12
//===----------------------------------------------------------------------===//
13
14
#ifndef LLVM_ADT_APSINT_H
15
#define LLVM_ADT_APSINT_H
16
17
#include "llvm/ADT/APInt.h"
18
19
namespace llvm {
20
21
class LLVM_NODISCARD APSInt : public APInt {
22
  bool IsUnsigned;
23
24
public:
25
  /// Default constructor that creates an uninitialized APInt.
26
11.3M
  explicit APSInt() : IsUnsigned(false) {}
27
28
  /// APSInt ctor - Create an APSInt with the specified width, default to
29
  /// unsigned.
30
  explicit APSInt(uint32_t BitWidth, bool isUnsigned = true)
31
49.2M
   : APInt(BitWidth, 0), IsUnsigned(isUnsigned) {}
32
33
  explicit APSInt(APInt I, bool isUnsigned = true)
34
57.2M
   : APInt(std::move(I)), IsUnsigned(isUnsigned) {}
35
36
  /// Construct an APSInt from a string representation.
37
  ///
38
  /// This constructor interprets the string \p Str using the radix of 10.
39
  /// The interpretation stops at the end of the string. The bit width of the
40
  /// constructed APSInt is determined automatically.
41
  ///
42
  /// \param Str the string to be interpreted.
43
  explicit APSInt(StringRef Str);
44
45
  /// Determine sign of this APSInt.
46
  ///
47
  /// \returns true if this APSInt is negative, false otherwise
48
8.87M
  bool isNegative() const { return isSigned() && 
APInt::isNegative()8.80M
; }
49
50
  /// Determine if this APSInt Value is non-negative (>= 0)
51
  ///
52
  /// \returns true if this APSInt is non-negative, false otherwise
53
597k
  bool isNonNegative() const { return !isNegative(); }
54
55
  /// Determine if this APSInt Value is positive.
56
  ///
57
  /// This tests if the value of this APSInt is positive (> 0). Note
58
  /// that 0 is not a positive value.
59
  ///
60
  /// \returns true if this APSInt is positive.
61
11.0k
  bool isStrictlyPositive() const { return isNonNegative() && 
!isNullValue()9.49k
; }
62
63
274k
  APSInt &operator=(APInt RHS) {
64
274k
    // Retain our current sign.
65
274k
    APInt::operator=(std::move(RHS));
66
274k
    return *this;
67
274k
  }
68
69
9.99M
  APSInt &operator=(uint64_t RHS) {
70
9.99M
    // Retain our current sign.
71
9.99M
    APInt::operator=(RHS);
72
9.99M
    return *this;
73
9.99M
  }
74
75
  // Query sign information.
76
43.7M
  bool isSigned() const { return !IsUnsigned; }
77
11.9M
  bool isUnsigned() const { return IsUnsigned; }
78
23.3M
  void setIsUnsigned(bool Val) { IsUnsigned = Val; }
79
1.23M
  void setIsSigned(bool Val) { IsUnsigned = !Val; }
80
81
  /// toString - Append this APSInt to the specified SmallString.
82
315
  void toString(SmallVectorImpl<char> &Str, unsigned Radix = 10) const {
83
315
    APInt::toString(Str, Radix, isSigned());
84
315
  }
85
  /// toString - Converts an APInt to a std::string.  This is an inefficient
86
  /// method; you should prefer passing in a SmallString instead.
87
2.44k
  std::string toString(unsigned Radix) const {
88
2.44k
    return APInt::toString(Radix, isSigned());
89
2.44k
  }
90
  using APInt::toString;
91
92
  /// Get the correctly-extended \c int64_t value.
93
38.2k
  int64_t getExtValue() const {
94
38.2k
    assert(getMinSignedBits() <= 64 && "Too many bits for int64_t");
95
38.2k
    return isSigned() ? 
getSExtValue()4.63k
:
getZExtValue()33.6k
;
96
38.2k
  }
97
98
1.35M
  APSInt trunc(uint32_t width) const {
99
1.35M
    return APSInt(APInt::trunc(width), IsUnsigned);
100
1.35M
  }
101
102
5.78M
  APSInt extend(uint32_t width) const {
103
5.78M
    if (IsUnsigned)
104
735k
      return APSInt(zext(width), IsUnsigned);
105
5.04M
    else
106
5.04M
      return APSInt(sext(width), IsUnsigned);
107
5.78M
  }
108
109
11.1M
  APSInt extOrTrunc(uint32_t width) const {
110
11.1M
    if (IsUnsigned)
111
6.93M
      return APSInt(zextOrTrunc(width), IsUnsigned);
112
4.21M
    else
113
4.21M
      return APSInt(sextOrTrunc(width), IsUnsigned);
114
11.1M
  }
115
116
0
  const APSInt &operator%=(const APSInt &RHS) {
117
0
    assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
118
0
    if (IsUnsigned)
119
0
      *this = urem(RHS);
120
0
    else
121
0
      *this = srem(RHS);
122
0
    return *this;
123
0
  }
124
0
  const APSInt &operator/=(const APSInt &RHS) {
125
0
    assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
126
0
    if (IsUnsigned)
127
0
      *this = udiv(RHS);
128
0
    else
129
0
      *this = sdiv(RHS);
130
0
    return *this;
131
0
  }
132
11.8k
  APSInt operator%(const APSInt &RHS) const {
133
11.8k
    assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
134
11.8k
    return IsUnsigned ? 
APSInt(urem(RHS), true)3.01k
:
APSInt(srem(RHS), false)8.80k
;
135
11.8k
  }
136
362k
  APSInt operator/(const APSInt &RHS) const {
137
362k
    assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
138
362k
    return IsUnsigned ? 
APSInt(udiv(RHS), true)33.5k
:
APSInt(sdiv(RHS), false)329k
;
139
362k
  }
140
141
22.1k
  APSInt operator>>(unsigned Amt) const {
142
22.1k
    return IsUnsigned ? 
APSInt(lshr(Amt), true)18.0k
:
APSInt(ashr(Amt), false)4.10k
;
143
22.1k
  }
144
526
  APSInt& operator>>=(unsigned Amt) {
145
526
    if (IsUnsigned)
146
166
      lshrInPlace(Amt);
147
360
    else
148
360
      ashrInPlace(Amt);
149
526
    return *this;
150
526
  }
151
152
1.27M
  inline bool operator<(const APSInt& RHS) const {
153
1.27M
    assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
154
1.27M
    return IsUnsigned ? 
ult(RHS)204k
:
slt(RHS)1.06M
;
155
1.27M
  }
156
1.53M
  inline bool operator>(const APSInt& RHS) const {
157
1.53M
    assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
158
1.53M
    return IsUnsigned ? 
ugt(RHS)147k
:
sgt(RHS)1.38M
;
159
1.53M
  }
160
1.35M
  inline bool operator<=(const APSInt& RHS) const {
161
1.35M
    assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
162
1.35M
    return IsUnsigned ? 
ule(RHS)498k
:
sle(RHS)860k
;
163
1.35M
  }
164
568k
  inline bool operator>=(const APSInt& RHS) const {
165
568k
    assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
166
568k
    return IsUnsigned ? 
uge(RHS)6.89k
:
sge(RHS)561k
;
167
568k
  }
168
2.95M
  inline bool operator==(const APSInt& RHS) const {
169
2.95M
    assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
170
2.95M
    return eq(RHS);
171
2.95M
  }
172
1.41M
  inline bool operator!=(const APSInt& RHS) const {
173
1.41M
    return !((*this) == RHS);
174
1.41M
  }
175
176
2.04M
  bool operator==(int64_t RHS) const {
177
2.04M
    return compareValues(*this, get(RHS)) == 0;
178
2.04M
  }
179
9.61M
  bool operator!=(int64_t RHS) const {
180
9.61M
    return compareValues(*this, get(RHS)) != 0;
181
9.61M
  }
182
0
  bool operator<=(int64_t RHS) const {
183
0
    return compareValues(*this, get(RHS)) <= 0;
184
0
  }
185
1.23k
  bool operator>=(int64_t RHS) const {
186
1.23k
    return compareValues(*this, get(RHS)) >= 0;
187
1.23k
  }
188
106k
  bool operator<(int64_t RHS) const {
189
106k
    return compareValues(*this, get(RHS)) < 0;
190
106k
  }
191
148k
  bool operator>(int64_t RHS) const {
192
148k
    return compareValues(*this, get(RHS)) > 0;
193
148k
  }
194
195
  // The remaining operators just wrap the logic of APInt, but retain the
196
  // signedness information.
197
198
214k
  APSInt operator<<(unsigned Bits) const {
199
214k
    return APSInt(static_cast<const APInt&>(*this) << Bits, IsUnsigned);
200
214k
  }
201
273
  APSInt& operator<<=(unsigned Amt) {
202
273
    static_cast<APInt&>(*this) <<= Amt;
203
273
    return *this;
204
273
  }
205
206
1.73M
  APSInt& operator++() {
207
1.73M
    ++(static_cast<APInt&>(*this));
208
1.73M
    return *this;
209
1.73M
  }
210
84.1k
  APSInt& operator--() {
211
84.1k
    --(static_cast<APInt&>(*this));
212
84.1k
    return *this;
213
84.1k
  }
214
0
  APSInt operator++(int) {
215
0
    return APSInt(++static_cast<APInt&>(*this), IsUnsigned);
216
0
  }
217
0
  APSInt operator--(int) {
218
0
    return APSInt(--static_cast<APInt&>(*this), IsUnsigned);
219
0
  }
220
158k
  APSInt operator-() const {
221
158k
    return APSInt(-static_cast<const APInt&>(*this), IsUnsigned);
222
158k
  }
223
108
  APSInt& operator+=(const APSInt& RHS) {
224
108
    assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
225
108
    static_cast<APInt&>(*this) += RHS;
226
108
    return *this;
227
108
  }
228
30
  APSInt& operator-=(const APSInt& RHS) {
229
30
    assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
230
30
    static_cast<APInt&>(*this) -= RHS;
231
30
    return *this;
232
30
  }
233
17
  APSInt& operator*=(const APSInt& RHS) {
234
17
    assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
235
17
    static_cast<APInt&>(*this) *= RHS;
236
17
    return *this;
237
17
  }
238
20
  APSInt& operator&=(const APSInt& RHS) {
239
20
    assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
240
20
    static_cast<APInt&>(*this) &= RHS;
241
20
    return *this;
242
20
  }
243
0
  APSInt& operator|=(const APSInt& RHS) {
244
0
    assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
245
0
    static_cast<APInt&>(*this) |= RHS;
246
0
    return *this;
247
0
  }
248
0
  APSInt& operator^=(const APSInt& RHS) {
249
0
    assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
250
0
    static_cast<APInt&>(*this) ^= RHS;
251
0
    return *this;
252
0
  }
253
254
32.0k
  APSInt operator&(const APSInt& RHS) const {
255
32.0k
    assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
256
32.0k
    return APSInt(static_cast<const APInt&>(*this) & RHS, IsUnsigned);
257
32.0k
  }
258
259
59.3k
  APSInt operator|(const APSInt& RHS) const {
260
59.3k
    assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
261
59.3k
    return APSInt(static_cast<const APInt&>(*this) | RHS, IsUnsigned);
262
59.3k
  }
263
264
7.45k
  APSInt operator^(const APSInt &RHS) const {
265
7.45k
    assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
266
7.45k
    return APSInt(static_cast<const APInt&>(*this) ^ RHS, IsUnsigned);
267
7.45k
  }
268
269
165k
  APSInt operator*(const APSInt& RHS) const {
270
165k
    assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
271
165k
    return APSInt(static_cast<const APInt&>(*this) * RHS, IsUnsigned);
272
165k
  }
273
420k
  APSInt operator+(const APSInt& RHS) const {
274
420k
    assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
275
420k
    return APSInt(static_cast<const APInt&>(*this) + RHS, IsUnsigned);
276
420k
  }
277
1.11M
  APSInt operator-(const APSInt& RHS) const {
278
1.11M
    assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
279
1.11M
    return APSInt(static_cast<const APInt&>(*this) - RHS, IsUnsigned);
280
1.11M
  }
281
22.5k
  APSInt operator~() const {
282
22.5k
    return APSInt(~static_cast<const APInt&>(*this), IsUnsigned);
283
22.5k
  }
284
285
  /// getMaxValue - Return the APSInt representing the maximum integer value
286
  ///  with the given bit width and signedness.
287
424k
  static APSInt getMaxValue(uint32_t numBits, bool Unsigned) {
288
424k
    return APSInt(Unsigned ? 
APInt::getMaxValue(numBits)119k
289
424k
                           : 
APInt::getSignedMaxValue(numBits)304k
, Unsigned);
290
424k
  }
291
292
  /// getMinValue - Return the APSInt representing the minimum integer value
293
  ///  with the given bit width and signedness.
294
455k
  static APSInt getMinValue(uint32_t numBits, bool Unsigned) {
295
455k
    return APSInt(Unsigned ? 
APInt::getMinValue(numBits)117k
296
455k
                           : 
APInt::getSignedMinValue(numBits)338k
, Unsigned);
297
455k
  }
298
299
  /// Determine if two APSInts have the same value, zero- or
300
  /// sign-extending as needed.
301
2.63k
  static bool isSameValue(const APSInt &I1, const APSInt &I2) {
302
2.63k
    return !compareValues(I1, I2);
303
2.63k
  }
304
305
  /// Compare underlying values of two numbers.
306
14.3M
  static int compareValues(const APSInt &I1, const APSInt &I2) {
307
14.3M
    if (I1.getBitWidth() == I2.getBitWidth() && 
I1.isSigned() == I2.isSigned()12.4M
)
308
11.5M
      return I1.IsUnsigned ? 
I1.compare(I2)104k
:
I1.compareSigned(I2)11.4M
;
309
2.73M
310
2.73M
    // Check for a bit-width mismatch.
311
2.73M
    if (I1.getBitWidth() > I2.getBitWidth())
312
9.30k
      return compareValues(I1, I2.extend(I1.getBitWidth()));
313
2.72M
    if (I2.getBitWidth() > I1.getBitWidth())
314
1.87M
      return compareValues(I1.extend(I2.getBitWidth()), I2);
315
857k
316
857k
    // We have a signedness mismatch. Check for negative values and do an
317
857k
    // unsigned compare if both are positive.
318
857k
    if (I1.isSigned()) {
319
866
      assert(!I2.isSigned() && "Expected signed mismatch");
320
866
      if (I1.isNegative())
321
2
        return -1;
322
856k
    } else {
323
856k
      assert(I2.isSigned() && "Expected signed mismatch");
324
856k
      if (I2.isNegative())
325
236
        return 1;
326
857k
    }
327
857k
328
857k
    return I1.compare(I2);
329
857k
  }
330
331
11.9M
  static APSInt get(int64_t X) { return APSInt(APInt(64, X), false); }
332
13.2k
  static APSInt getUnsigned(uint64_t X) { return APSInt(APInt(64, X), true); }
333
334
  /// Profile - Used to insert APSInt objects, or objects that contain APSInt
335
  ///  objects, into FoldingSets.
336
  void Profile(FoldingSetNodeID& ID) const;
337
};
338
339
2
inline bool operator==(int64_t V1, const APSInt &V2) { return V2 == V1; }
340
235k
inline bool operator!=(int64_t V1, const APSInt &V2) { return V2 != V1; }
341
0
inline bool operator<=(int64_t V1, const APSInt &V2) { return V2 >= V1; }
342
0
inline bool operator>=(int64_t V1, const APSInt &V2) { return V2 <= V1; }
343
23
inline bool operator<(int64_t V1, const APSInt &V2) { return V2 > V1; }
344
0
inline bool operator>(int64_t V1, const APSInt &V2) { return V2 < V1; }
345
346
72.4k
inline raw_ostream &operator<<(raw_ostream &OS, const APSInt &I) {
347
72.4k
  I.print(OS, I.isSigned());
348
72.4k
  return OS;
349
72.4k
}
350
351
} // end namespace llvm
352
353
#endif