blob: d854aadbd430519cba04e8e76332f1cb611076f3 [file] [log] [blame]
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001//===- llvm/Support/KnownBits.h - Stores known zeros/ones -------*- C++ -*-===//
2//
Andrew Walbran16937d02019-10-22 13:54:20 +01003// 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
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01006//
7//===----------------------------------------------------------------------===//
8//
9// This file contains a class for representing known zeros and ones used by
10// computeKnownBits.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_SUPPORT_KNOWNBITS_H
15#define LLVM_SUPPORT_KNOWNBITS_H
16
17#include "llvm/ADT/APInt.h"
Olivier Deprezf4ef2d02021-04-20 13:36:24 +020018#include "llvm/ADT/Optional.h"
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010019
20namespace llvm {
21
22// Struct for tracking the known zeros and ones of a value.
23struct KnownBits {
24 APInt Zero;
25 APInt One;
26
27private:
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
32public:
33 // Default construct Zero and One.
34 KnownBits() {}
35
36 /// Create a known bits object of BitWidth bits initialized to unknown.
37 KnownBits(unsigned BitWidth) : Zero(BitWidth, 0), One(BitWidth, 0) {}
38
39 /// Get the bit width of this value.
40 unsigned getBitWidth() const {
41 assert(Zero.getBitWidth() == One.getBitWidth() &&
42 "Zero and One should have the same width!");
43 return Zero.getBitWidth();
44 }
45
46 /// Returns true if there is conflicting information.
47 bool hasConflict() const { return Zero.intersects(One); }
48
49 /// Returns true if we know the value of all bits.
50 bool isConstant() const {
51 assert(!hasConflict() && "KnownBits conflict!");
52 return Zero.countPopulation() + One.countPopulation() == getBitWidth();
53 }
54
55 /// Returns the value when all bits have a known value. This just returns One
56 /// with a protective assertion.
57 const APInt &getConstant() const {
58 assert(isConstant() && "Can only get value when all bits are known");
59 return One;
60 }
61
62 /// Returns true if we don't know any bits.
63 bool isUnknown() const { return Zero.isNullValue() && One.isNullValue(); }
64
65 /// Resets the known state of all bits.
66 void resetAll() {
67 Zero.clearAllBits();
68 One.clearAllBits();
69 }
70
71 /// Returns true if value is all zero.
72 bool isZero() const {
73 assert(!hasConflict() && "KnownBits conflict!");
74 return Zero.isAllOnesValue();
75 }
76
77 /// Returns true if value is all one bits.
78 bool isAllOnes() const {
79 assert(!hasConflict() && "KnownBits conflict!");
80 return One.isAllOnesValue();
81 }
82
83 /// Make all bits known to be zero and discard any previous information.
84 void setAllZero() {
85 Zero.setAllBits();
86 One.clearAllBits();
87 }
88
89 /// Make all bits known to be one and discard any previous information.
90 void setAllOnes() {
91 Zero.clearAllBits();
92 One.setAllBits();
93 }
94
95 /// Returns true if this value is known to be negative.
96 bool isNegative() const { return One.isSignBitSet(); }
97
98 /// Returns true if this value is known to be non-negative.
99 bool isNonNegative() const { return Zero.isSignBitSet(); }
100
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200101 /// Returns true if this value is known to be non-zero.
102 bool isNonZero() const { return !One.isNullValue(); }
103
104 /// Returns true if this value is known to be positive.
105 bool isStrictlyPositive() const { return Zero.isSignBitSet() && !One.isNullValue(); }
106
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100107 /// Make this value negative.
108 void makeNegative() {
109 One.setSignBit();
110 }
111
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100112 /// Make this value non-negative.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100113 void makeNonNegative() {
114 Zero.setSignBit();
115 }
116
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200117 /// Return the minimal unsigned value possible given these KnownBits.
118 APInt getMinValue() const {
119 // Assume that all bits that aren't known-ones are zeros.
120 return One;
121 }
122
123 /// Return the minimal signed value possible given these KnownBits.
124 APInt getSignedMinValue() const {
125 // Assume that all bits that aren't known-ones are zeros.
126 APInt Min = One;
127 // Sign bit is unknown.
128 if (Zero.isSignBitClear())
129 Min.setSignBit();
130 return Min;
131 }
132
133 /// Return the maximal unsigned value possible given these KnownBits.
134 APInt getMaxValue() const {
135 // Assume that all bits that aren't known-zeros are ones.
136 return ~Zero;
137 }
138
139 /// Return the maximal signed value possible given these KnownBits.
140 APInt getSignedMaxValue() const {
141 // Assume that all bits that aren't known-zeros are ones.
142 APInt Max = ~Zero;
143 // Sign bit is unknown.
144 if (One.isSignBitClear())
145 Max.clearSignBit();
146 return Max;
147 }
148
149 /// Return known bits for a truncation of the value we're tracking.
Andrew Walbran3d2c1972020-04-07 12:24:26 +0100150 KnownBits trunc(unsigned BitWidth) const {
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100151 return KnownBits(Zero.trunc(BitWidth), One.trunc(BitWidth));
152 }
153
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200154 /// Return known bits for an "any" extension of the value we're tracking,
155 /// where we don't know anything about the extended bits.
156 KnownBits anyext(unsigned BitWidth) const {
157 return KnownBits(Zero.zext(BitWidth), One.zext(BitWidth));
158 }
159
160 /// Return known bits for a zero extension of the value we're tracking.
161 KnownBits zext(unsigned BitWidth) const {
Andrew Walbran3d2c1972020-04-07 12:24:26 +0100162 unsigned OldBitWidth = getBitWidth();
163 APInt NewZero = Zero.zext(BitWidth);
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200164 NewZero.setBitsFrom(OldBitWidth);
Andrew Walbran3d2c1972020-04-07 12:24:26 +0100165 return KnownBits(NewZero, One.zext(BitWidth));
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100166 }
167
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200168 /// Return known bits for a sign extension of the value we're tracking.
Andrew Walbran3d2c1972020-04-07 12:24:26 +0100169 KnownBits sext(unsigned BitWidth) const {
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100170 return KnownBits(Zero.sext(BitWidth), One.sext(BitWidth));
171 }
172
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200173 /// Return known bits for an "any" extension or truncation of the value we're
174 /// tracking.
175 KnownBits anyextOrTrunc(unsigned BitWidth) const {
Andrew Walbran3d2c1972020-04-07 12:24:26 +0100176 if (BitWidth > getBitWidth())
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200177 return anyext(BitWidth);
178 if (BitWidth < getBitWidth())
179 return trunc(BitWidth);
180 return *this;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100181 }
182
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200183 /// Return known bits for a zero extension or truncation of the value we're
184 /// tracking.
185 KnownBits zextOrTrunc(unsigned BitWidth) const {
186 if (BitWidth > getBitWidth())
187 return zext(BitWidth);
188 if (BitWidth < getBitWidth())
189 return trunc(BitWidth);
190 return *this;
191 }
192
193 /// Return known bits for a sign extension or truncation of the value we're
194 /// tracking.
195 KnownBits sextOrTrunc(unsigned BitWidth) const {
196 if (BitWidth > getBitWidth())
197 return sext(BitWidth);
198 if (BitWidth < getBitWidth())
199 return trunc(BitWidth);
200 return *this;
201 }
202
203 /// Return known bits for a in-register sign extension of the value we're
204 /// tracking.
205 KnownBits sextInReg(unsigned SrcBitWidth) const;
206
207 /// Return a KnownBits with the extracted bits
208 /// [bitPosition,bitPosition+numBits).
209 KnownBits extractBits(unsigned NumBits, unsigned BitPosition) const {
210 return KnownBits(Zero.extractBits(NumBits, BitPosition),
211 One.extractBits(NumBits, BitPosition));
212 }
213
214 /// Return KnownBits based on this, but updated given that the underlying
215 /// value is known to be greater than or equal to Val.
216 KnownBits makeGE(const APInt &Val) const;
217
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100218 /// Returns the minimum number of trailing zero bits.
219 unsigned countMinTrailingZeros() const {
220 return Zero.countTrailingOnes();
221 }
222
223 /// Returns the minimum number of trailing one bits.
224 unsigned countMinTrailingOnes() const {
225 return One.countTrailingOnes();
226 }
227
228 /// Returns the minimum number of leading zero bits.
229 unsigned countMinLeadingZeros() const {
230 return Zero.countLeadingOnes();
231 }
232
233 /// Returns the minimum number of leading one bits.
234 unsigned countMinLeadingOnes() const {
235 return One.countLeadingOnes();
236 }
237
238 /// Returns the number of times the sign bit is replicated into the other
239 /// bits.
240 unsigned countMinSignBits() const {
241 if (isNonNegative())
242 return countMinLeadingZeros();
243 if (isNegative())
244 return countMinLeadingOnes();
245 return 0;
246 }
247
248 /// Returns the maximum number of trailing zero bits possible.
249 unsigned countMaxTrailingZeros() const {
250 return One.countTrailingZeros();
251 }
252
253 /// Returns the maximum number of trailing one bits possible.
254 unsigned countMaxTrailingOnes() const {
255 return Zero.countTrailingZeros();
256 }
257
258 /// Returns the maximum number of leading zero bits possible.
259 unsigned countMaxLeadingZeros() const {
260 return One.countLeadingZeros();
261 }
262
263 /// Returns the maximum number of leading one bits possible.
264 unsigned countMaxLeadingOnes() const {
265 return Zero.countLeadingZeros();
266 }
267
268 /// Returns the number of bits known to be one.
269 unsigned countMinPopulation() const {
270 return One.countPopulation();
271 }
272
273 /// Returns the maximum number of bits that could be one.
274 unsigned countMaxPopulation() const {
275 return getBitWidth() - Zero.countPopulation();
276 }
277
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200278 /// Create known bits from a known constant.
279 static KnownBits makeConstant(const APInt &C) {
280 return KnownBits(~C, C);
281 }
282
283 /// Compute known bits common to LHS and RHS.
284 static KnownBits commonBits(const KnownBits &LHS, const KnownBits &RHS) {
285 return KnownBits(LHS.Zero & RHS.Zero, LHS.One & RHS.One);
286 }
287
Andrew Walbran3d2c1972020-04-07 12:24:26 +0100288 /// Compute known bits resulting from adding LHS, RHS and a 1-bit Carry.
289 static KnownBits computeForAddCarry(
290 const KnownBits &LHS, const KnownBits &RHS, const KnownBits &Carry);
291
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100292 /// Compute known bits resulting from adding LHS and RHS.
293 static KnownBits computeForAddSub(bool Add, bool NSW, const KnownBits &LHS,
294 KnownBits RHS);
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200295
296 /// Compute known bits resulting from multiplying LHS and RHS.
297 static KnownBits computeForMul(const KnownBits &LHS, const KnownBits &RHS);
298
299 /// Compute known bits for udiv(LHS, RHS).
300 static KnownBits udiv(const KnownBits &LHS, const KnownBits &RHS);
301
302 /// Compute known bits for urem(LHS, RHS).
303 static KnownBits urem(const KnownBits &LHS, const KnownBits &RHS);
304
305 /// Compute known bits for srem(LHS, RHS).
306 static KnownBits srem(const KnownBits &LHS, const KnownBits &RHS);
307
308 /// Compute known bits for umax(LHS, RHS).
309 static KnownBits umax(const KnownBits &LHS, const KnownBits &RHS);
310
311 /// Compute known bits for umin(LHS, RHS).
312 static KnownBits umin(const KnownBits &LHS, const KnownBits &RHS);
313
314 /// Compute known bits for smax(LHS, RHS).
315 static KnownBits smax(const KnownBits &LHS, const KnownBits &RHS);
316
317 /// Compute known bits for smin(LHS, RHS).
318 static KnownBits smin(const KnownBits &LHS, const KnownBits &RHS);
319
320 /// Compute known bits for shl(LHS, RHS).
321 /// NOTE: RHS (shift amount) bitwidth doesn't need to be the same as LHS.
322 static KnownBits shl(const KnownBits &LHS, const KnownBits &RHS);
323
324 /// Compute known bits for lshr(LHS, RHS).
325 /// NOTE: RHS (shift amount) bitwidth doesn't need to be the same as LHS.
326 static KnownBits lshr(const KnownBits &LHS, const KnownBits &RHS);
327
328 /// Compute known bits for ashr(LHS, RHS).
329 /// NOTE: RHS (shift amount) bitwidth doesn't need to be the same as LHS.
330 static KnownBits ashr(const KnownBits &LHS, const KnownBits &RHS);
331
332 /// Determine if these known bits always give the same ICMP_EQ result.
333 static Optional<bool> eq(const KnownBits &LHS, const KnownBits &RHS);
334
335 /// Determine if these known bits always give the same ICMP_NE result.
336 static Optional<bool> ne(const KnownBits &LHS, const KnownBits &RHS);
337
338 /// Determine if these known bits always give the same ICMP_UGT result.
339 static Optional<bool> ugt(const KnownBits &LHS, const KnownBits &RHS);
340
341 /// Determine if these known bits always give the same ICMP_UGE result.
342 static Optional<bool> uge(const KnownBits &LHS, const KnownBits &RHS);
343
344 /// Determine if these known bits always give the same ICMP_ULT result.
345 static Optional<bool> ult(const KnownBits &LHS, const KnownBits &RHS);
346
347 /// Determine if these known bits always give the same ICMP_ULE result.
348 static Optional<bool> ule(const KnownBits &LHS, const KnownBits &RHS);
349
350 /// Determine if these known bits always give the same ICMP_SGT result.
351 static Optional<bool> sgt(const KnownBits &LHS, const KnownBits &RHS);
352
353 /// Determine if these known bits always give the same ICMP_SGE result.
354 static Optional<bool> sge(const KnownBits &LHS, const KnownBits &RHS);
355
356 /// Determine if these known bits always give the same ICMP_SLT result.
357 static Optional<bool> slt(const KnownBits &LHS, const KnownBits &RHS);
358
359 /// Determine if these known bits always give the same ICMP_SLE result.
360 static Optional<bool> sle(const KnownBits &LHS, const KnownBits &RHS);
361
362 /// Insert the bits from a smaller known bits starting at bitPosition.
363 void insertBits(const KnownBits &SubBits, unsigned BitPosition) {
364 Zero.insertBits(SubBits.Zero, BitPosition);
365 One.insertBits(SubBits.One, BitPosition);
366 }
367
368 /// Return a subset of the known bits from [bitPosition,bitPosition+numBits).
369 KnownBits extractBits(unsigned NumBits, unsigned BitPosition) {
370 return KnownBits(Zero.extractBits(NumBits, BitPosition),
371 One.extractBits(NumBits, BitPosition));
372 }
373
374 /// Update known bits based on ANDing with RHS.
375 KnownBits &operator&=(const KnownBits &RHS);
376
377 /// Update known bits based on ORing with RHS.
378 KnownBits &operator|=(const KnownBits &RHS);
379
380 /// Update known bits based on XORing with RHS.
381 KnownBits &operator^=(const KnownBits &RHS);
382
383 /// Compute known bits for the absolute value.
384 KnownBits abs(bool IntMinIsPoison = false) const;
385
386 KnownBits byteSwap() {
387 return KnownBits(Zero.byteSwap(), One.byteSwap());
388 }
389
390 KnownBits reverseBits() {
391 return KnownBits(Zero.reverseBits(), One.reverseBits());
392 }
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100393};
394
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200395inline KnownBits operator&(KnownBits LHS, const KnownBits &RHS) {
396 LHS &= RHS;
397 return LHS;
398}
399
400inline KnownBits operator&(const KnownBits &LHS, KnownBits &&RHS) {
401 RHS &= LHS;
402 return std::move(RHS);
403}
404
405inline KnownBits operator|(KnownBits LHS, const KnownBits &RHS) {
406 LHS |= RHS;
407 return LHS;
408}
409
410inline KnownBits operator|(const KnownBits &LHS, KnownBits &&RHS) {
411 RHS |= LHS;
412 return std::move(RHS);
413}
414
415inline KnownBits operator^(KnownBits LHS, const KnownBits &RHS) {
416 LHS ^= RHS;
417 return LHS;
418}
419
420inline KnownBits operator^(const KnownBits &LHS, KnownBits &&RHS) {
421 RHS ^= LHS;
422 return std::move(RHS);
423}
424
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100425} // end namespace llvm
426
427#endif