[Alignment][NFC] Convert StoreInst to MaybeAlign
[llvm-complete.git] / include / llvm / ADT / APSInt.h
blob0f991826c457e12aa9cc7f6dfaead6a707e6c688
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.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_ADT_APSINT_H
15 #define LLVM_ADT_APSINT_H
17 #include "llvm/ADT/APInt.h"
19 namespace llvm {
21 class LLVM_NODISCARD APSInt : public APInt {
22 bool IsUnsigned;
24 public:
25 /// Default constructor that creates an uninitialized APInt.
26 explicit APSInt() : IsUnsigned(false) {}
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) {}
33 explicit APSInt(APInt I, bool isUnsigned = true)
34 : APInt(std::move(I)), IsUnsigned(isUnsigned) {}
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);
45 /// Determine sign of this APSInt.
46 ///
47 /// \returns true if this APSInt is negative, false otherwise
48 bool isNegative() const { return isSigned() && APInt::isNegative(); }
50 /// Determine if this APSInt Value is non-negative (>= 0)
51 ///
52 /// \returns true if this APSInt is non-negative, false otherwise
53 bool isNonNegative() const { return !isNegative(); }
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 bool isStrictlyPositive() const { return isNonNegative() && !isNullValue(); }
63 APSInt &operator=(APInt RHS) {
64 // Retain our current sign.
65 APInt::operator=(std::move(RHS));
66 return *this;
69 APSInt &operator=(uint64_t RHS) {
70 // Retain our current sign.
71 APInt::operator=(RHS);
72 return *this;
75 // Query sign information.
76 bool isSigned() const { return !IsUnsigned; }
77 bool isUnsigned() const { return IsUnsigned; }
78 void setIsUnsigned(bool Val) { IsUnsigned = Val; }
79 void setIsSigned(bool Val) { IsUnsigned = !Val; }
81 /// toString - Append this APSInt to the specified SmallString.
82 void toString(SmallVectorImpl<char> &Str, unsigned Radix = 10) const {
83 APInt::toString(Str, Radix, isSigned());
85 /// toString - Converts an APInt to a std::string. This is an inefficient
86 /// method; you should prefer passing in a SmallString instead.
87 std::string toString(unsigned Radix) const {
88 return APInt::toString(Radix, isSigned());
90 using APInt::toString;
92 /// Get the correctly-extended \c int64_t value.
93 int64_t getExtValue() const {
94 assert(getMinSignedBits() <= 64 && "Too many bits for int64_t");
95 return isSigned() ? getSExtValue() : getZExtValue();
98 APSInt trunc(uint32_t width) const {
99 return APSInt(APInt::trunc(width), IsUnsigned);
102 APSInt extend(uint32_t width) const {
103 if (IsUnsigned)
104 return APSInt(zext(width), IsUnsigned);
105 else
106 return APSInt(sext(width), IsUnsigned);
109 APSInt extOrTrunc(uint32_t width) const {
110 if (IsUnsigned)
111 return APSInt(zextOrTrunc(width), IsUnsigned);
112 else
113 return APSInt(sextOrTrunc(width), IsUnsigned);
116 const APSInt &operator%=(const APSInt &RHS) {
117 assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
118 if (IsUnsigned)
119 *this = urem(RHS);
120 else
121 *this = srem(RHS);
122 return *this;
124 const APSInt &operator/=(const APSInt &RHS) {
125 assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
126 if (IsUnsigned)
127 *this = udiv(RHS);
128 else
129 *this = sdiv(RHS);
130 return *this;
132 APSInt operator%(const APSInt &RHS) const {
133 assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
134 return IsUnsigned ? APSInt(urem(RHS), true) : APSInt(srem(RHS), false);
136 APSInt operator/(const APSInt &RHS) const {
137 assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
138 return IsUnsigned ? APSInt(udiv(RHS), true) : APSInt(sdiv(RHS), false);
141 APSInt operator>>(unsigned Amt) const {
142 return IsUnsigned ? APSInt(lshr(Amt), true) : APSInt(ashr(Amt), false);
144 APSInt& operator>>=(unsigned Amt) {
145 if (IsUnsigned)
146 lshrInPlace(Amt);
147 else
148 ashrInPlace(Amt);
149 return *this;
152 inline bool operator<(const APSInt& RHS) const {
153 assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
154 return IsUnsigned ? ult(RHS) : slt(RHS);
156 inline bool operator>(const APSInt& RHS) const {
157 assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
158 return IsUnsigned ? ugt(RHS) : sgt(RHS);
160 inline bool operator<=(const APSInt& RHS) const {
161 assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
162 return IsUnsigned ? ule(RHS) : sle(RHS);
164 inline bool operator>=(const APSInt& RHS) const {
165 assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
166 return IsUnsigned ? uge(RHS) : sge(RHS);
168 inline bool operator==(const APSInt& RHS) const {
169 assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
170 return eq(RHS);
172 inline bool operator!=(const APSInt& RHS) const {
173 return !((*this) == RHS);
176 bool operator==(int64_t RHS) const {
177 return compareValues(*this, get(RHS)) == 0;
179 bool operator!=(int64_t RHS) const {
180 return compareValues(*this, get(RHS)) != 0;
182 bool operator<=(int64_t RHS) const {
183 return compareValues(*this, get(RHS)) <= 0;
185 bool operator>=(int64_t RHS) const {
186 return compareValues(*this, get(RHS)) >= 0;
188 bool operator<(int64_t RHS) const {
189 return compareValues(*this, get(RHS)) < 0;
191 bool operator>(int64_t RHS) const {
192 return compareValues(*this, get(RHS)) > 0;
195 // The remaining operators just wrap the logic of APInt, but retain the
196 // signedness information.
198 APSInt operator<<(unsigned Bits) const {
199 return APSInt(static_cast<const APInt&>(*this) << Bits, IsUnsigned);
201 APSInt& operator<<=(unsigned Amt) {
202 static_cast<APInt&>(*this) <<= Amt;
203 return *this;
206 APSInt& operator++() {
207 ++(static_cast<APInt&>(*this));
208 return *this;
210 APSInt& operator--() {
211 --(static_cast<APInt&>(*this));
212 return *this;
214 APSInt operator++(int) {
215 return APSInt(++static_cast<APInt&>(*this), IsUnsigned);
217 APSInt operator--(int) {
218 return APSInt(--static_cast<APInt&>(*this), IsUnsigned);
220 APSInt operator-() const {
221 return APSInt(-static_cast<const APInt&>(*this), IsUnsigned);
223 APSInt& operator+=(const APSInt& RHS) {
224 assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
225 static_cast<APInt&>(*this) += RHS;
226 return *this;
228 APSInt& operator-=(const APSInt& RHS) {
229 assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
230 static_cast<APInt&>(*this) -= RHS;
231 return *this;
233 APSInt& operator*=(const APSInt& RHS) {
234 assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
235 static_cast<APInt&>(*this) *= RHS;
236 return *this;
238 APSInt& operator&=(const APSInt& RHS) {
239 assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
240 static_cast<APInt&>(*this) &= RHS;
241 return *this;
243 APSInt& operator|=(const APSInt& RHS) {
244 assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
245 static_cast<APInt&>(*this) |= RHS;
246 return *this;
248 APSInt& operator^=(const APSInt& RHS) {
249 assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
250 static_cast<APInt&>(*this) ^= RHS;
251 return *this;
254 APSInt operator&(const APSInt& RHS) const {
255 assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
256 return APSInt(static_cast<const APInt&>(*this) & RHS, IsUnsigned);
259 APSInt operator|(const APSInt& RHS) const {
260 assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
261 return APSInt(static_cast<const APInt&>(*this) | RHS, IsUnsigned);
264 APSInt operator^(const APSInt &RHS) const {
265 assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
266 return APSInt(static_cast<const APInt&>(*this) ^ RHS, IsUnsigned);
269 APSInt operator*(const APSInt& RHS) const {
270 assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
271 return APSInt(static_cast<const APInt&>(*this) * RHS, IsUnsigned);
273 APSInt operator+(const APSInt& RHS) const {
274 assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
275 return APSInt(static_cast<const APInt&>(*this) + RHS, IsUnsigned);
277 APSInt operator-(const APSInt& RHS) const {
278 assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
279 return APSInt(static_cast<const APInt&>(*this) - RHS, IsUnsigned);
281 APSInt operator~() const {
282 return APSInt(~static_cast<const APInt&>(*this), IsUnsigned);
285 /// getMaxValue - Return the APSInt representing the maximum integer value
286 /// with the given bit width and signedness.
287 static APSInt getMaxValue(uint32_t numBits, bool Unsigned) {
288 return APSInt(Unsigned ? APInt::getMaxValue(numBits)
289 : APInt::getSignedMaxValue(numBits), Unsigned);
292 /// getMinValue - Return the APSInt representing the minimum integer value
293 /// with the given bit width and signedness.
294 static APSInt getMinValue(uint32_t numBits, bool Unsigned) {
295 return APSInt(Unsigned ? APInt::getMinValue(numBits)
296 : APInt::getSignedMinValue(numBits), Unsigned);
299 /// Determine if two APSInts have the same value, zero- or
300 /// sign-extending as needed.
301 static bool isSameValue(const APSInt &I1, const APSInt &I2) {
302 return !compareValues(I1, I2);
305 /// Compare underlying values of two numbers.
306 static int compareValues(const APSInt &I1, const APSInt &I2) {
307 if (I1.getBitWidth() == I2.getBitWidth() && I1.isSigned() == I2.isSigned())
308 return I1.IsUnsigned ? I1.compare(I2) : I1.compareSigned(I2);
310 // Check for a bit-width mismatch.
311 if (I1.getBitWidth() > I2.getBitWidth())
312 return compareValues(I1, I2.extend(I1.getBitWidth()));
313 if (I2.getBitWidth() > I1.getBitWidth())
314 return compareValues(I1.extend(I2.getBitWidth()), I2);
316 // We have a signedness mismatch. Check for negative values and do an
317 // unsigned compare if both are positive.
318 if (I1.isSigned()) {
319 assert(!I2.isSigned() && "Expected signed mismatch");
320 if (I1.isNegative())
321 return -1;
322 } else {
323 assert(I2.isSigned() && "Expected signed mismatch");
324 if (I2.isNegative())
325 return 1;
328 return I1.compare(I2);
331 static APSInt get(int64_t X) { return APSInt(APInt(64, X), false); }
332 static APSInt getUnsigned(uint64_t X) { return APSInt(APInt(64, X), true); }
334 /// Profile - Used to insert APSInt objects, or objects that contain APSInt
335 /// objects, into FoldingSets.
336 void Profile(FoldingSetNodeID& ID) const;
339 inline bool operator==(int64_t V1, const APSInt &V2) { return V2 == V1; }
340 inline bool operator!=(int64_t V1, const APSInt &V2) { return V2 != V1; }
341 inline bool operator<=(int64_t V1, const APSInt &V2) { return V2 >= V1; }
342 inline bool operator>=(int64_t V1, const APSInt &V2) { return V2 <= V1; }
343 inline bool operator<(int64_t V1, const APSInt &V2) { return V2 > V1; }
344 inline bool operator>(int64_t V1, const APSInt &V2) { return V2 < V1; }
346 inline raw_ostream &operator<<(raw_ostream &OS, const APSInt &I) {
347 I.print(OS, I.isSigned());
348 return OS;
351 } // end namespace llvm
353 #endif