Coverage Report

Created: 2018-12-14 11:24

/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
//                     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
// This file implements the APSInt class, which is a simple class that
11
// represents an arbitrary sized integer that knows its signedness.
12
//
13
//===----------------------------------------------------------------------===//
14
15
#ifndef LLVM_ADT_APSINT_H
16
#define LLVM_ADT_APSINT_H
17
18
#include "llvm/ADT/APInt.h"
19
20
namespace llvm {
21
22
0
class LLVM_NODISCARD APSInt : public APInt {
Unexecuted instantiation: llvm::APSInt::operator=(llvm::APSInt const&)
Unexecuted instantiation: llvm::APSInt::operator=(llvm::APSInt&&)
23
  bool IsUnsigned;
24
25
public:
26
  /// Default constructor that creates an uninitialized APInt.
27
10.0M
  explicit APSInt() : IsUnsigned(false) {}
28
29
  /// APSInt ctor - Create an APSInt with the specified width, default to
30
  /// unsigned.
31
  explicit APSInt(uint32_t BitWidth, bool isUnsigned = true)
32
40.3M
   : APInt(BitWidth, 0), IsUnsigned(isUnsigned) {}
33
34
  explicit APSInt(APInt I, bool isUnsigned = true)
35
50.9M
   : APInt(std::move(I)), IsUnsigned(isUnsigned) {}
36
37
  /// Construct an APSInt from a string representation.
38
  ///
39
  /// This constructor interprets the string \p Str using the radix of 10.
40
  /// The interpretation stops at the end of the string. The bit width of the
41
  /// constructed APSInt is determined automatically.
42
  ///
43
  /// \param Str the string to be interpreted.
44
  explicit APSInt(StringRef Str);
45
46
268k
  APSInt &operator=(APInt RHS) {
47
268k
    // Retain our current sign.
48
268k
    APInt::operator=(std::move(RHS));
49
268k
    return *this;
50
268k
  }
51
52
8.08M
  APSInt &operator=(uint64_t RHS) {
53
8.08M
    // Retain our current sign.
54
8.08M
    APInt::operator=(RHS);
55
8.08M
    return *this;
56
8.08M
  }
57
58
  // Query sign information.
59
32.7M
  bool isSigned() const { return !IsUnsigned; }
60
9.26M
  bool isUnsigned() const { return IsUnsigned; }
61
21.5M
  void setIsUnsigned(bool Val) { IsUnsigned = Val; }
62
1.18M
  void setIsSigned(bool Val) { IsUnsigned = !Val; }
63
64
  /// toString - Append this APSInt to the specified SmallString.
65
289
  void toString(SmallVectorImpl<char> &Str, unsigned Radix = 10) const {
66
289
    APInt::toString(Str, Radix, isSigned());
67
289
  }
68
  /// toString - Converts an APInt to a std::string.  This is an inefficient
69
  /// method; you should prefer passing in a SmallString instead.
70
2.10k
  std::string toString(unsigned Radix) const {
71
2.10k
    return APInt::toString(Radix, isSigned());
72
2.10k
  }
73
  using APInt::toString;
74
75
  /// Get the correctly-extended \c int64_t value.
76
26.0k
  int64_t getExtValue() const {
77
26.0k
    assert(getMinSignedBits() <= 64 && "Too many bits for int64_t");
78
26.0k
    return isSigned() ? 
getSExtValue()3.97k
:
getZExtValue()22.1k
;
79
26.0k
  }
80
81
1.32M
  APSInt trunc(uint32_t width) const {
82
1.32M
    return APSInt(APInt::trunc(width), IsUnsigned);
83
1.32M
  }
84
85
5.53M
  APSInt extend(uint32_t width) const {
86
5.53M
    if (IsUnsigned)
87
634k
      return APSInt(zext(width), IsUnsigned);
88
4.90M
    else
89
4.90M
      return APSInt(sext(width), IsUnsigned);
90
5.53M
  }
91
92
9.10M
  APSInt extOrTrunc(uint32_t width) const {
93
9.10M
    if (IsUnsigned)
94
5.13M
      return APSInt(zextOrTrunc(width), IsUnsigned);
95
3.96M
    else
96
3.96M
      return APSInt(sextOrTrunc(width), IsUnsigned);
97
9.10M
  }
98
99
0
  const APSInt &operator%=(const APSInt &RHS) {
100
0
    assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
101
0
    if (IsUnsigned)
102
0
      *this = urem(RHS);
103
0
    else
104
0
      *this = srem(RHS);
105
0
    return *this;
106
0
  }
107
0
  const APSInt &operator/=(const APSInt &RHS) {
108
0
    assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
109
0
    if (IsUnsigned)
110
0
      *this = udiv(RHS);
111
0
    else
112
0
      *this = sdiv(RHS);
113
0
    return *this;
114
0
  }
115
11.1k
  APSInt operator%(const APSInt &RHS) const {
116
11.1k
    assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
117
11.1k
    return IsUnsigned ? 
APSInt(urem(RHS), true)2.47k
:
APSInt(srem(RHS), false)8.66k
;
118
11.1k
  }
119
346k
  APSInt operator/(const APSInt &RHS) const {
120
346k
    assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
121
346k
    return IsUnsigned ? 
APSInt(udiv(RHS), true)29.4k
:
APSInt(sdiv(RHS), false)316k
;
122
346k
  }
123
124
19.5k
  APSInt operator>>(unsigned Amt) const {
125
19.5k
    return IsUnsigned ? 
APSInt(lshr(Amt), true)15.5k
:
APSInt(ashr(Amt), false)3.99k
;
126
19.5k
  }
127
196
  APSInt& operator>>=(unsigned Amt) {
128
196
    if (IsUnsigned)
129
84
      lshrInPlace(Amt);
130
112
    else
131
112
      ashrInPlace(Amt);
132
196
    return *this;
133
196
  }
134
135
1.24M
  inline bool operator<(const APSInt& RHS) const {
136
1.24M
    assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
137
1.24M
    return IsUnsigned ? 
ult(RHS)194k
:
slt(RHS)1.04M
;
138
1.24M
  }
139
1.50M
  inline bool operator>(const APSInt& RHS) const {
140
1.50M
    assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
141
1.50M
    return IsUnsigned ? 
ugt(RHS)138k
:
sgt(RHS)1.36M
;
142
1.50M
  }
143
1.31M
  inline bool operator<=(const APSInt& RHS) const {
144
1.31M
    assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
145
1.31M
    return IsUnsigned ? 
ule(RHS)464k
:
sle(RHS)847k
;
146
1.31M
  }
147
558k
  inline bool operator>=(const APSInt& RHS) const {
148
558k
    assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
149
558k
    return IsUnsigned ? 
uge(RHS)5.69k
:
sge(RHS)552k
;
150
558k
  }
151
2.85M
  inline bool operator==(const APSInt& RHS) const {
152
2.85M
    assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
153
2.85M
    return eq(RHS);
154
2.85M
  }
155
1.36M
  inline bool operator!=(const APSInt& RHS) const {
156
1.36M
    return !((*this) == RHS);
157
1.36M
  }
158
159
1.94M
  bool operator==(int64_t RHS) const {
160
1.94M
    return compareValues(*this, get(RHS)) == 0;
161
1.94M
  }
162
9.07M
  bool operator!=(int64_t RHS) const {
163
9.07M
    return compareValues(*this, get(RHS)) != 0;
164
9.07M
  }
165
0
  bool operator<=(int64_t RHS) const {
166
0
    return compareValues(*this, get(RHS)) <= 0;
167
0
  }
168
1.03k
  bool operator>=(int64_t RHS) const {
169
1.03k
    return compareValues(*this, get(RHS)) >= 0;
170
1.03k
  }
171
90.3k
  bool operator<(int64_t RHS) const {
172
90.3k
    return compareValues(*this, get(RHS)) < 0;
173
90.3k
  }
174
131k
  bool operator>(int64_t RHS) const {
175
131k
    return compareValues(*this, get(RHS)) > 0;
176
131k
  }
177
178
  // The remaining operators just wrap the logic of APInt, but retain the
179
  // signedness information.
180
181
198k
  APSInt operator<<(unsigned Bits) const {
182
198k
    return APSInt(static_cast<const APInt&>(*this) << Bits, IsUnsigned);
183
198k
  }
184
120
  APSInt& operator<<=(unsigned Amt) {
185
120
    static_cast<APInt&>(*this) <<= Amt;
186
120
    return *this;
187
120
  }
188
189
1.72M
  APSInt& operator++() {
190
1.72M
    ++(static_cast<APInt&>(*this));
191
1.72M
    return *this;
192
1.72M
  }
193
80.6k
  APSInt& operator--() {
194
80.6k
    --(static_cast<APInt&>(*this));
195
80.6k
    return *this;
196
80.6k
  }
197
0
  APSInt operator++(int) {
198
0
    return APSInt(++static_cast<APInt&>(*this), IsUnsigned);
199
0
  }
200
0
  APSInt operator--(int) {
201
0
    return APSInt(--static_cast<APInt&>(*this), IsUnsigned);
202
0
  }
203
150k
  APSInt operator-() const {
204
150k
    return APSInt(-static_cast<const APInt&>(*this), IsUnsigned);
205
150k
  }
206
108
  APSInt& operator+=(const APSInt& RHS) {
207
108
    assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
208
108
    static_cast<APInt&>(*this) += RHS;
209
108
    return *this;
210
108
  }
211
30
  APSInt& operator-=(const APSInt& RHS) {
212
30
    assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
213
30
    static_cast<APInt&>(*this) -= RHS;
214
30
    return *this;
215
30
  }
216
17
  APSInt& operator*=(const APSInt& RHS) {
217
17
    assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
218
17
    static_cast<APInt&>(*this) *= RHS;
219
17
    return *this;
220
17
  }
221
18
  APSInt& operator&=(const APSInt& RHS) {
222
18
    assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
223
18
    static_cast<APInt&>(*this) &= RHS;
224
18
    return *this;
225
18
  }
226
0
  APSInt& operator|=(const APSInt& RHS) {
227
0
    assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
228
0
    static_cast<APInt&>(*this) |= RHS;
229
0
    return *this;
230
0
  }
231
0
  APSInt& operator^=(const APSInt& RHS) {
232
0
    assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
233
0
    static_cast<APInt&>(*this) ^= RHS;
234
0
    return *this;
235
0
  }
236
237
26.5k
  APSInt operator&(const APSInt& RHS) const {
238
26.5k
    assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
239
26.5k
    return APSInt(static_cast<const APInt&>(*this) & RHS, IsUnsigned);
240
26.5k
  }
241
242
55.7k
  APSInt operator|(const APSInt& RHS) const {
243
55.7k
    assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
244
55.7k
    return APSInt(static_cast<const APInt&>(*this) | RHS, IsUnsigned);
245
55.7k
  }
246
247
7.43k
  APSInt operator^(const APSInt &RHS) const {
248
7.43k
    assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
249
7.43k
    return APSInt(static_cast<const APInt&>(*this) ^ RHS, IsUnsigned);
250
7.43k
  }
251
252
155k
  APSInt operator*(const APSInt& RHS) const {
253
155k
    assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
254
155k
    return APSInt(static_cast<const APInt&>(*this) * RHS, IsUnsigned);
255
155k
  }
256
401k
  APSInt operator+(const APSInt& RHS) const {
257
401k
    assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
258
401k
    return APSInt(static_cast<const APInt&>(*this) + RHS, IsUnsigned);
259
401k
  }
260
1.09M
  APSInt operator-(const APSInt& RHS) const {
261
1.09M
    assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
262
1.09M
    return APSInt(static_cast<const APInt&>(*this) - RHS, IsUnsigned);
263
1.09M
  }
264
21.5k
  APSInt operator~() const {
265
21.5k
    return APSInt(~static_cast<const APInt&>(*this), IsUnsigned);
266
21.5k
  }
267
268
  /// getMaxValue - Return the APSInt representing the maximum integer value
269
  ///  with the given bit width and signedness.
270
408k
  static APSInt getMaxValue(uint32_t numBits, bool Unsigned) {
271
408k
    return APSInt(Unsigned ? 
APInt::getMaxValue(numBits)111k
272
408k
                           : 
APInt::getSignedMaxValue(numBits)297k
, Unsigned);
273
408k
  }
274
275
  /// getMinValue - Return the APSInt representing the minimum integer value
276
  ///  with the given bit width and signedness.
277
440k
  static APSInt getMinValue(uint32_t numBits, bool Unsigned) {
278
440k
    return APSInt(Unsigned ? 
APInt::getMinValue(numBits)109k
279
440k
                           : 
APInt::getSignedMinValue(numBits)330k
, Unsigned);
280
440k
  }
281
282
  /// Determine if two APSInts have the same value, zero- or
283
  /// sign-extending as needed.
284
2.52k
  static bool isSameValue(const APSInt &I1, const APSInt &I2) {
285
2.52k
    return !compareValues(I1, I2);
286
2.52k
  }
287
288
  /// Compare underlying values of two numbers.
289
13.5M
  static int compareValues(const APSInt &I1, const APSInt &I2) {
290
13.5M
    if (I1.getBitWidth() == I2.getBitWidth() && 
I1.isSigned() == I2.isSigned()11.7M
)
291
11.0M
      return I1.IsUnsigned ? 
I1.compare(I2)98.6k
:
I1.compareSigned(I2)10.9M
;
292
2.47M
293
2.47M
    // Check for a bit-width mismatch.
294
2.47M
    if (I1.getBitWidth() > I2.getBitWidth())
295
6.50k
      return compareValues(I1, I2.extend(I1.getBitWidth()));
296
2.47M
    if (I2.getBitWidth() > I1.getBitWidth())
297
1.73M
      return compareValues(I1.extend(I2.getBitWidth()), I2);
298
741k
299
741k
    // We have a signedness mismatch. Check for negative values and do an
300
741k
    // unsigned compare if both are positive.
301
741k
    if (I1.isSigned()) {
302
851
      assert(!I2.isSigned() && "Expected signed mismatch");
303
851
      if (I1.isNegative())
304
2
        return -1;
305
740k
    } else {
306
740k
      assert(I2.isSigned() && "Expected signed mismatch");
307
740k
      if (I2.isNegative())
308
196
        return 1;
309
741k
    }
310
741k
311
741k
    return I1.compare(I2);
312
741k
  }
313
314
11.2M
  static APSInt get(int64_t X) { return APSInt(APInt(64, X), false); }
315
6.56k
  static APSInt getUnsigned(uint64_t X) { return APSInt(APInt(64, X), true); }
316
317
  /// Profile - Used to insert APSInt objects, or objects that contain APSInt
318
  ///  objects, into FoldingSets.
319
  void Profile(FoldingSetNodeID& ID) const;
320
};
321
322
2
inline bool operator==(int64_t V1, const APSInt &V2) { return V2 == V1; }
323
216k
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
0
inline bool operator>=(int64_t V1, const APSInt &V2) { return V2 <= V1; }
326
23
inline bool operator<(int64_t V1, const APSInt &V2) { return V2 > V1; }
327
0
inline bool operator>(int64_t V1, const APSInt &V2) { return V2 < V1; }
328
329
57.6k
inline raw_ostream &operator<<(raw_ostream &OS, const APSInt &I) {
330
57.6k
  I.print(OS, I.isSigned());
331
57.6k
  return OS;
332
57.6k
}
333
334
} // end namespace llvm
335
336
#endif