KnownBits.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  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 "llvm/ADT/Optional.h"
  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. /// Return KnownBits based on this, but updated given that the underlying
  189. /// value is known to be greater than or equal to Val.
  190. KnownBits makeGE(const APInt &Val) const;
  191. /// Returns the minimum number of trailing zero bits.
  192. unsigned countMinTrailingZeros() const {
  193. return Zero.countTrailingOnes();
  194. }
  195. /// Returns the minimum number of trailing one bits.
  196. unsigned countMinTrailingOnes() const {
  197. return One.countTrailingOnes();
  198. }
  199. /// Returns the minimum number of leading zero bits.
  200. unsigned countMinLeadingZeros() const {
  201. return Zero.countLeadingOnes();
  202. }
  203. /// Returns the minimum number of leading one bits.
  204. unsigned countMinLeadingOnes() const {
  205. return One.countLeadingOnes();
  206. }
  207. /// Returns the number of times the sign bit is replicated into the other
  208. /// bits.
  209. unsigned countMinSignBits() const {
  210. if (isNonNegative())
  211. return countMinLeadingZeros();
  212. if (isNegative())
  213. return countMinLeadingOnes();
  214. // Every value has at least 1 sign bit.
  215. return 1;
  216. }
  217. /// Returns the maximum number of bits needed to represent all possible
  218. /// signed values with these known bits. This is the inverse of the minimum
  219. /// number of known sign bits. Examples for bitwidth 5:
  220. /// 110?? --> 4
  221. /// 0000? --> 2
  222. unsigned countMaxSignificantBits() const {
  223. return getBitWidth() - countMinSignBits() + 1;
  224. }
  225. /// Returns the maximum number of trailing zero bits possible.
  226. unsigned countMaxTrailingZeros() const {
  227. return One.countTrailingZeros();
  228. }
  229. /// Returns the maximum number of trailing one bits possible.
  230. unsigned countMaxTrailingOnes() const {
  231. return Zero.countTrailingZeros();
  232. }
  233. /// Returns the maximum number of leading zero bits possible.
  234. unsigned countMaxLeadingZeros() const {
  235. return One.countLeadingZeros();
  236. }
  237. /// Returns the maximum number of leading one bits possible.
  238. unsigned countMaxLeadingOnes() const {
  239. return Zero.countLeadingZeros();
  240. }
  241. /// Returns the number of bits known to be one.
  242. unsigned countMinPopulation() const {
  243. return One.countPopulation();
  244. }
  245. /// Returns the maximum number of bits that could be one.
  246. unsigned countMaxPopulation() const {
  247. return getBitWidth() - Zero.countPopulation();
  248. }
  249. /// Returns the maximum number of bits needed to represent all possible
  250. /// unsigned values with these known bits. This is the inverse of the
  251. /// minimum number of leading zeros.
  252. unsigned countMaxActiveBits() const {
  253. return getBitWidth() - countMinLeadingZeros();
  254. }
  255. /// Create known bits from a known constant.
  256. static KnownBits makeConstant(const APInt &C) {
  257. return KnownBits(~C, C);
  258. }
  259. /// Compute known bits common to LHS and RHS.
  260. static KnownBits commonBits(const KnownBits &LHS, const KnownBits &RHS) {
  261. return KnownBits(LHS.Zero & RHS.Zero, LHS.One & RHS.One);
  262. }
  263. /// Return true if LHS and RHS have no common bits set.
  264. static bool haveNoCommonBitsSet(const KnownBits &LHS, const KnownBits &RHS) {
  265. return (LHS.Zero | RHS.Zero).isAllOnes();
  266. }
  267. /// Compute known bits resulting from adding LHS, RHS and a 1-bit Carry.
  268. static KnownBits computeForAddCarry(
  269. const KnownBits &LHS, const KnownBits &RHS, const KnownBits &Carry);
  270. /// Compute known bits resulting from adding LHS and RHS.
  271. static KnownBits computeForAddSub(bool Add, bool NSW, const KnownBits &LHS,
  272. KnownBits RHS);
  273. /// Compute known bits resulting from multiplying LHS and RHS.
  274. static KnownBits mul(const KnownBits &LHS, const KnownBits &RHS,
  275. bool SelfMultiply = false);
  276. /// Compute known bits from sign-extended multiply-hi.
  277. static KnownBits mulhs(const KnownBits &LHS, const KnownBits &RHS);
  278. /// Compute known bits from zero-extended multiply-hi.
  279. static KnownBits mulhu(const KnownBits &LHS, const KnownBits &RHS);
  280. /// Compute known bits for udiv(LHS, RHS).
  281. static KnownBits udiv(const KnownBits &LHS, const KnownBits &RHS);
  282. /// Compute known bits for urem(LHS, RHS).
  283. static KnownBits urem(const KnownBits &LHS, const KnownBits &RHS);
  284. /// Compute known bits for srem(LHS, RHS).
  285. static KnownBits srem(const KnownBits &LHS, const KnownBits &RHS);
  286. /// Compute known bits for umax(LHS, RHS).
  287. static KnownBits umax(const KnownBits &LHS, const KnownBits &RHS);
  288. /// Compute known bits for umin(LHS, RHS).
  289. static KnownBits umin(const KnownBits &LHS, const KnownBits &RHS);
  290. /// Compute known bits for smax(LHS, RHS).
  291. static KnownBits smax(const KnownBits &LHS, const KnownBits &RHS);
  292. /// Compute known bits for smin(LHS, RHS).
  293. static KnownBits smin(const KnownBits &LHS, const KnownBits &RHS);
  294. /// Compute known bits for shl(LHS, RHS).
  295. /// NOTE: RHS (shift amount) bitwidth doesn't need to be the same as LHS.
  296. static KnownBits shl(const KnownBits &LHS, const KnownBits &RHS);
  297. /// Compute known bits for lshr(LHS, RHS).
  298. /// NOTE: RHS (shift amount) bitwidth doesn't need to be the same as LHS.
  299. static KnownBits lshr(const KnownBits &LHS, const KnownBits &RHS);
  300. /// Compute known bits for ashr(LHS, RHS).
  301. /// NOTE: RHS (shift amount) bitwidth doesn't need to be the same as LHS.
  302. static KnownBits ashr(const KnownBits &LHS, const KnownBits &RHS);
  303. /// Determine if these known bits always give the same ICMP_EQ result.
  304. static Optional<bool> eq(const KnownBits &LHS, const KnownBits &RHS);
  305. /// Determine if these known bits always give the same ICMP_NE result.
  306. static Optional<bool> ne(const KnownBits &LHS, const KnownBits &RHS);
  307. /// Determine if these known bits always give the same ICMP_UGT result.
  308. static Optional<bool> ugt(const KnownBits &LHS, const KnownBits &RHS);
  309. /// Determine if these known bits always give the same ICMP_UGE result.
  310. static Optional<bool> uge(const KnownBits &LHS, const KnownBits &RHS);
  311. /// Determine if these known bits always give the same ICMP_ULT result.
  312. static Optional<bool> ult(const KnownBits &LHS, const KnownBits &RHS);
  313. /// Determine if these known bits always give the same ICMP_ULE result.
  314. static Optional<bool> ule(const KnownBits &LHS, const KnownBits &RHS);
  315. /// Determine if these known bits always give the same ICMP_SGT result.
  316. static Optional<bool> sgt(const KnownBits &LHS, const KnownBits &RHS);
  317. /// Determine if these known bits always give the same ICMP_SGE result.
  318. static Optional<bool> sge(const KnownBits &LHS, const KnownBits &RHS);
  319. /// Determine if these known bits always give the same ICMP_SLT result.
  320. static Optional<bool> slt(const KnownBits &LHS, const KnownBits &RHS);
  321. /// Determine if these known bits always give the same ICMP_SLE result.
  322. static Optional<bool> sle(const KnownBits &LHS, const KnownBits &RHS);
  323. /// Update known bits based on ANDing with RHS.
  324. KnownBits &operator&=(const KnownBits &RHS);
  325. /// Update known bits based on ORing with RHS.
  326. KnownBits &operator|=(const KnownBits &RHS);
  327. /// Update known bits based on XORing with RHS.
  328. KnownBits &operator^=(const KnownBits &RHS);
  329. /// Compute known bits for the absolute value.
  330. KnownBits abs(bool IntMinIsPoison = false) const;
  331. KnownBits byteSwap() {
  332. return KnownBits(Zero.byteSwap(), One.byteSwap());
  333. }
  334. KnownBits reverseBits() {
  335. return KnownBits(Zero.reverseBits(), One.reverseBits());
  336. }
  337. void print(raw_ostream &OS) const;
  338. void dump() const;
  339. };
  340. inline KnownBits operator&(KnownBits LHS, const KnownBits &RHS) {
  341. LHS &= RHS;
  342. return LHS;
  343. }
  344. inline KnownBits operator&(const KnownBits &LHS, KnownBits &&RHS) {
  345. RHS &= LHS;
  346. return std::move(RHS);
  347. }
  348. inline KnownBits operator|(KnownBits LHS, const KnownBits &RHS) {
  349. LHS |= RHS;
  350. return LHS;
  351. }
  352. inline KnownBits operator|(const KnownBits &LHS, KnownBits &&RHS) {
  353. RHS |= LHS;
  354. return std::move(RHS);
  355. }
  356. inline KnownBits operator^(KnownBits LHS, const KnownBits &RHS) {
  357. LHS ^= RHS;
  358. return LHS;
  359. }
  360. inline KnownBits operator^(const KnownBits &LHS, KnownBits &&RHS) {
  361. RHS ^= LHS;
  362. return std::move(RHS);
  363. }
  364. } // end namespace llvm
  365. #endif
  366. #ifdef __GNUC__
  367. #pragma GCC diagnostic pop
  368. #endif