Coverage Report

Created: 2018-07-12 09:57

/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&&)
Unexecuted instantiation: llvm::APSInt::operator=(llvm::APSInt const&)
23
  bool IsUnsigned;
24
25
public:
26
  /// Default constructor that creates an uninitialized APInt.
27
10.6M
  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
37.1M
   : APInt(BitWidth, 0), IsUnsigned(isUnsigned) {}
33
34
  explicit APSInt(APInt I, bool isUnsigned = true)
35
46.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
261k
  APSInt &operator=(APInt RHS) {
47
261k
    // Retain our current sign.
48
261k
    APInt::operator=(std::move(RHS));
49
261k
    return *this;
50
261k
  }
51
52
7.62M
  APSInt &operator=(uint64_t RHS) {
53
7.62M
    // Retain our current sign.
54
7.62M
    APInt::operator=(RHS);
55
7.62M
    return *this;
56
7.62M
  }
57
58
  // Query sign information.
59
30.2M
  bool isSigned() const { return !IsUnsigned; }
60
8.64M
  bool isUnsigned() const { return IsUnsigned; }
61
20.1M
  void setIsUnsigned(bool Val) { IsUnsigned = Val; }
62
1.19M
  void setIsSigned(bool Val) { IsUnsigned = !Val; }
63
64
  /// toString - Append this APSInt to the specified SmallString.
65
744
  void toString(SmallVectorImpl<char> &Str, unsigned Radix = 10) const {
66
744
    APInt::toString(Str, Radix, isSigned());
67
744
  }
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.03k
  std::string toString(unsigned Radix) const {
71
2.03k
    return APInt::toString(Radix, isSigned());
72
2.03k
  }
73
  using APInt::toString;
74
75
  /// Get the correctly-extended \c int64_t value.
76
20.8k
  int64_t getExtValue() const {
77
20.8k
    assert(getMinSignedBits() <= 64 && "Too many bits for int64_t");
78
20.8k
    return isSigned() ? 
getSExtValue()3.77k
:
getZExtValue()17.0k
;
79
20.8k
  }
80
81
1.28M
  APSInt trunc(uint32_t width) const {
82
1.28M
    return APSInt(APInt::trunc(width), IsUnsigned);
83
1.28M
  }
84
85
5.29M
  APSInt extend(uint32_t width) const {
86
5.29M
    if (IsUnsigned)
87
563k
      return APSInt(zext(width), IsUnsigned);
88
4.72M
    else
89
4.72M
      return APSInt(sext(width), IsUnsigned);
90
5.29M
  }
91
92
8.24M
  APSInt extOrTrunc(uint32_t width) const {
93
8.24M
    if (IsUnsigned)
94
4.42M
      return APSInt(zextOrTrunc(width), IsUnsigned);
95
3.81M
    else
96
3.81M
      return APSInt(sextOrTrunc(width), IsUnsigned);
97
8.24M
  }
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
10.8k
  APSInt operator%(const APSInt &RHS) const {
116
10.8k
    assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
117
10.8k
    return IsUnsigned ? 
APSInt(urem(RHS), true)3.03k
:
APSInt(srem(RHS), false)7.77k
;
118
10.8k
  }
119
328k
  APSInt operator/(const APSInt &RHS) const {
120
328k
    assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
121
328k
    return IsUnsigned ? 
APSInt(udiv(RHS), true)29.1k
:
APSInt(sdiv(RHS), false)299k
;
122
328k
  }
123
124
18.3k
  APSInt operator>>(unsigned Amt) const {
125
18.3k
    return IsUnsigned ? 
APSInt(lshr(Amt), true)15.8k
:
APSInt(ashr(Amt), false)2.49k
;
126
18.3k
  }
127
0
  APSInt& operator>>=(unsigned Amt) {
128
0
    if (IsUnsigned)
129
0
      lshrInPlace(Amt);
130
0
    else
131
0
      ashrInPlace(Amt);
132
0
    return *this;
133
0
  }
