APSInt.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===-- llvm/ADT/APSInt.h - Arbitrary Precision Signed Int -----*- C++ -*--===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. ///
  14. /// \file
  15. /// This file implements the APSInt class, which is a simple class that
  16. /// represents an arbitrary sized integer that knows its signedness.
  17. ///
  18. //===----------------------------------------------------------------------===//
  19. #ifndef LLVM_ADT_APSINT_H
  20. #define LLVM_ADT_APSINT_H
  21. #include "llvm/ADT/APInt.h"
  22. namespace llvm {
  23. /// An arbitrary precision integer that knows its signedness.
  24. class [[nodiscard]] APSInt : public APInt {
  25. bool IsUnsigned = false;
  26. public:
  27. /// Default constructor that creates an uninitialized APInt.
  28. explicit APSInt() = default;
  29. /// Create an APSInt with the specified width, default to unsigned.
  30. explicit APSInt(uint32_t BitWidth, bool isUnsigned = true)
  31. : APInt(BitWidth, 0), IsUnsigned(isUnsigned) {}
  32. explicit APSInt(APInt I, bool isUnsigned = true)
  33. : APInt(std::move(I)), IsUnsigned(isUnsigned) {}
  34. /// Construct an APSInt from a string representation.
  35. ///
  36. /// This constructor interprets the string \p Str using the radix of 10.
  37. /// The interpretation stops at the end of the string. The bit width of the
  38. /// constructed APSInt is determined automatically.
  39. ///
  40. /// \param Str the string to be interpreted.
  41. explicit APSInt(StringRef Str);
  42. /// Determine sign of this APSInt.
  43. ///
  44. /// \returns true if this APSInt is negative, false otherwise
  45. bool isNegative() const { return isSigned() && APInt::isNegative(); }
  46. /// Determine if this APSInt Value is non-negative (>= 0)
  47. ///
  48. /// \returns true if this APSInt is non-negative, false otherwise
  49. bool isNonNegative() const { return !isNegative(); }
  50. /// Determine if this APSInt Value is positive.
  51. ///
  52. /// This tests if the value of this APSInt is positive (> 0). Note
  53. /// that 0 is not a positive value.
  54. ///
  55. /// \returns true if this APSInt is positive.
  56. bool isStrictlyPositive() const { return isNonNegative() && !isZero(); }
  57. APSInt &operator=(APInt RHS) {
  58. // Retain our current sign.
  59. APInt::operator=(std::move(RHS));
  60. return *this;
  61. }
  62. APSInt &operator=(uint64_t RHS) {
  63. // Retain our current sign.
  64. APInt::operator=(RHS);
  65. return *this;
  66. }
  67. // Query sign information.
  68. bool isSigned() const { return !IsUnsigned; }
  69. bool isUnsigned() const { return IsUnsigned; }
  70. void setIsUnsigned(bool Val) { IsUnsigned = Val; }
  71. void setIsSigned(bool Val) { IsUnsigned = !Val; }
  72. /// Append this APSInt to the specified SmallString.
  73. void toString(SmallVectorImpl<char> &Str, unsigned Radix = 10) const {
  74. APInt::toString(Str, Radix, isSigned());
  75. }
  76. using APInt::toString;
  77. /// If this int is representable using an int64_t.
  78. bool isRepresentableByInt64() const {
  79. // For unsigned values with 64 active bits, they technically fit into a
  80. // int64_t, but the user may get negative numbers and has to manually cast
  81. // them to unsigned. Let's not bet the user has the sanity to do that and
  82. // not give them a vague value at the first place.
  83. return isSigned() ? isSignedIntN(64) : isIntN(63);
  84. }
  85. /// Get the correctly-extended \c int64_t value.
  86. int64_t getExtValue() const {
  87. assert(isRepresentableByInt64() && "Too many bits for int64_t");
  88. return isSigned() ? getSExtValue() : getZExtValue();
  89. }
  90. std::optional<int64_t> tryExtValue() const {
  91. return isRepresentableByInt64() ? std::optional<int64_t>(getExtValue())
  92. : std::nullopt;
  93. }
  94. APSInt trunc(uint32_t width) const {
  95. return APSInt(APInt::trunc(width), IsUnsigned);
  96. }
  97. APSInt extend(uint32_t width) const {
  98. if (IsUnsigned)
  99. return APSInt(zext(width), IsUnsigned);
  100. else
  101. return APSInt(sext(width), IsUnsigned);
  102. }
  103. APSInt extOrTrunc(uint32_t width) const {
  104. if (IsUnsigned)
  105. return APSInt(zextOrTrunc(width), IsUnsigned);
  106. else
  107. return APSInt(sextOrTrunc(width), IsUnsigned);
  108. }
  109. const APSInt &operator%=(const APSInt &RHS) {
  110. assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
  111. if (IsUnsigned)
  112. *this = urem(RHS);
  113. else
  114. *this = srem(RHS);
  115. return *this;
  116. }
  117. const APSInt &operator/=(const APSInt &RHS) {
  118. assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
  119. if (IsUnsigned)
  120. *this = udiv(RHS);
  121. else
  122. *this = sdiv(RHS);
  123. return *this;
  124. }
  125. APSInt operator%(const APSInt &RHS) const {
  126. assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
  127. return IsUnsigned ? APSInt(urem(RHS), true) : APSInt(srem(RHS), false);
  128. }
  129. APSInt operator/(const APSInt &RHS) const {
  130. assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
  131. return IsUnsigned ? APSInt(udiv(RHS), true) : APSInt(sdiv(RHS), false);
  132. }
  133. APSInt operator>>(unsigned Amt) const {
  134. return IsUnsigned ? APSInt(lshr(Amt), true) : APSInt(ashr(Amt), false);
  135. }
  136. APSInt &operator>>=(unsigned Amt) {
  137. if (IsUnsigned)
  138. lshrInPlace(Amt);
  139. else
  140. ashrInPlace(Amt);
  141. return *this;
  142. }
  143. APSInt relativeShr(unsigned Amt) const {
  144. return IsUnsigned ? APSInt(relativeLShr(Amt), true)
  145. : APSInt(relativeAShr(Amt), false);
  146. }
  147. inline bool operator<(const APSInt &RHS) const {
  148. assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
  149. return IsUnsigned ? ult(RHS) : slt(RHS);
  150. }
  151. inline bool operator>(const APSInt &RHS) const {
  152. assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
  153. return IsUnsigned ? ugt(RHS) : sgt(RHS);
  154. }
  155. inline bool operator<=(const APSInt &RHS) const {
  156. assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
  157. return IsUnsigned ? ule(RHS) : sle(RHS);
  158. }
  159. inline bool operator>=(const APSInt &RHS) const {
  160. assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
  161. return IsUnsigned ? uge(RHS) : sge(RHS);
  162. }
  163. inline bool operator==(const APSInt &RHS) const {
  164. assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
  165. return eq(RHS);
  166. }
  167. inline bool operator!=(const APSInt &RHS) const { return !((*this) == RHS); }
  168. bool operator==(int64_t RHS) const {
  169. return compareValues(*this, get(RHS)) == 0;
  170. }
  171. bool operator!=(int64_t RHS) const {
  172. return compareValues(*this, get(RHS)) != 0;
  173. }
  174. bool operator<=(int64_t RHS) const {
  175. return compareValues(*this, get(RHS)) <= 0;
  176. }
  177. bool operator>=(int64_t RHS) const {
  178. return compareValues(*this, get(RHS)) >= 0;
  179. }
  180. bool operator<(int64_t RHS) const {
  181. return compareValues(*this, get(RHS)) < 0;
  182. }
  183. bool operator>(int64_t RHS) const {
  184. return compareValues(*this, get(RHS)) > 0;
  185. }
  186. // The remaining operators just wrap the logic of APInt, but retain the
  187. // signedness information.
  188. APSInt operator<<(unsigned Bits) const {
  189. return APSInt(static_cast<const APInt &>(*this) << Bits, IsUnsigned);
  190. }
  191. APSInt &operator<<=(unsigned Amt) {
  192. static_cast<APInt &>(*this) <<= Amt;
  193. return *this;
  194. }
  195. APSInt relativeShl(unsigned Amt) const {
  196. return IsUnsigned ? APSInt(relativeLShl(Amt), true)
  197. : APSInt(relativeAShl(Amt), false);
  198. }
  199. APSInt &operator++() {
  200. ++(static_cast<APInt &>(*this));
  201. return *this;
  202. }
  203. APSInt &operator--() {
  204. --(static_cast<APInt &>(*this));
  205. return *this;
  206. }
  207. APSInt operator++(int) {
  208. return APSInt(++static_cast<APInt &>(*this), IsUnsigned);
  209. }
  210. APSInt operator--(int) {
  211. return APSInt(--static_cast<APInt &>(*this), IsUnsigned);
  212. }
  213. APSInt operator-() const {
  214. return APSInt(-static_cast<const APInt &>(*this), IsUnsigned);
  215. }
  216. APSInt &operator+=(const APSInt &RHS) {
  217. assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
  218. static_cast<APInt &>(*this) += RHS;
  219. return *this;
  220. }
  221. APSInt &operator-=(const APSInt &RHS) {
  222. assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
  223. static_cast<APInt &>(*this) -= RHS;
  224. return *this;
  225. }
  226. APSInt &operator*=(const APSInt &RHS) {
  227. assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
  228. static_cast<APInt &>(*this) *= RHS;
  229. return *this;
  230. }
  231. APSInt &operator&=(const APSInt &RHS) {
  232. assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
  233. static_cast<APInt &>(*this) &= RHS;
  234. return *this;
  235. }
  236. APSInt &operator|=(const APSInt &RHS) {
  237. assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
  238. static_cast<APInt &>(*this) |= RHS;
  239. return *this;
  240. }
  241. APSInt &operator^=(const APSInt &RHS) {
  242. assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
  243. static_cast<APInt &>(*this) ^= RHS;
  244. return *this;
  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. APSInt operator|(const APSInt &RHS) const {
  251. assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
  252. return APSInt(static_cast<const APInt &>(*this) | RHS, IsUnsigned);
  253. }
  254. APSInt operator^(const APSInt &RHS) const {
  255. assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
  256. return APSInt(static_cast<const APInt &>(*this) ^ RHS, IsUnsigned);
  257. }
  258. APSInt operator*(const APSInt &RHS) const {
  259. assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
  260. return APSInt(static_cast<const APInt &>(*this) * RHS, IsUnsigned);
  261. }
  262. APSInt operator+(const APSInt &RHS) const {
  263. assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
  264. return APSInt(static_cast<const APInt &>(*this) + RHS, IsUnsigned);
  265. }
  266. APSInt operator-(const APSInt &RHS) const {
  267. assert(IsUnsigned == RHS.IsUnsigned && "Signedness mismatch!");
  268. return APSInt(static_cast<const APInt &>(*this) - RHS, IsUnsigned);
  269. }
  270. APSInt operator~() const {
  271. return APSInt(~static_cast<const APInt &>(*this), IsUnsigned);
  272. }
  273. /// Return the APSInt representing the maximum integer value with the given
  274. /// bit width and signedness.
  275. static APSInt getMaxValue(uint32_t numBits, bool Unsigned) {
  276. return APSInt(Unsigned ? APInt::getMaxValue(numBits)
  277. : APInt::getSignedMaxValue(numBits),
  278. Unsigned);
  279. }
  280. /// Return the APSInt representing the minimum integer value with the given
  281. /// bit width and signedness.
  282. static APSInt getMinValue(uint32_t numBits, bool Unsigned) {
  283. return APSInt(Unsigned ? APInt::getMinValue(numBits)
  284. : APInt::getSignedMinValue(numBits),
  285. Unsigned);
  286. }
  287. /// Determine if two APSInts have the same value, zero- or
  288. /// sign-extending as needed.
  289. static bool isSameValue(const APSInt &I1, const APSInt &I2) {
  290. return !compareValues(I1, I2);
  291. }
  292. /// Compare underlying values of two numbers.
  293. static int compareValues(const APSInt &I1, const APSInt &I2) {
  294. if (I1.getBitWidth() == I2.getBitWidth() && I1.isSigned() == I2.isSigned())
  295. return I1.IsUnsigned ? I1.compare(I2) : I1.compareSigned(I2);
  296. // Check for a bit-width mismatch.
  297. if (I1.getBitWidth() > I2.getBitWidth())
  298. return compareValues(I1, I2.extend(I1.getBitWidth()));
  299. if (I2.getBitWidth() > I1.getBitWidth())
  300. return compareValues(I1.extend(I2.getBitWidth()), I2);
  301. // We have a signedness mismatch. Check for negative values and do an
  302. // unsigned compare if both are positive.
  303. if (I1.isSigned()) {
  304. assert(!I2.isSigned() && "Expected signed mismatch");
  305. if (I1.isNegative())
  306. return -1;
  307. } else {
  308. assert(I2.isSigned() && "Expected signed mismatch");
  309. if (I2.isNegative())
  310. return 1;
  311. }
  312. return I1.compare(I2);
  313. }
  314. static APSInt get(int64_t X) { return APSInt(APInt(64, X), false); }
  315. static APSInt getUnsigned(uint64_t X) { return APSInt(APInt(64, X), true); }
  316. /// Used to insert APSInt objects, or objects that contain APSInt objects,
  317. /// into FoldingSets.
  318. void Profile(FoldingSetNodeID &ID) const;
  319. };
  320. inline bool operator==(int64_t V1, const APSInt &V2) { return V2 == V1; }
  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 raw_ostream &operator<<(raw_ostream &OS, const APSInt &I) {
  327. I.print(OS, I.isSigned());
  328. return OS;
  329. }
  330. /// Provide DenseMapInfo for APSInt, using the DenseMapInfo for APInt.
  331. template <> struct DenseMapInfo<APSInt, void> {
  332. static inline APSInt getEmptyKey() {
  333. return APSInt(DenseMapInfo<APInt, void>::getEmptyKey());
  334. }
  335. static inline APSInt getTombstoneKey() {
  336. return APSInt(DenseMapInfo<APInt, void>::getTombstoneKey());
  337. }
  338. static unsigned getHashValue(const APSInt &Key) {
  339. return DenseMapInfo<APInt, void>::getHashValue(Key);
  340. }
  341. static bool isEqual(const APSInt &LHS, const APSInt &RHS) {
  342. return LHS.getBitWidth() == RHS.getBitWidth() &&
  343. LHS.isUnsigned() == RHS.isUnsigned() && LHS == RHS;
  344. }
  345. };
  346. } // end namespace llvm
  347. #endif
  348. #ifdef __GNUC__
  349. #pragma GCC diagnostic pop
  350. #endif