LLVM 22.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 /// Return known bits for a in-register sign extension of the value we're
218 /// tracking.
219 LLVM_ABI KnownBits sextInReg(unsigned SrcBitWidth) const;
220
221 /// Insert the bits from a smaller known bits starting at bitPosition.
222 void insertBits(const KnownBits &SubBits, unsigned BitPosition) {
223 Zero.insertBits(SubBits.Zero, BitPosition);
224 One.insertBits(SubBits.One, BitPosition);
225 }
226
227 /// Return a subset of the known bits from [bitPosition,bitPosition+numBits).
228 KnownBits extractBits(unsigned NumBits, unsigned BitPosition) const {
229 return KnownBits(Zero.extractBits(NumBits, BitPosition),
230 One.extractBits(NumBits, BitPosition));
231 }
232
233 /// Concatenate the bits from \p Lo onto the bottom of *this. This is
234 /// equivalent to:
235 /// (this->zext(NewWidth) << Lo.getBitWidth()) | Lo.zext(NewWidth)
236 KnownBits concat(const KnownBits &Lo) const {
237 return KnownBits(Zero.concat(Lo.Zero), One.concat(Lo.One));
238 }
239
240 /// Return KnownBits based on this, but updated given that the underlying
241 /// value is known to be greater than or equal to Val.
242 LLVM_ABI KnownBits makeGE(const APInt &Val) const;
243
244 /// Returns the minimum number of trailing zero bits.
245 unsigned countMinTrailingZeros() const { return Zero.countr_one(); }
246
247 /// Returns the minimum number of trailing one bits.
248 unsigned countMinTrailingOnes() const { return One.countr_one(); }
249
250 /// Returns the minimum number of leading zero bits.
251 unsigned countMinLeadingZeros() const { return Zero.countl_one(); }
252
253 /// Returns the minimum number of leading one bits.
254 unsigned countMinLeadingOnes() const { return One.countl_one(); }
255
256 /// Returns the number of times the sign bit is replicated into the other
257 /// bits.
258 unsigned countMinSignBits() const {
259 if (isNonNegative())
260 return countMinLeadingZeros();
261 if (isNegative())
262 return countMinLeadingOnes();
263 // Every value has at least 1 sign bit.
264 return 1;
265 }
266
267 /// Returns the maximum number of bits needed to represent all possible
268 /// signed values with these known bits. This is the inverse of the minimum
269 /// number of known sign bits. Examples for bitwidth 5:
270 /// 110?? --> 4
271 /// 0000? --> 2
272 unsigned countMaxSignificantBits() const {
273 return getBitWidth() - countMinSignBits() + 1;
274 }
275
276 /// Returns the maximum number of trailing zero bits possible.
277 unsigned countMaxTrailingZeros() const { return One.countr_zero(); }
278
279 /// Returns the maximum number of trailing one bits possible.
280 unsigned countMaxTrailingOnes() const { return Zero.countr_zero(); }
281
282 /// Returns the maximum number of leading zero bits possible.
283 unsigned countMaxLeadingZeros() const { return One.countl_zero(); }
284
285 /// Returns the maximum number of leading one bits possible.
286 unsigned countMaxLeadingOnes() const { return Zero.countl_zero(); }
287
288 /// Returns the number of bits known to be one.
289 unsigned countMinPopulation() const { return One.popcount(); }
290
291 /// Returns the maximum number of bits that could be one.
292 unsigned countMaxPopulation() const {
293 return getBitWidth() - Zero.popcount();
294 }
295
296 /// Returns the maximum number of bits needed to represent all possible
297 /// unsigned values with these known bits. This is the inverse of the
298 /// minimum number of leading zeros.
299 unsigned countMaxActiveBits() const {
301 }
302
303 /// Create known bits from a known constant.
304 static KnownBits makeConstant(const APInt &C) {
305 return KnownBits(~C, C);
306 }
307
308 /// Returns KnownBits information that is known to be true for both this and
309 /// RHS.
310 ///
311 /// When an operation is known to return one of its operands, this can be used
312 /// to combine information about the known bits of the operands to get the
313 /// information that must be true about the result.
314 KnownBits intersectWith(const KnownBits &RHS) const {
315 return KnownBits(Zero & RHS.Zero, One & RHS.One);
316 }
317
318 /// Returns KnownBits information that is known to be true for either this or
319 /// RHS or both.
320 ///
321 /// This can be used to combine different sources of information about the
322 /// known bits of a single value, e.g. information about the low bits and the
323 /// high bits of the result of a multiplication.
324 KnownBits unionWith(const KnownBits &RHS) const {
325 return KnownBits(Zero | RHS.Zero, One | RHS.One);
326 }
327
328 /// Return true if LHS and RHS have no common bits set.
329 static bool haveNoCommonBitsSet(const KnownBits &LHS, const KnownBits &RHS) {
330 return (LHS.Zero | RHS.Zero).isAllOnes();
331 }
332
333 /// Compute known bits resulting from adding LHS, RHS and a 1-bit Carry.
335 const KnownBits &RHS,
336 const KnownBits &Carry);
337
338 /// Compute known bits resulting from adding LHS and RHS.
339 LLVM_ABI static KnownBits computeForAddSub(bool Add, bool NSW, bool NUW,
340 const KnownBits &LHS,
341 const KnownBits &RHS);
342
343 /// Compute known bits results from subtracting RHS from LHS with 1-bit
344 /// Borrow.
347 const KnownBits &Borrow);
348
349 /// Compute knownbits resulting from addition of LHS and RHS.
350 static KnownBits add(const KnownBits &LHS, const KnownBits &RHS,
351 bool NSW = false, bool NUW = false) {
352 return computeForAddSub(/*Add=*/true, NSW, NUW, LHS, RHS);
353 }
354
355 /// Compute knownbits resulting from subtraction of LHS and RHS.
356 static KnownBits sub(const KnownBits &LHS, const KnownBits &RHS,
357 bool NSW = false, bool NUW = false) {
358 return computeForAddSub(/*Add=*/false, NSW, NUW, LHS, RHS);
359 }
360
361 /// Compute knownbits resulting from llvm.sadd.sat(LHS, RHS)
363 const KnownBits &RHS);
364
365 /// Compute knownbits resulting from llvm.uadd.sat(LHS, RHS)
367 const KnownBits &RHS);
368
369 /// Compute knownbits resulting from llvm.ssub.sat(LHS, RHS)
371 const KnownBits &RHS);
372
373 /// Compute knownbits resulting from llvm.usub.sat(LHS, RHS)
375 const KnownBits &RHS);
376
377 /// Compute knownbits resulting from APIntOps::avgFloorS
379 const KnownBits &RHS);
380
381 /// Compute knownbits resulting from APIntOps::avgFloorU
383 const KnownBits &RHS);
384
385 /// Compute knownbits resulting from APIntOps::avgCeilS
387 const KnownBits &RHS);
388
389 /// Compute knownbits resulting from APIntOps::avgCeilU
391 const KnownBits &RHS);
392
393 /// Compute known bits resulting from multiplying LHS and RHS.
394 LLVM_ABI static KnownBits mul(const KnownBits &LHS, const KnownBits &RHS,
395 bool NoUndefSelfMultiply = false);
396
397 /// Compute known bits from sign-extended multiply-hi.
398 LLVM_ABI static KnownBits mulhs(const KnownBits &LHS, const KnownBits &RHS);
399
400 /// Compute known bits from zero-extended multiply-hi.
401 LLVM_ABI static KnownBits mulhu(const KnownBits &LHS, const KnownBits &RHS);
402
403 /// Compute known bits for sdiv(LHS, RHS).
404 LLVM_ABI static KnownBits sdiv(const KnownBits &LHS, const KnownBits &RHS,
405 bool Exact = false);
406
407 /// Compute known bits for udiv(LHS, RHS).
408 LLVM_ABI static KnownBits udiv(const KnownBits &LHS, const KnownBits &RHS,
409 bool Exact = false);
410
411 /// Compute known bits for urem(LHS, RHS).
412 LLVM_ABI static KnownBits urem(const KnownBits &LHS, const KnownBits &RHS);
413
414 /// Compute known bits for srem(LHS, RHS).
415 LLVM_ABI static KnownBits srem(const KnownBits &LHS, const KnownBits &RHS);
416
417 /// Compute known bits for umax(LHS, RHS).
418 LLVM_ABI static KnownBits umax(const KnownBits &LHS, const KnownBits &RHS);
419
420 /// Compute known bits for umin(LHS, RHS).
421 LLVM_ABI static KnownBits umin(const KnownBits &LHS, const KnownBits &RHS);
422
423 /// Compute known bits for smax(LHS, RHS).
424 LLVM_ABI static KnownBits smax(const KnownBits &LHS, const KnownBits &RHS);
425
426 /// Compute known bits for smin(LHS, RHS).
427 LLVM_ABI static KnownBits smin(const KnownBits &LHS, const KnownBits &RHS);
428
429 /// Compute known bits for abdu(LHS, RHS).
430 LLVM_ABI static KnownBits abdu(const KnownBits &LHS, const KnownBits &RHS);
431
432 /// Compute known bits for abds(LHS, RHS).
434
435 /// Compute known bits for shl(LHS, RHS).
436 /// NOTE: RHS (shift amount) bitwidth doesn't need to be the same as LHS.
437 LLVM_ABI static KnownBits shl(const KnownBits &LHS, const KnownBits &RHS,
438 bool NUW = false, bool NSW = false,
439 bool ShAmtNonZero = false);
440
441 /// Compute known bits for lshr(LHS, RHS).
442 /// NOTE: RHS (shift amount) bitwidth doesn't need to be the same as LHS.
443 LLVM_ABI static KnownBits lshr(const KnownBits &LHS, const KnownBits &RHS,
444 bool ShAmtNonZero = false, bool Exact = false);
445
446 /// Compute known bits for ashr(LHS, RHS).
447 /// NOTE: RHS (shift amount) bitwidth doesn't need to be the same as LHS.
448 LLVM_ABI static KnownBits ashr(const KnownBits &LHS, const KnownBits &RHS,
449 bool ShAmtNonZero = false, bool Exact = false);
450
451 /// Determine if these known bits always give the same ICMP_EQ result.
452 LLVM_ABI static std::optional<bool> eq(const KnownBits &LHS,
453 const KnownBits &RHS);
454
455 /// Determine if these known bits always give the same ICMP_NE result.
456 LLVM_ABI static std::optional<bool> ne(const KnownBits &LHS,
457 const KnownBits &RHS);
458
459 /// Determine if these known bits always give the same ICMP_UGT result.
460 LLVM_ABI static std::optional<bool> ugt(const KnownBits &LHS,
461 const KnownBits &RHS);
462
463 /// Determine if these known bits always give the same ICMP_UGE result.
464 LLVM_ABI static std::optional<bool> uge(const KnownBits &LHS,
465 const KnownBits &RHS);
466
467 /// Determine if these known bits always give the same ICMP_ULT result.
468 LLVM_ABI static std::optional<bool> ult(const KnownBits &LHS,
469 const KnownBits &RHS);
470
471 /// Determine if these known bits always give the same ICMP_ULE result.
472 LLVM_ABI static std::optional<bool> ule(const KnownBits &LHS,
473 const KnownBits &RHS);
474
475 /// Determine if these known bits always give the same ICMP_SGT result.
476 LLVM_ABI static std::optional<bool> sgt(const KnownBits &LHS,
477 const KnownBits &RHS);
478
479 /// Determine if these known bits always give the same ICMP_SGE result.
480 LLVM_ABI static std::optional<bool> sge(const KnownBits &LHS,
481 const KnownBits &RHS);
482
483 /// Determine if these known bits always give the same ICMP_SLT result.
484 LLVM_ABI static std::optional<bool> slt(const KnownBits &LHS,
485 const KnownBits &RHS);
486
487 /// Determine if these known bits always give the same ICMP_SLE result.
488 LLVM_ABI static std::optional<bool> sle(const KnownBits &LHS,
489 const KnownBits &RHS);
490
491 /// Update known bits based on ANDing with RHS.
493
494 /// Update known bits based on ORing with RHS.
496
497 /// Update known bits based on XORing with RHS.
499
500 /// Shift known bits left by ShAmt. Shift in bits are unknown.
501 KnownBits &operator<<=(unsigned ShAmt) {
502 Zero <<= ShAmt;
503 One <<= ShAmt;
504 return *this;
505 }
506
507 /// Shift known bits right by ShAmt. Shifted in bits are unknown.
508 KnownBits &operator>>=(unsigned ShAmt) {
509 Zero.lshrInPlace(ShAmt);
510 One.lshrInPlace(ShAmt);
511 return *this;
512 }
513
514 /// Compute known bits for the absolute value.
515 LLVM_ABI KnownBits abs(bool IntMinIsPoison = false) const;
516
517 /// Compute known bits for horizontal add for a vector with NumElts
518 /// elements, where each element has the known bits represented by this
519 /// object.
520 LLVM_ABI KnownBits reduceAdd(unsigned NumElts) const;
521
522 KnownBits byteSwap() const {
523 return KnownBits(Zero.byteSwap(), One.byteSwap());
524 }
525
526 KnownBits reverseBits() const {
527 return KnownBits(Zero.reverseBits(), One.reverseBits());
528 }
529
530 /// Compute known bits for X & -X, which has only the lowest bit set of X set.
531 /// The name comes from the X86 BMI instruction
532 LLVM_ABI KnownBits blsi() const;
533
534 /// Compute known bits for X ^ (X - 1), which has all bits up to and including
535 /// the lowest set bit of X set. The name comes from the X86 BMI instruction.
536 LLVM_ABI KnownBits blsmsk() const;
537
538 bool operator==(const KnownBits &Other) const {
539 return Zero == Other.Zero && One == Other.One;
540 }
541
542 bool operator!=(const KnownBits &Other) const { return !(*this == Other); }
543
544 LLVM_ABI void print(raw_ostream &OS) const;
545
546#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
547 LLVM_DUMP_METHOD void dump() const;
548#endif
549
550private:
551 // Internal helper for getting the initial KnownBits for an `srem` or `urem`
552 // operation with the low-bits set.
553 static KnownBits remGetLowBits(const KnownBits &LHS, const KnownBits &RHS);
554};
555
557 LHS &= RHS;
558 return LHS;
559}
560
562 RHS &= LHS;
563 return std::move(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
586inline raw_ostream &operator<<(raw_ostream &OS, const KnownBits &Known) {
587 Known.print(OS);
588 return OS;
589}
590
591} // end namespace llvm
592
593#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:638
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:1386
void setSignBit()
Set the sign bit to 1.
Definition APInt.h:1341
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:2124
APInt operator^(APInt a, const APInt &b)
Definition APInt.h:2164
@ 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:2144
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:304
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:280
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:258
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:254
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:245
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:277
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:508
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:522
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:292
void setAllZero()
Make all bits known to be zero and discard any previous information.
Definition KnownBits.h:86
KnownBits reverseBits() const
Definition KnownBits.h:526
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:236
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:324
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:538
KnownBits extractBits(unsigned NumBits, unsigned BitPosition) const
Return a subset of the known bits from [bitPosition,bitPosition+numBits).
Definition KnownBits.h:228
unsigned countMaxActiveBits() const
Returns the maximum number of bits needed to represent all possible unsigned values with these known ...
Definition KnownBits.h:299
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:314
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:248
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:350
KnownBits & operator<<=(unsigned ShAmt)
Shift known bits left by ShAmt. Shift in bits are unknown.
Definition KnownBits.h:501
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:251
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:542
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:329
bool isNegative() const
Returns true if this value is known to be negative.
Definition KnownBits.h:105
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:356
unsigned countMaxLeadingZeros() const
Returns the maximum number of leading zero bits possible.
Definition KnownBits.h:283
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:222
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:272
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:289
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:286
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).
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