134
135
1.17M
  inline bool operator<(const APSInt& RHS) const {
136
1.17M
    assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
137
1.17M
    return IsUnsigned ? 
ult(RHS)183k
:
slt(RHS)986k
;
138
1.17M
  }
139
1.45M
  inline bool operator>(const APSInt& RHS) const {
140
1.45M
    assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
141
1.45M
    return IsUnsigned ? 
ugt(RHS)133k
:
sgt(RHS)1.32M
;
142
1.45M
  }
143
1.17M
  inline bool operator<=(const APSInt& RHS) const {
144
1.17M
    assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
145
1.17M
    return IsUnsigned ? 
ule(RHS)454k
:
sle(RHS)715k
;
146
1.17M
  }
147
531k
  inline bool operator>=(const APSInt& RHS) const {
148
531k
    assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
149
531k
    return IsUnsigned ? 
uge(RHS)5.04k
:
sge(RHS)526k
;
150
531k
  }
151
2.69M
  inline bool operator==(const APSInt& RHS) const {
152
2.69M
    assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
153
2.69M
    return eq(RHS);
154
2.69M
  }
155
1.33M
  inline bool operator!=(const APSInt& RHS) const {
156
1.33M
    return !((*this) == RHS);
157
1.33M
  }
158
159
1.87M
  bool operator==(int64_t RHS) const {
160
1.87M
    return compareValues(*this, get(RHS)) == 0;
161
1.87M
  }
162
8.53M
  bool operator!=(int64_t RHS) const {
163
8.53M
    return compareValues(*this, get(RHS)) != 0;
164
8.53M
  }
165
0
  bool operator<=(int64_t RHS) const {
166
0
    return compareValues(*this, get(RHS)) <= 0;
167
0
  }
168
571
  bool operator>=(int64_t RHS) const {
169
571
    return compareValues(*this, get(RHS)) >= 0;
170
571
  }
171
81.7k
  bool operator<(int64_t RHS) const {
172
81.7k
    return compareValues(*this, get(RHS)) < 0;
173
81.7k
  }
174
122k
  bool operator>(int64_t RHS) const {
175
122k
    return compareValues(*this, get(RHS)) > 0;
176
122k
  }
177
178
  // The remaining operators just wrap the logic of APInt, but retain the
179
  // signedness information.
180
181
190k
  APSInt operator<<(unsigned Bits) const {
182
190k
    return APSInt(static_cast<const APInt&>(*this) << Bits, IsUnsigned);
183
190k
  }
184
0
  APSInt& operator<<=(unsigned Amt) {
185
0
    static_cast<APInt&>(*this) <<= Amt;
186
0
    return *this;
187
0
  }
188
189
1.70M
  APSInt& operator++() {
190
1.70M
    ++(static_cast<APInt&>(*this));
191
1.70M
    return *this;
192
1.70M
  }
193
69.8k
  APSInt& operator--() {
194
69.8k
    --(static_cast<APInt&>(*this));
195
69.8k
    return *this;
196
69.8k
  }
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
144k
  APSInt operator-() const {
204
144k
    return APSInt(-static_cast<const APInt&>(*this), IsUnsigned);
205
144k
  }
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
25.6k
  APSInt operator&(const APSInt& RHS) const {
238
25.6k
    assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
239
25.6k
    return APSInt(static_cast<const APInt&>(*this) & RHS, IsUnsigned);
240
25.6k
  }
241
242
52.5k
  APSInt operator|(const APSInt& RHS) const {
243
52.5k
    assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
244
52.5k
    return APSInt(static_cast<const APInt&>(*this) | RHS, IsUnsigned);
245
52.5k
  }
246
247
4.02k
  APSInt operator^(const APSInt &RHS) const {
248
4.02k
    assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
249
4.02k
    return APSInt(static_cast<const APInt&>(*this) ^ RHS, IsUnsigned);
250
4.02k
  }
