Coverage Report

Created: 2019-03-24 22:13

/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
0
class LLVM_NODISCARD APSInt : public APInt {
Unexecuted instantiation: llvm::APSInt::operator=(llvm::APSInt const&)
Unexecuted instantiation: llvm::APSInt::operator=(llvm::APSInt&&)
22
  bool IsUnsigned;
23
24
public:
25
  /// Default constructor that creates an uninitialized APInt.
26
10.4M
  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
41.4M
   : APInt(BitWidth, 0), IsUnsigned(isUnsigned) {}
32
33
  explicit APSInt(APInt I, bool isUnsigned = true)
34
52.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
270k
  APSInt &operator=(APInt RHS) {
46
270k
    // Retain our current sign.
47
270k
    APInt::operator=(std::move(RHS));
48
270k
    return *this;
49
270k
  }
50
51
8.23M
  APSInt &operator=(uint64_t RHS) {
52
8.23M
    // Retain our current sign.
53
8.23M
    APInt::operator=(RHS);
54
8.23M
    return *this;
55
8.23M
  }
56
57
  // Query sign information.
58
33.3M
  bool isSigned() const { return !IsUnsigned; }
59
9.42M
  bool isUnsigned() const { return IsUnsigned; }
60
21.8M
  void setIsUnsigned(bool Val) { IsUnsigned = Val; }
61
1.22M
  void setIsSigned(bool Val) { IsUnsigned = !Val; }
62
63
  /// toString - Append this APSInt to the specified SmallString.
64
308
  void toString(SmallVectorImpl<char> &Str, unsigned Radix = 10) const {
65
308
    APInt::toString(Str, Radix, isSigned());
66
308
  }
67
  /// toString - Converts an APInt to a std::string.  This is an inefficient
68
  /// method; you should prefer passing in a SmallString instead.
69
2.21k
  std::string toString(unsigned Radix) const {
70
2.21k
    return APInt::toString(Radix, isSigned());
71
2.21k
  }
72
  using APInt::toString;
73
74
  /// Get the correctly-extended \c int64_t value.
75
30.4k
  int64_t getExtValue() const {
76
30.4k
    assert(getMinSignedBits() <= 64 && "Too many bits for int64_t");
77
30.4k
    return isSigned() ? 
getSExtValue()4.35k
:
getZExtValue()26.1k
;
78
30.4k
  }
79
80
1.33M
  APSInt trunc(uint32_t width) const {
81
1.33M
    return APSInt(APInt::trunc(width), IsUnsigned);
82
1.33M
  }
83
84
5.59M
  APSInt extend(uint32_t width) const {
85
5.59M
    if (IsUnsigned)
86
641k
      return APSInt(zext(width), IsUnsigned);
87
4.95M
    else
88
4.95M
      return APSInt(sext(width), IsUnsigned);
89
5.59M
  }
90
91
9.38M
  APSInt extOrTrunc(uint32_t width) const {
92
9.38M
    if (IsUnsigned)
93
5.31M
      return APSInt(zextOrTrunc(width), IsUnsigned);
94
4.06M
    else
95
4.06M
      return APSInt(sextOrTrunc(width), IsUnsigned);
96
9.38M
  }
97
98
0
  const APSInt &operator%=(const APSInt &RHS) {
99
0
    assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
100
0
    if (IsUnsigned)
101
0
      *this = urem(RHS);
102
0
    else
103
0
      *this = srem(RHS);
104
0
    return *this;
105
0
  }
106
0
  const APSInt &operator/=(const APSInt &RHS) {
107
0
    assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
108
0
    if (IsUnsigned)
109
0
      *this = udiv(RHS);
110
0
    else
111
0
      *this = sdiv(RHS);
112
0
    return *this;
113
0
  }
114
11.3k
  APSInt operator%(const APSInt &RHS) const {
115
11.3k
    assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
116
11.3k
    return IsUnsigned ? 
APSInt(urem(RHS), true)2.50k
:
APSInt(srem(RHS), false)8.80k
;
117
11.3k
  }
118
355k
  APSInt operator/(const APSInt &RHS) const {
119
355k
    assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
120
355k
    return IsUnsigned ? 
APSInt(udiv(RHS), true)29.5k
:
APSInt(sdiv(RHS), false)325k
;
121
355k
  }
122
123
19.6k
  APSInt operator>>(unsigned Amt) const {
124
19.6k
    return IsUnsigned ? 
APSInt(lshr(Amt), true)15.6k
:
APSInt(ashr(Amt), false)4.07k
;
125
19.6k
  }
126
526
  APSInt& operator>>=(unsigned Amt) {
127
526
    if (IsUnsigned)
128
166
      lshrInPlace(Amt);
129
360
    else
130
360
      ashrInPlace(Amt);
131
526
    return *this;
132
526
  }
133
134
1.25M
  inline bool operator<(const APSInt& RHS) const {
135
1.25M
    assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
136
1.25M
    return IsUnsigned ? 
ult(RHS)198k
:
slt(RHS)1.06M
;
137
1.25M
  }
138
1.50M
  inline bool operator>(const APSInt& RHS) const {
139
1.50M
    assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
140
1.50M
    return IsUnsigned ? 
ugt(RHS)142k
:
sgt(RHS)1.36M
;
141
1.50M
  }
142
1.33M
  inline bool operator<=(const APSInt& RHS) const {
143
1.33M
    assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
144
1.33M
    return IsUnsigned ? 
ule(RHS)476k
:
sle(RHS)854k
;
145
1.33M
  }
146
566k
  inline bool operator>=(const APSInt& RHS) const {
147
566k
    assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
148
566k
    return IsUnsigned ? 
uge(RHS)5.61k
:
sge(RHS)561k
;
149
566k
  }
150
2.90M
  inline bool operator==(const APSInt& RHS) const {
151
2.90M
    assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
152
2.90M
    return eq(RHS);
153
2.90M
  }
154
1.38M
  inline bool operator!=(const APSInt& RHS) const {
155
1.38M
    return !((*this) == RHS);
156
1.38M
  }
157
158
1.98M
  bool operator==(int64_t RHS) const {
159
1.98M
    return compareValues(*this, get(RHS)) == 0;
160
1.98M
  }
161
9.15M
  bool operator!=(int64_t RHS) const {
162
9.15M
    return compareValues(*this, get(RHS)) != 0;
163
9.15M
  }
164
0
  bool operator<=(int64_t RHS) const {
165
0
    return compareValues(*this, get(RHS)) <= 0;
166
0
  }
167
1.16k
  bool operator>=(int64_t RHS) const {
168
1.16k
    return compareValues(*this, get(RHS)) >= 0;
169
1.16k
  }
170
98.2k
  bool operator<(int64_t RHS) const {
171
98.2k
    return compareValues(*this, get(RHS)) < 0;
172
98.2k
  }
173
139k
  bool operator>(int64_t RHS) const {
174
139k
    return compareValues(*this, get(RHS)) > 0;
175
139k
  }
176
177
  // The remaining operators just wrap the logic of APInt, but retain the
178
  // signedness information.
179
180
200k
  APSInt operator<<(unsigned Bits) const {
181
200k
    return APSInt(static_cast<const APInt&>(*this) << Bits, IsUnsigned);
182
200k
  }
183
273
  APSInt& operator<<=(unsigned Amt) {
184
273
    static_cast<APInt&>(*this) <<= Amt;
185
273
    return *this;
186
273
  }
187
188
1.72M
  APSInt& operator++() {
189
1.72M
    ++(static_cast<APInt&>(*this));
190
1.72M
    return *this;
191
1.72M
  }
192
81.6k
  APSInt& operator--() {
193
81.6k
    --(static_cast<APInt&>(*this));
194
81.6k
    return *this;
195
81.6k
  }
196
0
  APSInt operator++(int) {
197
0
    return APSInt(++static_cast<APInt&>(*this), IsUnsigned);
198
0
  }
199
0
  APSInt operator--(int) {
200
0
    return APSInt(--static_cast<APInt&>(*this), IsUnsigned);
201
0
  }
202
154k
  APSInt operator-() const {
203
154k
    return APSInt(-static_cast<const APInt&>(*this), IsUnsigned);
204
154k
  }
205
108
  APSInt& operator+=(const APSInt& RHS) {
206
108
    assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
207
108
    static_cast<APInt&>(*this) += RHS;
208
108
    return *this;
209
108
  }
210
30
  APSInt& operator-=(const APSInt& RHS) {
211
30
    assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
212
30
    static_cast<APInt&>(*this) -= RHS;
213
30
    return *this;
214
30
  }
215
17
  APSInt& operator*=(const APSInt& RHS) {
216
17
    assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
217
17
    static_cast<APInt&>(*this) *= RHS;
218
17
    return *this;
219
17
  }
220
20
  APSInt& operator&=(const APSInt& RHS) {
221
20
    assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
222
20
    static_cast<APInt&>(*this) &= RHS;
223
20
    return *this;
224
20
  }
225
0
  APSInt& operator|=(const APSInt& RHS) {
226
0
    assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
227
0
    static_cast<APInt&>(*this) |= RHS;
228
0
    return *this;
229
0
  }
230
0
  APSInt& operator^=(const APSInt& RHS) {
231
0
    assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
232
0
    static_cast<APInt&>(*this) ^= RHS;
233
0
    return *this;
234
0
  }
235
236
26.9k
  APSInt operator&(const APSInt& RHS) const {
237
26.9k
    assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
238
26.9k
    return APSInt(static_cast<const APInt&>(*this) & RHS, IsUnsigned);
239
26.9k
  }
240
241
58.5k
  APSInt operator|(const APSInt& RHS) const {
242
58.5k
    assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
243
58.5k
    return APSInt(static_cast<const APInt&>(*this) | RHS, IsUnsigned);
244
58.5k
  }
245
246
7.44k
  APSInt operator^(const APSInt &RHS) const {
247
7.44k
    assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
248
7.44k
    return APSInt(static_cast<const APInt&>(*this) ^ RHS, IsUnsigned);
249
7.44k
  }
250
251
159k
  APSInt operator*(const APSInt& RHS) const {
252
159k
    assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
253
159k
    return APSInt(static_cast<const APInt&>(*this) * RHS, IsUnsigned);
254
159k
  }
255
411k
  APSInt operator+(const APSInt& RHS) const {
256
411k
    assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
257
411k
    return APSInt(static_cast<const APInt&>(*this) + RHS, IsUnsigned);
258
411k
  }
259
1.09M
  APSInt operator-(const APSInt& RHS) const {
260
1.09M
    assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
261
1.09M
    return APSInt(static_cast<const APInt&>(*this) - RHS, IsUnsigned);
262
1.09M
  }
263
22.0k
  APSInt operator~() const {
264
22.0k
    return APSInt(~static_cast<const APInt&>(*this), IsUnsigned);
265
22.0k
  }
266
267
  /// getMaxValue - Return the APSInt representing the maximum integer value
268
  ///  with the given bit width and signedness.
269
415k
  static APSInt getMaxValue(uint32_t numBits, bool Unsigned) {
270
415k
    return APSInt(Unsigned ? 
APInt::getMaxValue(numBits)115k
271
415k
                           : 
APInt::getSignedMaxValue(numBits)299k
, Unsigned);
272
415k
  }
273
274
  /// getMinValue - Return the APSInt representing the minimum integer value
275
  ///  with the given bit width and signedness.
276
446k
  static APSInt getMinValue(uint32_t numBits, bool Unsigned) {
277
446k
    return APSInt(Unsigned ? 
APInt::getMinValue(numBits)113k
278
446k
                           : 
APInt::getSignedMinValue(numBits)333k
, Unsigned);
279
446k
  }
280
281
  /// Determine if two APSInts have the same value, zero- or
282
  /// sign-extending as needed.
283
2.54k
  static bool isSameValue(const APSInt &I1, const APSInt &I2) {
284
2.54k
    return !compareValues(I1, I2);
285
2.54k
  }
286
287
  /// Compare underlying values of two numbers.
288
13.6M
  static int compareValues(const APSInt &I1, const APSInt &I2) {
289
13.6M
    if (I1.getBitWidth() == I2.getBitWidth() && 
I1.isSigned() == I2.isSigned()11.9M
)
290
11.1M
      return I1.IsUnsigned ? 
I1.compare(I2)102k
:
I1.compareSigned(I2)11.0M
;
291
2.50M
292
2.50M
    // Check for a bit-width mismatch.
293
2.50M
    if (I1.getBitWidth() > I2.getBitWidth())
294
8.61k
      return compareValues(I1, I2.extend(I1.getBitWidth()));
295
2.49M
    if (I2.getBitWidth() > I1.getBitWidth())
296
1.74M
      return compareValues(I1.extend(I2.getBitWidth()), I2);
297
750k
298
750k
    // We have a signedness mismatch. Check for negative values and do an
299
750k
    // unsigned compare if both are positive.
300
750k
    if (I1.isSigned()) {
301
851
      assert(!I2.isSigned() && "Expected signed mismatch");
302
851
      if (I1.isNegative())
303
2
        return -1;
304
749k
    } else {
305
749k
      assert(I2.isSigned() && "Expected signed mismatch");
306
749k
      if (I2.isNegative())
307
209
        return 1;
308
750k
    }
309
750k
310
750k
    return I1.compare(I2);
311
750k
  }
312
313
11.4M
  static APSInt get(int64_t X) { return APSInt(APInt(64, X), false); }
314
7.48k
  static APSInt getUnsigned(uint64_t X) { return APSInt(APInt(64, X), true); }
315
316
  /// Profile - Used to insert APSInt objects, or objects that contain APSInt
317
  ///  objects, into FoldingSets.
318
  void Profile(FoldingSetNodeID& ID) const;
319
};
320
321
2
inline bool operator==(int64_t V1, const APSInt &V2) { return V2 == V1; }
322
218k
inline bool operator!=(int64_t V1, const APSInt &V2) { return V2 != V1; }
323
0
inline bool operator<=(int64_t V1, const APSInt &V2) { return V2 >= V1; }
324
0
inline bool operator>=(int64_t V1, const APSInt &V2) { return V2 <= V1; }
325
23
inline bool operator<(int64_t V1, const APSInt &V2) { return V2 > V1; }
326
0
inline bool operator>(int64_t V1, const APSInt &V2) { return V2 < V1; }
327
328
60.5k
inline raw_ostream &operator<<(raw_ostream &OS, const APSInt &I) {
329
60.5k
  I.print(OS, I.isSigned());
330
60.5k
  return OS;
331
60.5k
}
332
333
} // end namespace llvm
334
335
#endif