KnownBits.h 14 KB

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