251
252
136k
  APSInt operator*(const APSInt& RHS) const {
253
136k
    assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
254
136k
    return APSInt(static_cast<const APInt&>(*this) * RHS, IsUnsigned);
255
136k
  }
256
374k
  APSInt operator+(const APSInt& RHS) const {
257
374k
    assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
258
374k
    return APSInt(static_cast<const APInt&>(*this) + RHS, IsUnsigned);
259
374k
  }
260
1.00M
  APSInt operator-(const APSInt& RHS) const {
261
1.00M
    assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
262
1.00M
    return APSInt(static_cast<const APInt&>(*this) - RHS, IsUnsigned);
263
1.00M
  }
264
20.9k
  APSInt operator~() const {
265
20.9k
    return APSInt(~static_cast<const APInt&>(*this), IsUnsigned);
266
20.9k
  }
267
268
  /// getMaxValue - Return the APSInt representing the maximum integer value
269
  ///  with the given bit width and signedness.
270
379k
  static APSInt getMaxValue(uint32_t numBits, bool Unsigned) {
271
379k
    return APSInt(Unsigned ? 
APInt::getMaxValue(numBits)113k
272
379k
                           : 
APInt::getSignedMaxValue(numBits)265k
, Unsigned);
273
379k
  }
274
275
  /// getMinValue - Return the APSInt representing the minimum integer value
276
  ///  with the given bit width and signedness.
277
412k
  static APSInt getMinValue(uint32_t numBits, bool Unsigned) {
278
412k
    return APSInt(Unsigned ? 
APInt::getMinValue(numBits)112k
279
412k
                           : 
APInt::getSignedMinValue(numBits)299k
, Unsigned);
280
412k
  }
281
282
  /// Determine if two APSInts have the same value, zero- or
283
  /// sign-extending as needed.
284
2.44k
  static bool isSameValue(const APSInt &I1, const APSInt &I2) {
285
2.44k
    return !compareValues(I1, I2);
286
2.44k
  }
287
288
  /// Compare underlying values of two numbers.
289
12.7M
  static int compareValues(const APSInt &I1, const APSInt &I2) {
290
12.7M
    if (I1.getBitWidth() == I2.getBitWidth() && 
I1.isSigned() == I2.isSigned()11.1M
)
291
10.4M
      return I1.IsUnsigned ? 
I1.compare(I2)100k
:
I1.compareSigned(I2)10.3M
;
292
2.25M
293
2.25M
    // Check for a bit-width mismatch.
294
2.25M
    if (I1.getBitWidth() > I2.getBitWidth())
295
6.09k
      return compareValues(I1, I2.extend(I1.getBitWidth()));
296
2.24M
    if (I2.getBitWidth() > I1.getBitWidth())
297
1.59M
      return compareValues(I1.extend(I2.getBitWidth()), I2);
298
658k
299
658k
    // We have a signedness mismatch. Check for negative values and do an
300
658k
    // unsigned compare if both are positive.
301
658k
    if (I1.isSigned()) {
302
837
      assert(!I2.isSigned() && "Expected signed mismatch");
303
837
      if (I1.isNegative())
304
2
        return -1;
305
657k
    } else {
306
657k
      assert(I2.isSigned() && "Expected signed mismatch");
307
657k
      if (I2.isNegative())
308
184
        return 1;
309
658k
    }
310
658k
311
658k
    return I1.compare(I2);
312
658k
  }
313
314
10.6M
  static APSInt get(int64_t X) { return APSInt(APInt(64, X), false); }
315
6.62k
  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
207k
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
44.6k
inline raw_ostream &operator<<(raw_ostream &OS, const APSInt &I) {
330
44.6k
  I.print(OS, I.isSigned());
331
44.6k
  return OS;
332
44.6k
}
333
334
} // end namespace llvm
335
336
#endif