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