LLVM  9.0.0svn
APSInt.h
Go to the documentation of this file.
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  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  : APInt(BitWidth, 0), IsUnsigned(isUnsigned) {}
32 
33  explicit APSInt(APInt I, bool isUnsigned = true)
34  : 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 
46  // Retain our current sign.
47  APInt::operator=(std::move(RHS));
48  return *this;
49  }
50 
51  APSInt &operator=(uint64_t RHS) {
52  // Retain our current sign.
53  APInt::operator=(RHS);
54  return *this;
55  }
56 
57  // Query sign information.
58  bool isSigned() const { return !IsUnsigned; }
59  bool isUnsigned() const { return IsUnsigned; }
60  void setIsUnsigned(bool Val) { IsUnsigned = Val; }
61  void setIsSigned(bool Val) { IsUnsigned = !Val; }
62 
63  /// toString - Append this APSInt to the specified SmallString.
64  void toString(SmallVectorImpl<char> &Str, unsigned Radix = 10) const {
65  APInt::toString(Str, Radix, isSigned());
66  }
67  /// toString - Converts an APInt to a std::string. This is an inefficient
68  /// method; you should prefer passing in a SmallString instead.
69  std::string toString(unsigned Radix) const {
70  return APInt::toString(Radix, isSigned());
71  }
72  using APInt::toString;
73 
74  /// Get the correctly-extended \c int64_t value.
75  int64_t getExtValue() const {
76  assert(getMinSignedBits() <= 64 && "Too many bits for int64_t");
77  return isSigned() ? getSExtValue() : getZExtValue();
78  }
79 
80  APSInt trunc(uint32_t width) const {
81  return APSInt(APInt::trunc(width), IsUnsigned);
82  }
83 
84  APSInt extend(uint32_t width) const {
85  if (IsUnsigned)
86  return APSInt(zext(width), IsUnsigned);
87  else
88  return APSInt(sext(width), IsUnsigned);
89  }
90 
91  APSInt extOrTrunc(uint32_t width) const {
92  if (IsUnsigned)
93  return APSInt(zextOrTrunc(width), IsUnsigned);
94  else
95  return APSInt(sextOrTrunc(width), IsUnsigned);
96  }
97 
98  const APSInt &operator%=(const APSInt &RHS) {
99  assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
100  if (IsUnsigned)
101  *this = urem(RHS);
102  else
103  *this = srem(RHS);
104  return *this;
105  }
106  const APSInt &operator/=(const APSInt &RHS) {
107  assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
108  if (IsUnsigned)
109  *this = udiv(RHS);
110  else
111  *this = sdiv(RHS);
112  return *this;
113  }
114  APSInt operator%(const APSInt &RHS) const {
115  assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
116  return IsUnsigned ? APSInt(urem(RHS), true) : APSInt(srem(RHS), false);
117  }
118  APSInt operator/(const APSInt &RHS) const {
119  assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
120  return IsUnsigned ? APSInt(udiv(RHS), true) : APSInt(sdiv(RHS), false);
121  }
122 
123  APSInt operator>>(unsigned Amt) const {
124  return IsUnsigned ? APSInt(lshr(Amt), true) : APSInt(ashr(Amt), false);
125  }
126  APSInt& operator>>=(unsigned Amt) {
127  if (IsUnsigned)
128  lshrInPlace(Amt);
129  else
130  ashrInPlace(Amt);
131  return *this;
132  }
133 
134  inline bool operator<(const APSInt& RHS) const {
135  assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
136  return IsUnsigned ? ult(RHS) : slt(RHS);
137  }
138  inline bool operator>(const APSInt& RHS) const {
139  assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
140  return IsUnsigned ? ugt(RHS) : sgt(RHS);
141  }
142  inline bool operator<=(const APSInt& RHS) const {
143  assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
144  return IsUnsigned ? ule(RHS) : sle(RHS);
145  }
146  inline bool operator>=(const APSInt& RHS) const {
147  assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
148  return IsUnsigned ? uge(RHS) : sge(RHS);
149  }
150  inline bool operator==(const APSInt& RHS) const {
151  assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
152  return eq(RHS);
153  }
154  inline bool operator!=(const APSInt& RHS) const {
155  return !((*this) == RHS);
156  }
157 
158  bool operator==(int64_t RHS) const {
159  return compareValues(*this, get(RHS)) == 0;
160  }
161  bool operator!=(int64_t RHS) const {
162  return compareValues(*this, get(RHS)) != 0;
163  }
164  bool operator<=(int64_t RHS) const {
165  return compareValues(*this, get(RHS)) <= 0;
166  }
167  bool operator>=(int64_t RHS) const {
168  return compareValues(*this, get(RHS)) >= 0;
169  }
170  bool operator<(int64_t RHS) const {
171  return compareValues(*this, get(RHS)) < 0;
172  }
173  bool operator>(int64_t RHS) const {
174  return compareValues(*this, get(RHS)) > 0;
175  }
176 
177  // The remaining operators just wrap the logic of APInt, but retain the
178  // signedness information.
179 
180  APSInt operator<<(unsigned Bits) const {
181  return APSInt(static_cast<const APInt&>(*this) << Bits, IsUnsigned);
182  }
183  APSInt& operator<<=(unsigned Amt) {
184  static_cast<APInt&>(*this) <<= Amt;
185  return *this;
186  }
187 
189  ++(static_cast<APInt&>(*this));
190  return *this;
191  }
193  --(static_cast<APInt&>(*this));
194  return *this;
195  }
197  return APSInt(++static_cast<APInt&>(*this), IsUnsigned);
198  }
200  return APSInt(--static_cast<APInt&>(*this), IsUnsigned);
201  }
202  APSInt operator-() const {
203  return APSInt(-static_cast<const APInt&>(*this), IsUnsigned);
204  }
205  APSInt& operator+=(const APSInt& RHS) {
206  assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
207  static_cast<APInt&>(*this) += RHS;
208  return *this;
209  }
210  APSInt& operator-=(const APSInt& RHS) {
211  assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
212  static_cast<APInt&>(*this) -= RHS;
213  return *this;
214  }
215  APSInt& operator*=(const APSInt& RHS) {
216  assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
217  static_cast<APInt&>(*this) *= RHS;
218  return *this;
219  }
220  APSInt& operator&=(const APSInt& RHS) {
221  assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
222  static_cast<APInt&>(*this) &= RHS;
223  return *this;
224  }
225  APSInt& operator|=(const APSInt& RHS) {
226  assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
227  static_cast<APInt&>(*this) |= RHS;
228  return *this;
229  }
230  APSInt& operator^=(const APSInt& RHS) {
231  assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
232  static_cast<APInt&>(*this) ^= RHS;
233  return *this;
234  }
235 
236  APSInt operator&(const APSInt& RHS) const {
237  assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
238  return APSInt(static_cast<const APInt&>(*this) & RHS, IsUnsigned);
239  }
240 
241  APSInt operator|(const APSInt& RHS) const {
242  assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
243  return APSInt(static_cast<const APInt&>(*this) | RHS, IsUnsigned);
244  }
245 
246  APSInt operator^(const APSInt &RHS) const {
247  assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
248  return APSInt(static_cast<const APInt&>(*this) ^ RHS, IsUnsigned);
249  }
250 
251  APSInt operator*(const APSInt& RHS) const {
252  assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
253  return APSInt(static_cast<const APInt&>(*this) * RHS, IsUnsigned);
254  }
255  APSInt operator+(const APSInt& RHS) const {
256  assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
257  return APSInt(static_cast<const APInt&>(*this) + RHS, IsUnsigned);
258  }
259  APSInt operator-(const APSInt& RHS) const {
260  assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
261  return APSInt(static_cast<const APInt&>(*this) - RHS, IsUnsigned);
262  }
263  APSInt operator~() const {
264  return APSInt(~static_cast<const APInt&>(*this), IsUnsigned);
265  }
266 
267  /// getMaxValue - Return the APSInt representing the maximum integer value
268  /// with the given bit width and signedness.
269  static APSInt getMaxValue(uint32_t numBits, bool Unsigned) {
270  return APSInt(Unsigned ? APInt::getMaxValue(numBits)
271  : APInt::getSignedMaxValue(numBits), Unsigned);
272  }
273 
274  /// getMinValue - Return the APSInt representing the minimum integer value
275  /// with the given bit width and signedness.
276  static APSInt getMinValue(uint32_t numBits, bool Unsigned) {
277  return APSInt(Unsigned ? APInt::getMinValue(numBits)
278  : APInt::getSignedMinValue(numBits), Unsigned);
279  }
280 
281  /// Determine if two APSInts have the same value, zero- or
282  /// sign-extending as needed.
283  static bool isSameValue(const APSInt &I1, const APSInt &I2) {
284  return !compareValues(I1, I2);
285  }
286 
287  /// Compare underlying values of two numbers.
288  static int compareValues(const APSInt &I1, const APSInt &I2) {
289  if (I1.getBitWidth() == I2.getBitWidth() && I1.isSigned() == I2.isSigned())
290  return I1.IsUnsigned ? I1.compare(I2) : I1.compareSigned(I2);
291 
292  // Check for a bit-width mismatch.
293  if (I1.getBitWidth() > I2.getBitWidth())
294  return compareValues(I1, I2.extend(I1.getBitWidth()));
295  if (I2.getBitWidth() > I1.getBitWidth())
296  return compareValues(I1.extend(I2.getBitWidth()), I2);
297 
298  // We have a signedness mismatch. Check for negative values and do an
299  // unsigned compare if both are positive.
300  if (I1.isSigned()) {
301  assert(!I2.isSigned() && "Expected signed mismatch");
302  if (I1.isNegative())
303  return -1;
304  } else {
305  assert(I2.isSigned() && "Expected signed mismatch");
306  if (I2.isNegative())
307  return 1;
308  }
309 
310  return I1.compare(I2);
311  }
312 
313  static APSInt get(int64_t X) { return APSInt(APInt(64, X), false); }
314  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 inline bool operator==(int64_t V1, const APSInt &V2) { return V2 == V1; }
322 inline bool operator!=(int64_t V1, const APSInt &V2) { return V2 != V1; }
323 inline bool operator<=(int64_t V1, const APSInt &V2) { return V2 >= V1; }
324 inline bool operator>=(int64_t V1, const APSInt &V2) { return V2 <= V1; }
325 inline bool operator<(int64_t V1, const APSInt &V2) { return V2 > V1; }
326 inline bool operator>(int64_t V1, const APSInt &V2) { return V2 < V1; }
327 
328 inline raw_ostream &operator<<(raw_ostream &OS, const APSInt &I) {
329  I.print(OS, I.isSigned());
330  return OS;
331 }
332 
333 } // end namespace llvm
334 
335 #endif
APSInt operator-() const
Definition: APSInt.h:202
static GCMetadataPrinterRegistry::Add< ErlangGCPrinter > X("erlang", "erlang-compatible garbage collector")
const APSInt & operator/=(const APSInt &RHS)
Definition: APSInt.h:106
APSInt & operator-=(const APSInt &RHS)
Definition: APSInt.h:210
This class represents lattice values for constants.
Definition: AllocatorList.h:23
APSInt operator/(const APSInt &RHS) const
Definition: APSInt.h:118
bool operator>(int64_t V1, const APSInt &V2)
Definition: APSInt.h:326
bool operator!=(int64_t RHS) const
Definition: APSInt.h:161
APSInt()
Default constructor that creates an uninitialized APInt.
Definition: APSInt.h:26
bool operator<=(int64_t RHS) const
Definition: APSInt.h:164
APSInt(uint32_t BitWidth, bool isUnsigned=true)
APSInt ctor - Create an APSInt with the specified width, default to unsigned.
Definition: APSInt.h:30
void setIsSigned(bool Val)
Definition: APSInt.h:61
APInt trunc(unsigned width) const
Truncate to new width.
Definition: APInt.cpp:810
APInt operator &(APInt a, const APInt &b)
Definition: APInt.h:1978
APSInt & operator^=(const APSInt &RHS)
Definition: APSInt.h:230
static APInt getSignedMaxValue(unsigned numBits)
Gets maximum signed value of APInt for a specific bit width.
Definition: APInt.h:534
void setIsUnsigned(bool Val)
Definition: APSInt.h:60
unsigned getBitWidth() const
Return the number of bits in the APInt.
Definition: APInt.h:1508
bool operator<=(int64_t V1, const APSInt &V2)
Definition: APSInt.h:323
Definition: BitVector.h:937
bool operator>=(int64_t V1, const APSInt &V2)
Definition: APSInt.h:324
APSInt & operator--()
Definition: APSInt.h:192
APSInt extend(uint32_t width) const
Definition: APSInt.h:84
APSInt extOrTrunc(uint32_t width) const
Definition: APSInt.h:91
This file implements a class to represent arbitrary precision integral constant values and operations...
APSInt & operator<<=(unsigned Amt)
Definition: APSInt.h:183
bool operator<=(const APSInt &RHS) const
Definition: APSInt.h:142
APSInt operator^(const APSInt &RHS) const
Definition: APSInt.h:246
APSInt & operator*=(const APSInt &RHS)
Definition: APSInt.h:215
bool operator<(const APSInt &RHS) const
Definition: APSInt.h:134
APSInt operator*(const APSInt &RHS) const
Definition: APSInt.h:251
bool operator!=(const APSInt &RHS) const
Definition: APSInt.h:154
static bool isSameValue(const APSInt &I1, const APSInt &I2)
Determine if two APSInts have the same value, zero- or sign-extending as needed.
Definition: APSInt.h:283
FoldingSetNodeID - This class is used to gather all the unique data bits of a node.
Definition: FoldingSet.h:305
bool isNegative() const
Determine sign of this APInt.
Definition: APInt.h:363
APSInt operator--(int)
Definition: APSInt.h:199
bool isSigned() const
Definition: APSInt.h:58
APSInt trunc(uint32_t width) const
Definition: APSInt.h:80
APInt & operator=(const APInt &RHS)
Copy assignment operator.
Definition: APInt.h:734
APSInt operator++(int)
Definition: APSInt.h:196
APSInt & operator++()
Definition: APSInt.h:188
APSInt operator|(const APSInt &RHS) const
Definition: APSInt.h:241
APSInt & operator=(uint64_t RHS)
Definition: APSInt.h:51
int64_t getExtValue() const
Get the correctly-extended int64_t value.
Definition: APSInt.h:75
bool operator>(const APSInt &RHS) const
Definition: APSInt.h:138
static APSInt getMaxValue(uint32_t numBits, bool Unsigned)
getMaxValue - Return the APSInt representing the maximum integer value with the given bit width and s...
Definition: APSInt.h:269
static APSInt getMinValue(uint32_t numBits, bool Unsigned)
getMinValue - Return the APSInt representing the minimum integer value with the given bit width and s...
Definition: APSInt.h:276
APSInt & operator|=(const APSInt &RHS)
Definition: APSInt.h:225
bool operator==(const APSInt &RHS) const
Definition: APSInt.h:150
APSInt operator>>(unsigned Amt) const
Definition: APSInt.h:123
static APInt getMinValue(unsigned numBits)
Gets minimum unsigned value of APInt for a specific bit width.
Definition: APInt.h:541
void print(raw_ostream &OS, bool isSigned) const
Definition: APInt.cpp:2170
bool isUnsigned() const
Definition: APSInt.h:59
std::string toString(unsigned Radix) const
toString - Converts an APInt to a std::string.
Definition: APSInt.h:69
APSInt operator%(const APSInt &RHS) const
Definition: APSInt.h:114
Class for arbitrary precision integers.
Definition: APInt.h:69
static APInt getMaxValue(unsigned numBits)
Gets maximum unsigned value of APInt for specific bit width.
Definition: APInt.h:529
bool operator>(int64_t RHS) const
Definition: APSInt.h:173
const APSInt & operator%=(const APSInt &RHS)
Definition: APSInt.h:98
bool operator!=(uint64_t V1, const APInt &V2)
Definition: APInt.h:1968
APSInt operator<<(unsigned Bits) const
Definition: APSInt.h:180
bool operator>=(int64_t RHS) const
Definition: APSInt.h:167
bool operator>=(const APSInt &RHS) const
Definition: APSInt.h:146
APSInt(APInt I, bool isUnsigned=true)
Definition: APSInt.h:33
#define I(x, y, z)
Definition: MD5.cpp:58
raw_ostream & operator<<(raw_ostream &OS, const APInt &I)
Definition: APInt.h:2038
APSInt & operator+=(const APSInt &RHS)
Definition: APSInt.h:205
#define LLVM_NODISCARD
LLVM_NODISCARD - Warn if a type or return value is discarded.
Definition: Compiler.h:128
bool operator==(int64_t RHS) const
Definition: APSInt.h:158
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
static APSInt getUnsigned(uint64_t X)
Definition: APSInt.h:314
static APInt getSignedMinValue(unsigned numBits)
Gets minimum signed value of APInt for a specific bit width.
Definition: APInt.h:544
bool operator<(int64_t V1, const APSInt &V2)
Definition: APSInt.h:325
E & operator &=(E &LHS, E RHS)
Definition: BitmaskEnum.h:133
APSInt operator+(const APSInt &RHS) const
Definition: APSInt.h:255
void toString(SmallVectorImpl< char > &Str, unsigned Radix=10) const
toString - Append this APSInt to the specified SmallString.
Definition: APSInt.h:64
static int compareValues(const APSInt &I1, const APSInt &I2)
Compare underlying values of two numbers.
Definition: APSInt.h:288
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:45
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:48
APSInt & operator>>=(unsigned Amt)
Definition: APSInt.h:126
bool operator==(uint64_t V1, const APInt &V2)
Definition: APInt.h:1966
APSInt operator-(const APSInt &RHS) const
Definition: APSInt.h:259
APSInt operator~() const
Definition: APSInt.h:263
void toString(SmallVectorImpl< char > &Str, unsigned Radix, bool Signed, bool formatAsCLiteral=false) const
Converts an APInt to a string and append it to Str.
Definition: APInt.cpp:2039
bool operator<(int64_t RHS) const
Definition: APSInt.h:170
APSInt & operator=(APInt RHS)
Definition: APSInt.h:45