KnownBits.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- llvm/Support/KnownBits.h - Stores known zeros/ones -------*- 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. // This file contains a class for representing known zeros and ones used by
  15. // computeKnownBits.
  16. //
  17. //===----------------------------------------------------------------------===//
  18. #ifndef LLVM_SUPPORT_KNOWNBITS_H
  19. #define LLVM_SUPPORT_KNOWNBITS_H
  20. #include "llvm/ADT/APInt.h"
  21. #include <optional>
  22. namespace llvm {
  23. // Struct for tracking the known zeros and ones of a value.
  24. struct KnownBits {
  25. APInt Zero;
  26. APInt One;
  27. private:
  28. // Internal constructor for creating a KnownBits from two APInts.
  29. KnownBits(APInt Zero, APInt One)
  30. : Zero(std::move(Zero)), One(std::move(One)) {}
  31. public:
  32. // Default construct Zero and One.
  33. KnownBits() = default;
  34. /// Create a known bits object of BitWidth bits initialized to unknown.
  35. KnownBits(unsigned BitWidth) : Zero(BitWidth, 0), One(BitWidth, 0) {}
  36. /// Get the bit width of this value.
  37. unsigned getBitWidth() const {
  38. assert(Zero.getBitWidth() == One.getBitWidth() &&
  39. "Zero and One should have the same width!");
  40. return Zero.getBitWidth();
  41. }
  42. /// Returns true if there is conflicting information.
  43. bool hasConflict() const { return Zero.intersects(One); }
  44. /// Returns true if we know the value of all bits.
  45. bool isConstant() const {
  46. assert(!hasConflict() && "KnownBits conflict!");
  47. return Zero.countPopulation() + One.countPopulation() == getBitWidth();
  48. }
  49. /// Returns the value when all bits have a known value. This just returns One
  50. /// with a protective assertion.
  51. const APInt &getConstant() const {
  52. assert(isConstant() && "Can only get value when all bits are known");
  53. return One;
  54. }
  55. /// Returns true if we don't know any bits.
  56. bool isUnknown() const { return Zero.isZero() && One.isZero(); }
  57. /// Resets the known state of all bits.
  58. void resetAll() {
  59. Zero.clearAllBits();
  60. One.clearAllBits();
  61. }
  62. /// Returns true if value is all zero.
  63. bool isZero() const {
  64. assert(!hasConflict() && "KnownBits conflict!");
  65. return Zero.isAllOnes();
  66. }
  67. /// Returns true if value is all one bits.
  68. bool isAllOnes() const {
  69. assert(!hasConflict() && "KnownBits conflict!");
  70. return One.isAllOnes();
  71. }
  72. /// Make all bits known to be zero and discard any previous information.
  73. void setAllZero() {
  74. Zero.setAllBits();
  75. One.clearAllBits();
  76. }
  77. /// Make all bits known to be one and discard any previous information.
  78. void setAllOnes() {
  79. Zero.clearAllBits();
  80. One.setAllBits();
  81. }
  82. /// Returns true if this value is known to be negative.
  83. bool isNegative() const { return One.isSignBitSet(); }
  84. /// Returns true if this value is known to be non-negative.
  85. bool isNonNegative() const { return Zero.isSignBitSet(); }
  86. /// Returns true if this value is known to be non-zero.
  87. bool isNonZero() const { return !One.isZero(); }
  88. /// Returns true if this value is known to be positive.
  89. bool isStrictlyPositive() const {
  90. return Zero.isSignBitSet() && !One.isZero();
  91. }
  92. /// Make this value negative.
  93. void makeNegative() {
  94. One.setSignBit();
  95. }
  96. /// Make this value non-negative.
  97. void makeNonNegative() {
  98. Zero.setSignBit();
  99. }
  100. /// Return the minimal unsigned value possible given these KnownBits.
  101. APInt getMinValue() const {
  102. // Assume that all bits that aren't known-ones are zeros.
  103. return One;
  104. }
  105. /// Return the minimal signed value possible given these KnownBits.
  106. APInt getSignedMinValue() const {
  107. // Assume that all bits that aren't known-ones are zeros.
  108. APInt Min = One;
  109. // Sign bit is unknown.
  110. if (Zero.isSignBitClear())
  111. Min.setSignBit();
  112. return Min;
  113. }
  114. /// Return the maximal unsigned value possible given these KnownBits.
  115. APInt getMaxValue() const {
  116. // Assume that all bits that aren't known-zeros are ones.
  117. return ~Zero;
  118. }
  119. /// Return the maximal signed value possible given these KnownBits.
  120. APInt getSignedMaxValue() const {
  121. // Assume that all bits that aren't known-zeros are ones.
  122. APInt Max = ~Zero;
  123. // Sign bit is unknown.
  124. if (One.isSignBitClear())
  125. Max.clearSignBit();
  126. return Max;
  127. }
  128. /// Return known bits for a truncation of the value we're tracking.
  129. KnownBits trunc(unsigned BitWidth) const {
  130. return KnownBits(Zero.trunc(BitWidth), One.trunc(BitWidth));
  131. }
  132. /// Return known bits for an "any" extension of the value we're tracking,
  133. /// where we don't know anything about the extended bits.
  134. KnownBits anyext(unsigned BitWidth) const {
  135. return KnownBits(Zero.zext(BitWidth), One.zext(BitWidth));
  136. }
  137. /// Return known bits for a zero extension of the value we're tracking.
  138. KnownBits zext(unsigned BitWidth) const {
  139. unsigned OldBitWidth = getBitWidth();
  140. APInt NewZero = Zero.zext(BitWidth);
  141. NewZero.setBitsFrom(OldBitWidth);
  142. return KnownBits(NewZero, One.zext(BitWidth));
  143. }
  144. /// Return known bits for a sign extension of the value we're tracking.
  145. KnownBits sext(unsigned BitWidth) const {
  146. return KnownBits(Zero.sext(BitWidth), One.sext(BitWidth));
  147. }
  148. /// Return known bits for an "any" extension or truncation of the value we're
  149. /// tracking.
  150. KnownBits anyextOrTrunc(unsigned BitWidth) const {
  151. if (BitWidth > getBitWidth())
  152. return anyext(BitWidth);
  153. if (BitWidth < getBitWidth())
  154. return trunc(BitWidth);
  155. return *this;
  156. }
  157. /// Return known bits for a zero extension or truncation of the value we're
  158. /// tracking.
  159. KnownBits zextOrTrunc(unsigned BitWidth) const {
  160. if (BitWidth > getBitWidth())
  161. return zext(BitWidth);
  162. if (BitWidth < getBitWidth())
  163. return trunc(BitWidth);
  164. return *this;
  165. }
  166. /// Return known bits for a sign extension or truncation of the value we're
  167. /// tracking.
  168. KnownBits sextOrTrunc(unsigned BitWidth) const {
  169. if (BitWidth > getBitWidth())
  170. return sext(BitWidth);
  171. if (BitWidth < getBitWidth())
  172. return trunc(BitWidth);
  173. return *this;
  174. }
  175. /// Return known bits for a in-register sign extension of the value we're
  176. /// tracking.
  177. KnownBits sextInReg(unsigned SrcBitWidth) const;
  178. /// Insert the bits from a smaller known bits starting at bitPosition.
  179. void insertBits(const KnownBits &SubBits, unsigned BitPosition) {
  180. Zero.insertBits(SubBits.Zero, BitPosition);
  181. One.insertBits(SubBits.One, BitPosition);
  182. }
  183. /// Return a subset of the known bits from [bitPosition,bitPosition+numBits).
  184. KnownBits extractBits(unsigned NumBits, unsigned BitPosition) const {
  185. return KnownBits(Zero.extractBits(NumBits, BitPosition),
  186. One.extractBits(NumBits, BitPosition));
  187. }
  188. /// Concatenate the bits from \p Lo onto the bottom of *this. This is
  189. /// equivalent to:
  190. /// (this->zext(NewWidth) << Lo.getBitWidth()) | Lo.zext(NewWidth)
  191. KnownBits concat(const KnownBits &Lo) const {
  192. return KnownBits(Zero.concat(Lo.Zero), One.concat(Lo.One));
  193. }
  194. /// Return KnownBits based on this, but updated given that the underlying
  195. /// value is known to be greater than or equal to Val.
  196. KnownBits makeGE(const APInt &Val) const;
  197. /// Returns the minimum number of trailing zero bits.
  198. unsigned countMinTrailingZeros() const {
  199. return Zero.countTrailingOnes();
  200. }
  201. /// Returns the minimum number of trailing one bits.
  202. unsigned countMinTrailingOnes() const {
  203. return One.countTrailingOnes();
  204. }
  205. /// Returns the minimum number of leading zero bits.
  206. unsigned countMinLeadingZeros() const {
  207. return Zero.countLeadingOnes();
  208. }
  209. /// Returns the minimum number of leading one bits.
  210. unsigned countMinLeadingOnes() const {
  211. return One.countLeadingOnes();
  212. }
  213. /// Returns the number of times the sign bit is replicated into the other
  214. /// bits.
  215. unsigned countMinSignBits() const {
  216. if (isNonNegative())
  217. return countMinLeadingZeros();
  218. if (isNegative())
  219. return countMinLeadingOnes();
  220. // Every value has at least 1 sign bit.
  221. return 1;
  222. }
  223. /// Returns the maximum number of bits needed to represent all possible
  224. /// signed values with these known bits. This is the inverse of the minimum
  225. /// number of known sign bits. Examples for bitwidth 5:
  226. /// 110?? --> 4
  227. /// 0000? --> 2
  228. unsigned countMaxSignificantBits() const {
  229. return getBitWidth() - countMinSignBits() + 1;
  230. }
  231. /// Returns the maximum number of trailing zero bits possible.
  232. unsigned countMaxTrailingZeros() const {
  233. return One.countTrailingZeros();
  234. }
  235. /// Returns the maximum number of trailing one bits possible.
  236. unsigned countMaxTrailingOnes() const {
  237. return Zero.countTrailingZeros();
  238. }
  239. /// Returns the maximum number of leading zero bits possible.
  240. unsigned countMaxLeadingZeros() const {
  241. return One.countLeadingZeros();
  242. }
  243. /// Returns the maximum number of leading one bits possible.
  244. unsigned countMaxLeadingOnes() const {
  245. return Zero.countLeadingZeros();
  246. }
  247. /// Returns the number of bits known to be one.
  248. unsigned countMinPopulation() const {
  249. return One.countPopulation();
  250. }
  251. /// Returns the maximum number of bits that could be one.
  252. unsigned countMaxPopulation() const {
  253. return getBitWidth() - Zero.countPopulation();
  254. }
  255. /// Returns the maximum number of bits needed to represent all possible
  256. /// unsigned values with these known bits. This is the inverse of the
  257. /// minimum number of leading zeros.
  258. unsigned countMaxActiveBits() const {
  259. return getBitWidth() - countMinLeadingZeros();
  260. }
  261. /// Create known bits from a known constant.
  262. static KnownBits makeConstant(const APInt &C) {
  263. return KnownBits(~C, C);
  264. }
  265. /// Compute known bits common to LHS and RHS.
  266. static KnownBits commonBits(const KnownBits &LHS, const KnownBits &RHS) {
  267. return KnownBits(LHS.Zero & RHS.Zero, LHS.One & RHS.One);
  268. }
  269. /// Return true if LHS and RHS have no common bits set.
  270. static bool haveNoCommonBitsSet(const KnownBits &LHS, const KnownBits &RHS) {
  271. return (LHS.Zero | RHS.Zero).isAllOnes();
  272. }
  273. /// Compute known bits resulting from adding LHS, RHS and a 1-bit Carry.
  274. static KnownBits computeForAddCarry(
  275. const KnownBits &LHS, const KnownBits &RHS, const KnownBits &Carry);
  276. /// Compute known bits resulting from adding LHS and RHS.
  277. static KnownBits computeForAddSub(bool Add, bool NSW, const KnownBits &LHS,
  278. KnownBits RHS);
  279. /// Compute known bits resulting from multiplying LHS and RHS.
  280. static KnownBits mul(const KnownBits &LHS, const KnownBits &RHS,
  281. bool NoUndefSelfMultiply = false);
  282. /// Compute known bits from sign-extended multiply-hi.
  283. static KnownBits mulhs(const KnownBits &LHS, const KnownBits &RHS);
  284. /// Compute known bits from zero-extended multiply-hi.
  285. static KnownBits mulhu(const KnownBits &LHS, const KnownBits &RHS);
  286. /// Compute known bits for udiv(LHS, RHS).
  287. static KnownBits udiv(const KnownBits &LHS, const KnownBits &RHS);
  288. /// Compute known bits for urem(LHS, RHS).
  289. static KnownBits urem(const KnownBits &LHS, const KnownBits &RHS);
  290. /// Compute known bits for srem(LHS, RHS).
  291. static KnownBits srem(const KnownBits &LHS, const KnownBits &RHS);
  292. /// Compute known bits for umax(LHS, RHS).
  293. static KnownBits umax(const KnownBits &LHS, const KnownBits &RHS);
  294. /// Compute known bits for umin(LHS, RHS).
  295. static KnownBits umin(const KnownBits &LHS, const KnownBits &RHS);
  296. /// Compute known bits for smax(LHS, RHS).
  297. static KnownBits smax(const KnownBits &LHS, const KnownBits &RHS);
  298. /// Compute known bits for smin(LHS, RHS).
  299. static KnownBits smin(const KnownBits &LHS, const KnownBits &RHS);
  300. /// Compute known bits for shl(LHS, RHS).
  301. /// NOTE: RHS (shift amount) bitwidth doesn't need to be the same as LHS.
  302. static KnownBits shl(const KnownBits &LHS, const KnownBits &RHS);
  303. /// Compute known bits for lshr(LHS, RHS).
  304. /// NOTE: RHS (shift amount) bitwidth doesn't need to be the same as LHS.
  305. static KnownBits lshr(const KnownBits &LHS, const KnownBits &RHS);
  306. /// Compute known bits for ashr(LHS, RHS).
  307. /// NOTE: RHS (shift amount) bitwidth doesn't need to be the same as LHS.
  308. static KnownBits ashr(const KnownBits &LHS, const KnownBits &RHS);
  309. /// Determine if these known bits always give the same ICMP_EQ result.
  310. static std::optional<bool> eq(const KnownBits &LHS, const KnownBits &RHS);
  311. /// Determine if these known bits always give the same ICMP_NE result.
  312. static std::optional<bool> ne(const KnownBits &LHS, const KnownBits &RHS);
  313. /// Determine if these known bits always give the same ICMP_UGT result.
  314. static std::optional<bool> ugt(const KnownBits &LHS, const KnownBits &RHS);
  315. /// Determine if these known bits always give the same ICMP_UGE result.
  316. static std::optional<bool> uge(const KnownBits &LHS, const KnownBits &RHS);
  317. /// Determine if these known bits always give the same ICMP_ULT result.
  318. static std::optional<bool> ult(const KnownBits &LHS, const KnownBits &RHS);
  319. /// Determine if these known bits always give the same ICMP_ULE result.
  320. static std::optional<bool> ule(const KnownBits &LHS, const KnownBits &RHS);
  321. /// Determine if these known bits always give the same ICMP_SGT result.
  322. static std::optional<bool> sgt(const KnownBits &LHS, const KnownBits &RHS);
  323. /// Determine if these known bits always give the same ICMP_SGE result.
  324. static std::optional<bool> sge(const KnownBits &LHS, const KnownBits &RHS);
  325. /// Determine if these known bits always give the same ICMP_SLT result.
  326. static std::optional<bool> slt(const KnownBits &LHS, const KnownBits &RHS);
  327. /// Determine if these known bits always give the same ICMP_SLE result.
  328. static std::optional<bool> sle(const KnownBits &LHS, const KnownBits &RHS);
  329. /// Update known bits based on ANDing with RHS.
  330. KnownBits &operator&=(const KnownBits &RHS);
  331. /// Update known bits based on ORing with RHS.
  332. KnownBits &operator|=(const KnownBits &RHS);
  333. /// Update known bits based on XORing with RHS.
  334. KnownBits &operator^=(const KnownBits &RHS);
  335. /// Compute known bits for the absolute value.
  336. KnownBits abs(bool IntMinIsPoison = false) const;
  337. KnownBits byteSwap() const {
  338. return KnownBits(Zero.byteSwap(), One.byteSwap());
  339. }
  340. KnownBits reverseBits() const {
  341. return KnownBits(Zero.reverseBits(), One.reverseBits());
  342. }
  343. bool operator==(const KnownBits &Other) const {
  344. return Zero == Other.Zero && One == Other.One;
  345. }
  346. bool operator!=(const KnownBits &Other) const { return !(*this == Other); }
  347. void print(raw_ostream &OS) const;
  348. void dump() const;
  349. };
  350. inline KnownBits operator&(KnownBits LHS, const KnownBits &RHS) {
  351. LHS &= RHS;
  352. return LHS;
  353. }
  354. inline KnownBits operator&(const KnownBits &LHS, KnownBits &&RHS) {
  355. RHS &= LHS;
  356. return std::move(RHS);
  357. }
  358. inline KnownBits operator|(KnownBits LHS, const KnownBits &RHS) {
  359. LHS |= RHS;
  360. return LHS;
  361. }
  362. inline KnownBits operator|(const KnownBits &LHS, KnownBits &&RHS) {
  363. RHS |= LHS;
  364. return std::move(RHS);
  365. }
  366. inline KnownBits operator^(KnownBits LHS, const KnownBits &RHS) {
  367. LHS ^= RHS;
  368. return LHS;
  369. }
  370. inline KnownBits operator^(const KnownBits &LHS, KnownBits &&RHS) {
  371. RHS ^= LHS;
  372. return std::move(RHS);
  373. }
  374. } // end namespace llvm
  375. #endif
  376. #ifdef __GNUC__
  377. #pragma GCC diagnostic pop
  378. #endif