LLVM 23.0.0git
KnownBits.h
Go to the documentation of this file.
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
14#ifndef LLVM_SUPPORT_KNOWNBITS_H
15#define LLVM_SUPPORT_KNOWNBITS_H
16
17#include "llvm/ADT/APInt.h"
19#include <optional>
20
21namespace llvm {
22
23// Struct for tracking the known zeros and ones of a value.
24struct KnownBits {
27
28private:
29 // Internal constructor for creating a KnownBits from two APInts.
31 : Zero(std::move(Zero)), One(std::move(One)) {}
32
33 // Flip the range of values: [-0x80000000, 0x7FFFFFFF] <-> [0, 0xFFFFFFFF]
34 static KnownBits flipSignBit(const KnownBits &Val);
35
36public:
37 // Default construct Zero and One.
38 KnownBits() = default;
39
40 /// Create a known bits object of BitWidth bits initialized to unknown.
41 KnownBits(unsigned BitWidth) : Zero(BitWidth, 0), One(BitWidth, 0) {}
42
43 /// Get the bit width of this value.
44 unsigned getBitWidth() const {
45 assert(Zero.getBitWidth() == One.getBitWidth() &&
46 "Zero and One should have the same width!");
47 return Zero.getBitWidth();
48 }
49
50 /// Returns true if there is conflicting information.
51 bool hasConflict() const { return Zero.intersects(One); }
52
53 /// Returns true if we know the value of all bits.
54 bool isConstant() const {
55 return Zero.popcount() + One.popcount() == getBitWidth();
56 }
57
58 /// Returns the value when all bits have a known value. This just returns One
59 /// with a protective assertion.
60 const APInt &getConstant() const {
61 assert(isConstant() && "Can only get value when all bits are known");
62 return One;
63 }
64
65 /// Returns true if we don't know any bits.
66 bool isUnknown() const { return Zero.isZero() && One.isZero(); }
67
68 /// Returns true if we don't know the sign bit.
69 bool isSignUnknown() const {
70 return !Zero.isSignBitSet() && !One.isSignBitSet();
71 }
72
73 /// Resets the known state of all bits.
74 void resetAll() {
75 Zero.clearAllBits();
76 One.clearAllBits();
77 }
78
79 /// Returns true if value is all zero.
80 bool isZero() const { return Zero.isAllOnes(); }
81
82 /// Returns true if value is all one bits.
83 bool isAllOnes() const { return One.isAllOnes(); }
84
85 /// Make all bits known to be zero and discard any previous information.
86 void setAllZero() {
87 Zero.setAllBits();
88 One.clearAllBits();
89 }
90
91 /// Make all bits known to be one and discard any previous information.
92 void setAllOnes() {
93 Zero.clearAllBits();
94 One.setAllBits();
95 }
96
97 /// Make all bits known to be both zero and one. Useful before a loop that
98 /// calls intersectWith.
100 Zero.setAllBits();
101 One.setAllBits();
102 }
103
104 /// Returns true if this value is known to be negative.
105 bool isNegative() const { return One.isSignBitSet(); }
106
107 /// Returns true if this value is known to be non-negative.
108 bool isNonNegative() const { return Zero.isSignBitSet(); }
109
110 /// Returns true if this value is known to be non-zero.
111 bool isNonZero() const { return !One.isZero(); }
112
113 /// Returns true if this value is known to be positive.
114 bool isStrictlyPositive() const {
115 return Zero.isSignBitSet() && !One.isZero();
116 }
117
118 /// Returns true if this value is known to be non-positive.
119 bool isNonPositive() const { return getSignedMaxValue().isNonPositive(); }
120
121 /// Make this value negative.
123 One.setSignBit();
124 }
125
126 /// Make this value non-negative.
128 Zero.setSignBit();
129 }
130
131 /// Return the minimal unsigned value possible given these KnownBits.
133 // Assume that all bits that aren't known-ones are zeros.
134 return One;
135 }
136
137 /// Return the minimal signed value possible given these KnownBits.
139 // Assume that all bits that aren't known-ones are zeros.
140 APInt Min = One;
141 // Sign bit is unknown.
142 if (Zero.isSignBitClear())
143 Min.setSignBit();
144 return Min;
145 }
146
147 /// Return the maximal unsigned value possible given these KnownBits.
149 // Assume that all bits that aren't known-zeros are ones.
150 return ~Zero;
151 }
152
153 /// Return the maximal signed value possible given these KnownBits.
155 // Assume that all bits that aren't known-zeros are ones.
156 APInt Max = ~Zero;
157 // Sign bit is unknown.
158 if (One.isSignBitClear())
159 Max.clearSignBit();
160 return Max;
161 }
162
163 /// Return known bits for a truncation of the value we're tracking.
164 KnownBits trunc(unsigned BitWidth) const {
165 return KnownBits(Zero.trunc(BitWidth), One.trunc(BitWidth));
166 }
167
168 /// Return known bits for an "any" extension of the value we're tracking,
169 /// where we don't know anything about the extended bits.
170 KnownBits anyext(unsigned BitWidth) const {
171 return KnownBits(Zero.zext(BitWidth), One.zext(BitWidth));
172 }
173
174 /// Return known bits for a zero extension of the value we're tracking.
175 KnownBits zext(unsigned BitWidth) const {
176 unsigned OldBitWidth = getBitWidth();
177 APInt NewZero = Zero.zext(BitWidth);
178 NewZero.setBitsFrom(OldBitWidth);
179 return KnownBits(NewZero, One.zext(BitWidth));
180 }
181
182 /// Return known bits for a sign extension of the value we're tracking.
183 KnownBits sext(unsigned BitWidth) const {
184 return KnownBits(Zero.sext(BitWidth), One.sext(BitWidth));
185 }
186
187 /// Return known bits for an "any" extension or truncation of the value we're
188 /// tracking.
189 KnownBits anyextOrTrunc(unsigned BitWidth) const {
190 if (BitWidth > getBitWidth())
191 return anyext(BitWidth);
192 if (BitWidth < getBitWidth())
193 return trunc(BitWidth);
194 return *this;
195 }
196
197 /// Return known bits for a zero extension or truncation of the value we're
198 /// tracking.
199 KnownBits zextOrTrunc(unsigned BitWidth) const {
200 if (BitWidth > getBitWidth())
201 return zext(BitWidth);
202 if (BitWidth < getBitWidth())
203 return trunc(BitWidth);
204 return *this;
205 }
206
207 /// Return known bits for a sign extension or truncation of the value we're
208 /// tracking.
209 KnownBits sextOrTrunc(unsigned BitWidth) const {
210 if (BitWidth > getBitWidth())
211 return sext(BitWidth);
212 if (BitWidth < getBitWidth())
213 return trunc(BitWidth);
214 return *this;
215 }
216
217 /// Truncate with signed saturation (signed input -> signed output)
218 LLVM_ABI KnownBits truncSSat(unsigned BitWidth) const;
219
220 /// Truncate with signed saturation to unsigned (signed input -> unsigned
221 /// output)
222 LLVM_ABI KnownBits truncSSatU(unsigned BitWidth) const;
223
224 /// Truncate with unsigned saturation (unsigned input -> unsigned output)
225 LLVM_ABI KnownBits truncUSat(unsigned BitWidth) const;
226
227 /// Return known bits for a in-register sign extension of the value we're
228 /// tracking.
229 LLVM_ABI KnownBits sextInReg(unsigned SrcBitWidth) const;
230
231 /// Insert the bits from a smaller known bits starting at bitPosition.
232 void insertBits(const KnownBits &SubBits, unsigned BitPosition) {
233 Zero.insertBits(SubBits.Zero, BitPosition);
234 One.insertBits(SubBits.One, BitPosition);
235 }
236
237 /// Return a subset of the known bits from [bitPosition,bitPosition+numBits).
238 KnownBits extractBits(unsigned NumBits, unsigned BitPosition) const {
239 return KnownBits(Zero.extractBits(NumBits, BitPosition),
240 One.extractBits(NumBits, BitPosition));
241 }
242
243 /// Concatenate the bits from \p Lo onto the bottom of *this. This is
244 /// equivalent to:
245 /// (this->zext(NewWidth) << Lo.getBitWidth()) | Lo.zext(NewWidth)
246 KnownBits concat(const KnownBits &Lo) const {
247 return KnownBits(Zero.concat(Lo.Zero), One.concat(Lo.One));
248 }
249
250 /// Return KnownBits based on this, but updated given that the underlying
251 /// value is known to be greater than or equal to Val.
252 LLVM_ABI KnownBits makeGE(const APInt &Val) const;
253
254 /// Returns the minimum number of trailing zero bits.
255 unsigned countMinTrailingZeros() const { return Zero.countr_one(); }
256
257 /// Returns the minimum number of trailing one bits.
258 unsigned countMinTrailingOnes() const { return One.countr_one(); }
259
260 /// Returns the minimum number of leading zero bits.
261 unsigned countMinLeadingZeros() const { return Zero.countl_one(); }
262
263 /// Returns the minimum number of leading one bits.
264 unsigned countMinLeadingOnes() const { return One.countl_one(); }
265
266 /// Returns the number of times the sign bit is replicated into the other
267 /// bits.
268 unsigned countMinSignBits() const {
269 if (isNonNegative())
270 return countMinLeadingZeros();
271 if (isNegative())
272 return countMinLeadingOnes();
273 // Every value has at least 1 sign bit.
274 return 1;
275 }
276
277 /// Returns the maximum number of bits needed to represent all possible
278 /// signed values with these known bits. This is the inverse of the minimum
279 /// number of known sign bits. Examples for bitwidth 5:
280 /// 110?? --> 4
281 /// 0000? --> 2
282 unsigned countMaxSignificantBits() const {
283 return getBitWidth() - countMinSignBits() + 1;
284 }
285
286 /// Returns the maximum number of trailing zero bits possible.
287 unsigned countMaxTrailingZeros() const { return One.countr_zero(); }
288
289 /// Returns the maximum number of trailing one bits possible.
290 unsigned countMaxTrailingOnes() const { return Zero.countr_zero(); }
291
292 /// Returns the maximum number of leading zero bits possible.
293 unsigned countMaxLeadingZeros() const { return One.countl_zero(); }
294
295 /// Returns the maximum number of leading one bits possible.
296 unsigned countMaxLeadingOnes() const { return Zero.countl_zero(); }
297
298 /// Returns the number of bits known to be one.
299 unsigned countMinPopulation() const { return One.popcount(); }
300
301 /// Returns the maximum number of bits that could be one.
302 unsigned countMaxPopulation() const {
303 return getBitWidth() - Zero.popcount();
304 }
305
306 /// Returns the maximum number of bits needed to represent all possible
307 /// unsigned values with these known bits. This is the inverse of the
308 /// minimum number of leading zeros.
309 unsigned countMaxActiveBits() const {
311 }
312
313 /// Create known bits from a known constant.
314 static KnownBits makeConstant(const APInt &C) {
315 return KnownBits(~C, C);
316 }
317
318 /// Returns KnownBits information that is known to be true for both this and
319 /// RHS.
320 ///
321 /// When an operation is known to return one of its operands, this can be used
322 /// to combine information about the known bits of the operands to get the
323 /// information that must be true about the result.
324 KnownBits intersectWith(const KnownBits &RHS) const {
325 return KnownBits(Zero & RHS.Zero, One & RHS.One);
326 }
327
328 /// Returns KnownBits information that is known to be true for either this or
329 /// RHS or both.
330 ///
331 /// This can be used to combine different sources of information about the
332 /// known bits of a single value, e.g. information about the low bits and the
333 /// high bits of the result of a multiplication.
334 KnownBits unionWith(const KnownBits &RHS) const {
335 return KnownBits(Zero | RHS.Zero, One | RHS.One);
336 }
337
338 /// Return true if LHS and RHS have no common bits set.
339 static bool haveNoCommonBitsSet(const KnownBits &LHS, const KnownBits &RHS) {
340 return (LHS.Zero | RHS.Zero).isAllOnes();
341 }
342
343 /// Compute known bits resulting from adding LHS, RHS and a 1-bit Carry.
345 const KnownBits &RHS,
346 const KnownBits &Carry);
347
348 /// Compute known bits resulting from adding LHS and RHS.
349 LLVM_ABI static KnownBits computeForAddSub(bool Add, bool NSW, bool NUW,
350 const KnownBits &LHS,
351 const KnownBits &RHS);
352
353 /// Compute known bits results from subtracting RHS from LHS with 1-bit
354 /// Borrow.
357 const KnownBits &Borrow);
358
359 /// Compute knownbits resulting from addition of LHS and RHS.
360 static KnownBits add(const KnownBits &LHS, const KnownBits &RHS,
361 bool NSW = false, bool NUW = false) {
362 return computeForAddSub(/*Add=*/true, NSW, NUW, LHS, RHS);
363 }
364
365 /// Compute knownbits resulting from subtraction of LHS and RHS.
366 static KnownBits sub(const KnownBits &LHS, const KnownBits &RHS,
367 bool NSW = false, bool NUW = false) {
368 return computeForAddSub(/*Add=*/false, NSW, NUW, LHS, RHS);
369 }
370
371 /// Compute knownbits resulting from llvm.sadd.sat(LHS, RHS)
373 const KnownBits &RHS);
374
375 /// Compute knownbits resulting from llvm.uadd.sat(LHS, RHS)
377 const KnownBits &RHS);
378
379 /// Compute knownbits resulting from llvm.ssub.sat(LHS, RHS)
381 const KnownBits &RHS);
382
383 /// Compute knownbits resulting from llvm.usub.sat(LHS, RHS)
385 const KnownBits &RHS);
386
387 /// Compute knownbits resulting from APIntOps::avgFloorS
389 const KnownBits &RHS);
390
391 /// Compute knownbits resulting from APIntOps::avgFloorU
393 const KnownBits &RHS);
394
395 /// Compute knownbits resulting from APIntOps::avgCeilS
397 const KnownBits &RHS);
398
399 /// Compute knownbits resulting from APIntOps::avgCeilU
401 const KnownBits &RHS);
402
403 /// Compute known bits resulting from multiplying LHS and RHS.
404 LLVM_ABI static KnownBits mul(const KnownBits &LHS, const KnownBits &RHS,
405 bool NoUndefSelfMultiply = false);
406
407 /// Compute known bits from sign-extended multiply-hi.
408 LLVM_ABI static KnownBits mulhs(const KnownBits &LHS, const KnownBits &RHS);
409
410 /// Compute known bits from zero-extended multiply-hi.
411 LLVM_ABI static KnownBits mulhu(const KnownBits &LHS, const KnownBits &RHS);
412
413 /// Compute known bits for sdiv(LHS, RHS).
414 LLVM_ABI static KnownBits sdiv(const KnownBits &LHS, const KnownBits &RHS,
415 bool Exact = false);
416
417 /// Compute known bits for udiv(LHS, RHS).
418 LLVM_ABI static KnownBits udiv(const KnownBits &LHS, const KnownBits &RHS,
419 bool Exact = false);
420
421 /// Compute known bits for urem(LHS, RHS).
422 LLVM_ABI static KnownBits urem(const KnownBits &LHS, const KnownBits &RHS);
423
424 /// Compute known bits for srem(LHS, RHS).
425 LLVM_ABI static KnownBits srem(const KnownBits &LHS, const KnownBits &RHS);
426
427 /// Compute known bits for umax(LHS, RHS).
428 LLVM_ABI static KnownBits umax(const KnownBits &LHS, const KnownBits &RHS);
429
430 /// Compute known bits for umin(LHS, RHS).
431 LLVM_ABI static KnownBits umin(const KnownBits &LHS, const KnownBits &RHS);
432
433 /// Compute known bits for smax(LHS, RHS).
434 LLVM_ABI static KnownBits smax(const KnownBits &LHS, const KnownBits &RHS);
435
436 /// Compute known bits for smin(LHS, RHS).
437 LLVM_ABI static KnownBits smin(const KnownBits &LHS, const KnownBits &RHS);
438
439 /// Compute known bits for abdu(LHS, RHS).
440 LLVM_ABI static KnownBits abdu(const KnownBits &LHS, const KnownBits &RHS);
441
442 /// Compute known bits for abds(LHS, RHS).
444
445 /// Compute known bits for shl(LHS, RHS).
446 /// NOTE: RHS (shift amount) bitwidth doesn't need to be the same as LHS.
447 LLVM_ABI static KnownBits shl(const KnownBits &LHS, const KnownBits &RHS,
448 bool NUW = false, bool NSW = false,
449 bool ShAmtNonZero = false);
450
451 /// Compute known bits for lshr(LHS, RHS).
452 /// NOTE: RHS (shift amount) bitwidth doesn't need to be the same as LHS.
453 LLVM_ABI static KnownBits lshr(const KnownBits &LHS, const KnownBits &RHS,
454 bool ShAmtNonZero = false, bool Exact = false);
455
456 /// Compute known bits for ashr(LHS, RHS).
457 /// NOTE: RHS (shift amount) bitwidth doesn't need to be the same as LHS.
458 LLVM_ABI static KnownBits ashr(const KnownBits &LHS, const KnownBits &RHS,
459 bool ShAmtNonZero = false, bool Exact = false);
460
461 /// Determine if these known bits always give the same ICMP_EQ result.
462 LLVM_ABI static std::optional<bool> eq(const KnownBits &LHS,
463 const KnownBits &RHS);
464
465 /// Determine if these known bits always give the same ICMP_NE result.
466 LLVM_ABI static std::optional<bool> ne(const KnownBits &LHS,
467 const KnownBits &RHS);
468
469 /// Determine if these known bits always give the same ICMP_UGT result.
470 LLVM_ABI static std::optional<bool> ugt(const KnownBits &LHS,
471 const KnownBits &RHS);
472
473 /// Determine if these known bits always give the same ICMP_UGE result.
474 LLVM_ABI static std::optional<bool> uge(const KnownBits &LHS,
475 const KnownBits &RHS);
476
477 /// Determine if these known bits always give the same ICMP_ULT result.
478 LLVM_ABI static std::optional<bool> ult(const KnownBits &LHS,
479 const KnownBits &RHS);
480
481 /// Determine if these known bits always give the same ICMP_ULE result.
482 LLVM_ABI static std::optional<bool> ule(const KnownBits &LHS,
483 const KnownBits &RHS);
484
485 /// Determine if these known bits always give the same ICMP_SGT result.
486 LLVM_ABI static std::optional<bool> sgt(const KnownBits &LHS,
487 const KnownBits &RHS);
488
489 /// Determine if these known bits always give the same ICMP_SGE result.
490 LLVM_ABI static std::optional<bool> sge(const KnownBits &LHS,
491 const KnownBits &RHS);
492
493 /// Determine if these known bits always give the same ICMP_SLT result.
494 LLVM_ABI static std::optional<bool> slt(const KnownBits &LHS,
495 const KnownBits &RHS);
496
497 /// Determine if these known bits always give the same ICMP_SLE result.
498 LLVM_ABI static std::optional<bool> sle(const KnownBits &LHS,
499 const KnownBits &RHS);
500
501 /// Update known bits based on ANDing with RHS.
503
504 /// Update known bits based on ORing with RHS.
506
507 /// Update known bits based on XORing with RHS.
509
510 /// Shift known bits left by ShAmt. Shift in bits are unknown.
511 KnownBits &operator<<=(unsigned ShAmt) {
512 Zero <<= ShAmt;
513 One <<= ShAmt;
514 return *this;
515 }
516
517 /// Shift known bits right by ShAmt. Shifted in bits are unknown.
518 KnownBits &operator>>=(unsigned ShAmt) {
519 Zero.lshrInPlace(ShAmt);
520 One.lshrInPlace(ShAmt);
521 return *this;
522 }
523
524 /// Compute known bits for the absolute value.
525 LLVM_ABI KnownBits abs(bool IntMinIsPoison = false) const;
526
527 /// Compute known bits for horizontal add for a vector with NumElts
528 /// elements, where each element has the known bits represented by this
529 /// object.
530 LLVM_ABI KnownBits reduceAdd(unsigned NumElts) const;
531
532 KnownBits byteSwap() const {
533 return KnownBits(Zero.byteSwap(), One.byteSwap());
534 }
535
536 KnownBits reverseBits() const {
537 return KnownBits(Zero.reverseBits(), One.reverseBits());
538 }
539
540 /// Compute known bits for X & -X, which has only the lowest bit set of X set.
541 /// The name comes from the X86 BMI instruction
542 LLVM_ABI KnownBits blsi() const;
543
544 /// Compute known bits for X ^ (X - 1), which has all bits up to and including
545 /// the lowest set bit of X set. The name comes from the X86 BMI instruction.
546 LLVM_ABI KnownBits blsmsk() const;
547
548 bool operator==(const KnownBits &Other) const {
549 return Zero == Other.Zero && One == Other.One;
550 }
551
552 bool operator!=(const KnownBits &Other) const { return !(*this == Other); }
553
554 LLVM_ABI void print(raw_ostream &OS) const;
555
556#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
557 LLVM_DUMP_METHOD void dump() const;
558#endif
559
560private:
561 // Internal helper for getting the initial KnownBits for an `srem` or `urem`
562 // operation with the low-bits set.
563 static KnownBits remGetLowBits(const KnownBits &LHS, const KnownBits &RHS);
564};
565
567 LHS &= RHS;
568 return LHS;
569}
570
572 RHS &= LHS;
573 return std::move(RHS);
574}
575
577 LHS |= RHS;
578 return LHS;
579}
580
582 RHS |= LHS;
583 return std::move(RHS);
584}
585
587 LHS ^= RHS;
588 return LHS;
589}
590
592 RHS ^= LHS;
593 return std::move(RHS);
594}
595
596inline raw_ostream &operator<<(raw_ostream &OS, const KnownBits &Known) {
597 Known.print(OS);
598 return OS;
599}
600
601} // end namespace llvm
602
603#endif
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
This file implements a class to represent arbitrary precision integral constant values and operations...
#define LLVM_ABI
Definition Compiler.h:213
#define LLVM_DUMP_METHOD
Mark debug helper function definitions like dump() that should not be stripped from debug builds.
Definition Compiler.h:661
Value * RHS
Value * LHS
Class for arbitrary precision integers.
Definition APInt.h:78
void setBitsFrom(unsigned loBit)
Set the top bits starting from loBit.
Definition APInt.h:1394
void setSignBit()
Set the sign bit to 1.
Definition APInt.h:1349
bool isNonPositive() const
Determine if this APInt Value is non-positive (<= 0).
Definition APInt.h:362
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition raw_ostream.h:53
@ C
The default llvm calling convention, compatible with C.
Definition CallingConv.h:34
This is an optimization pass for GlobalISel generic memory operations.
Definition Types.h:26
APInt operator&(APInt a, const APInt &b)
Definition APInt.h:2132
APInt operator^(APInt a, const APInt &b)
Definition APInt.h:2172
@ Other
Any other memory.
Definition ModRef.h:68
@ Add
Sum of integers.
raw_ostream & operator<<(raw_ostream &OS, const APFixedPoint &FX)
constexpr unsigned BitWidth
OutputIt move(R &&Range, OutputIt Out)
Provide wrappers to std::move which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1915
APInt operator|(APInt a, const APInt &b)
Definition APInt.h:2152
Implement std::hash so that hash_code can be used in STL containers.
Definition BitVector.h:870
static KnownBits makeConstant(const APInt &C)
Create known bits from a known constant.
Definition KnownBits.h:314
static LLVM_ABI KnownBits sadd_sat(const KnownBits &LHS, const KnownBits &RHS)
Compute knownbits resulting from llvm.sadd.sat(LHS, RHS)
unsigned countMaxTrailingOnes() const
Returns the maximum number of trailing one bits possible.
Definition KnownBits.h:290
static LLVM_ABI std::optional< bool > eq(const KnownBits &LHS, const KnownBits &RHS)
Determine if these known bits always give the same ICMP_EQ result.
KnownBits anyextOrTrunc(unsigned BitWidth) const
Return known bits for an "any" extension or truncation of the value we're tracking.
Definition KnownBits.h:189
LLVM_ABI KnownBits sextInReg(unsigned SrcBitWidth) const
Return known bits for a in-register sign extension of the value we're tracking.
static LLVM_ABI KnownBits mulhu(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits from zero-extended multiply-hi.
unsigned countMinSignBits() const
Returns the number of times the sign bit is replicated into the other bits.
Definition KnownBits.h:268
static LLVM_ABI KnownBits smax(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for smax(LHS, RHS).
bool isNonNegative() const
Returns true if this value is known to be non-negative.
Definition KnownBits.h:108
bool isZero() const
Returns true if value is all zero.
Definition KnownBits.h:80
LLVM_ABI KnownBits blsi() const
Compute known bits for X & -X, which has only the lowest bit set of X set.
void makeNonNegative()
Make this value non-negative.
Definition KnownBits.h:127
static LLVM_ABI KnownBits usub_sat(const KnownBits &LHS, const KnownBits &RHS)
Compute knownbits resulting from llvm.usub.sat(LHS, RHS)
unsigned countMinLeadingOnes() const
Returns the minimum number of leading one bits.
Definition KnownBits.h:264
LLVM_ABI KnownBits reduceAdd(unsigned NumElts) const
Compute known bits for horizontal add for a vector with NumElts elements, where each element has the ...
unsigned countMinTrailingZeros() const
Returns the minimum number of trailing zero bits.
Definition KnownBits.h:255
static LLVM_ABI KnownBits ashr(const KnownBits &LHS, const KnownBits &RHS, bool ShAmtNonZero=false, bool Exact=false)
Compute known bits for ashr(LHS, RHS).
static LLVM_ABI KnownBits ssub_sat(const KnownBits &LHS, const KnownBits &RHS)
Compute knownbits resulting from llvm.ssub.sat(LHS, RHS)
static LLVM_ABI KnownBits urem(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for urem(LHS, RHS).
bool isUnknown() const
Returns true if we don't know any bits.
Definition KnownBits.h:66
unsigned countMaxTrailingZeros() const
Returns the maximum number of trailing zero bits possible.
Definition KnownBits.h:287
static LLVM_ABI std::optional< bool > ne(const KnownBits &LHS, const KnownBits &RHS)
Determine if these known bits always give the same ICMP_NE result.
LLVM_ABI KnownBits makeGE(const APInt &Val) const
Return KnownBits based on this, but updated given that the underlying value is known to be greater th...
APInt getSignedMaxValue() const
Return the maximal signed value possible given these KnownBits.
Definition KnownBits.h:154
LLVM_ABI KnownBits blsmsk() const
Compute known bits for X ^ (X - 1), which has all bits up to and including the lowest set bit of X se...
void makeNegative()
Make this value negative.
Definition KnownBits.h:122
KnownBits & operator>>=(unsigned ShAmt)
Shift known bits right by ShAmt. Shifted in bits are unknown.
Definition KnownBits.h:518
void setAllConflict()
Make all bits known to be both zero and one.
Definition KnownBits.h:99
KnownBits trunc(unsigned BitWidth) const
Return known bits for a truncation of the value we're tracking.
Definition KnownBits.h:164
KnownBits byteSwap() const
Definition KnownBits.h:532
bool hasConflict() const
Returns true if there is conflicting information.
Definition KnownBits.h:51
static LLVM_ABI std::optional< bool > sge(const KnownBits &LHS, const KnownBits &RHS)
Determine if these known bits always give the same ICMP_SGE result.
unsigned countMaxPopulation() const
Returns the maximum number of bits that could be one.
Definition KnownBits.h:302
void setAllZero()
Make all bits known to be zero and discard any previous information.
Definition KnownBits.h:86
KnownBits reverseBits() const
Definition KnownBits.h:536
LLVM_ABI KnownBits & operator|=(const KnownBits &RHS)
Update known bits based on ORing with RHS.
LLVM_ABI void print(raw_ostream &OS) const
KnownBits concat(const KnownBits &Lo) const
Concatenate the bits from Lo onto the bottom of *this.
Definition KnownBits.h:246
unsigned getBitWidth() const
Get the bit width of this value.
Definition KnownBits.h:44
static LLVM_ABI KnownBits umax(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for umax(LHS, RHS).
KnownBits zext(unsigned BitWidth) const
Return known bits for a zero extension of the value we're tracking.
Definition KnownBits.h:175
bool isConstant() const
Returns true if we know the value of all bits.
Definition KnownBits.h:54
void resetAll()
Resets the known state of all bits.
Definition KnownBits.h:74
LLVM_DUMP_METHOD void dump() const
KnownBits unionWith(const KnownBits &RHS) const
Returns KnownBits information that is known to be true for either this or RHS or both.
Definition KnownBits.h:334
static LLVM_ABI KnownBits lshr(const KnownBits &LHS, const KnownBits &RHS, bool ShAmtNonZero=false, bool Exact=false)
Compute known bits for lshr(LHS, RHS).
bool isSignUnknown() const
Returns true if we don't know the sign bit.
Definition KnownBits.h:69
bool isNonZero() const
Returns true if this value is known to be non-zero.
Definition KnownBits.h:111
static LLVM_ABI KnownBits abdu(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for abdu(LHS, RHS).
bool operator==(const KnownBits &Other) const
Definition KnownBits.h:548
KnownBits extractBits(unsigned NumBits, unsigned BitPosition) const
Return a subset of the known bits from [bitPosition,bitPosition+numBits).
Definition KnownBits.h:238
unsigned countMaxActiveBits() const
Returns the maximum number of bits needed to represent all possible unsigned values with these known ...
Definition KnownBits.h:309
static LLVM_ABI KnownBits avgFloorU(const KnownBits &LHS, const KnownBits &RHS)
Compute knownbits resulting from APIntOps::avgFloorU.
KnownBits intersectWith(const KnownBits &RHS) const
Returns KnownBits information that is known to be true for both this and RHS.
Definition KnownBits.h:324
KnownBits sext(unsigned BitWidth) const
Return known bits for a sign extension of the value we're tracking.
Definition KnownBits.h:183
unsigned countMinTrailingOnes() const
Returns the minimum number of trailing one bits.
Definition KnownBits.h:258
static LLVM_ABI KnownBits computeForSubBorrow(const KnownBits &LHS, KnownBits RHS, const KnownBits &Borrow)
Compute known bits results from subtracting RHS from LHS with 1-bit Borrow.
static KnownBits add(const KnownBits &LHS, const KnownBits &RHS, bool NSW=false, bool NUW=false)
Compute knownbits resulting from addition of LHS and RHS.
Definition KnownBits.h:360
KnownBits & operator<<=(unsigned ShAmt)
Shift known bits left by ShAmt. Shift in bits are unknown.
Definition KnownBits.h:511
KnownBits zextOrTrunc(unsigned BitWidth) const
Return known bits for a zero extension or truncation of the value we're tracking.
Definition KnownBits.h:199
unsigned countMinLeadingZeros() const
Returns the minimum number of leading zero bits.
Definition KnownBits.h:261
APInt getMaxValue() const
Return the maximal unsigned value possible given these KnownBits.
Definition KnownBits.h:148
KnownBits()=default
static LLVM_ABI KnownBits abds(KnownBits LHS, KnownBits RHS)
Compute known bits for abds(LHS, RHS).
static LLVM_ABI KnownBits smin(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for smin(LHS, RHS).
LLVM_ABI KnownBits & operator&=(const KnownBits &RHS)
Update known bits based on ANDing with RHS.
static LLVM_ABI KnownBits mulhs(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits from sign-extended multiply-hi.
static LLVM_ABI KnownBits srem(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for srem(LHS, RHS).
static LLVM_ABI std::optional< bool > ugt(const KnownBits &LHS, const KnownBits &RHS)
Determine if these known bits always give the same ICMP_UGT result.
static LLVM_ABI KnownBits udiv(const KnownBits &LHS, const KnownBits &RHS, bool Exact=false)
Compute known bits for udiv(LHS, RHS).
static LLVM_ABI std::optional< bool > slt(const KnownBits &LHS, const KnownBits &RHS)
Determine if these known bits always give the same ICMP_SLT result.
APInt getMinValue() const
Return the minimal unsigned value possible given these KnownBits.
Definition KnownBits.h:132
static LLVM_ABI KnownBits computeForAddSub(bool Add, bool NSW, bool NUW, const KnownBits &LHS, const KnownBits &RHS)
Compute known bits resulting from adding LHS and RHS.
Definition KnownBits.cpp:60
bool isStrictlyPositive() const
Returns true if this value is known to be positive.
Definition KnownBits.h:114
static LLVM_ABI KnownBits sdiv(const KnownBits &LHS, const KnownBits &RHS, bool Exact=false)
Compute known bits for sdiv(LHS, RHS).
static LLVM_ABI std::optional< bool > ult(const KnownBits &LHS, const KnownBits &RHS)
Determine if these known bits always give the same ICMP_ULT result.
static LLVM_ABI KnownBits avgFloorS(const KnownBits &LHS, const KnownBits &RHS)
Compute knownbits resulting from APIntOps::avgFloorS.
bool operator!=(const KnownBits &Other) const
Definition KnownBits.h:552
static LLVM_ABI std::optional< bool > ule(const KnownBits &LHS, const KnownBits &RHS)
Determine if these known bits always give the same ICMP_ULE result.
static bool haveNoCommonBitsSet(const KnownBits &LHS, const KnownBits &RHS)
Return true if LHS and RHS have no common bits set.
Definition KnownBits.h:339
bool isNegative() const
Returns true if this value is known to be negative.
Definition KnownBits.h:105
LLVM_ABI KnownBits truncSSat(unsigned BitWidth) const
Truncate with signed saturation (signed input -> signed output)
static LLVM_ABI KnownBits computeForAddCarry(const KnownBits &LHS, const KnownBits &RHS, const KnownBits &Carry)
Compute known bits resulting from adding LHS, RHS and a 1-bit Carry.
Definition KnownBits.cpp:53
static KnownBits sub(const KnownBits &LHS, const KnownBits &RHS, bool NSW=false, bool NUW=false)
Compute knownbits resulting from subtraction of LHS and RHS.
Definition KnownBits.h:366
unsigned countMaxLeadingZeros() const
Returns the maximum number of leading zero bits possible.
Definition KnownBits.h:293
void setAllOnes()
Make all bits known to be one and discard any previous information.
Definition KnownBits.h:92
void insertBits(const KnownBits &SubBits, unsigned BitPosition)
Insert the bits from a smaller known bits starting at bitPosition.
Definition KnownBits.h:232
static LLVM_ABI KnownBits avgCeilU(const KnownBits &LHS, const KnownBits &RHS)
Compute knownbits resulting from APIntOps::avgCeilU.
static LLVM_ABI KnownBits uadd_sat(const KnownBits &LHS, const KnownBits &RHS)
Compute knownbits resulting from llvm.uadd.sat(LHS, RHS)
static LLVM_ABI KnownBits mul(const KnownBits &LHS, const KnownBits &RHS, bool NoUndefSelfMultiply=false)
Compute known bits resulting from multiplying LHS and RHS.
KnownBits anyext(unsigned BitWidth) const
Return known bits for an "any" extension of the value we're tracking, where we don't know anything ab...
Definition KnownBits.h:170
LLVM_ABI KnownBits abs(bool IntMinIsPoison=false) const
Compute known bits for the absolute value.
static LLVM_ABI std::optional< bool > sle(const KnownBits &LHS, const KnownBits &RHS)
Determine if these known bits always give the same ICMP_SLE result.
unsigned countMaxSignificantBits() const
Returns the maximum number of bits needed to represent all possible signed values with these known bi...
Definition KnownBits.h:282
static LLVM_ABI std::optional< bool > sgt(const KnownBits &LHS, const KnownBits &RHS)
Determine if these known bits always give the same ICMP_SGT result.
unsigned countMinPopulation() const
Returns the number of bits known to be one.
Definition KnownBits.h:299
LLVM_ABI KnownBits truncUSat(unsigned BitWidth) const
Truncate with unsigned saturation (unsigned input -> unsigned output)
static LLVM_ABI std::optional< bool > uge(const KnownBits &LHS, const KnownBits &RHS)
Determine if these known bits always give the same ICMP_UGE result.
KnownBits(unsigned BitWidth)
Create a known bits object of BitWidth bits initialized to unknown.
Definition KnownBits.h:41
unsigned countMaxLeadingOnes() const
Returns the maximum number of leading one bits possible.
Definition KnownBits.h:296
APInt getSignedMinValue() const
Return the minimal signed value possible given these KnownBits.
Definition KnownBits.h:138
LLVM_ABI KnownBits & operator^=(const KnownBits &RHS)
Update known bits based on XORing with RHS.
static LLVM_ABI KnownBits shl(const KnownBits &LHS, const KnownBits &RHS, bool NUW=false, bool NSW=false, bool ShAmtNonZero=false)
Compute known bits for shl(LHS, RHS).
static LLVM_ABI KnownBits umin(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for umin(LHS, RHS).
LLVM_ABI KnownBits truncSSatU(unsigned BitWidth) const
Truncate with signed saturation to unsigned (signed input -> unsigned output)
bool isNonPositive() const
Returns true if this value is known to be non-positive.
Definition KnownBits.h:119
bool isAllOnes() const
Returns true if value is all one bits.
Definition KnownBits.h:83
KnownBits sextOrTrunc(unsigned BitWidth) const
Return known bits for a sign extension or truncation of the value we're tracking.
Definition KnownBits.h:209
static LLVM_ABI KnownBits avgCeilS(const KnownBits &LHS, const KnownBits &RHS)
Compute knownbits resulting from APIntOps::avgCeilS.
const APInt & getConstant() const
Returns the value when all bits have a known value.
Definition KnownBits.h:60