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 if the value is known even (the low bit is 0).
164 bool isEven() const { return Zero[0]; }
165
166 /// Return known bits for a truncation of the value we're tracking.
167 KnownBits trunc(unsigned BitWidth) const {
168 return KnownBits(Zero.trunc(BitWidth), One.trunc(BitWidth));
169 }
170
171 /// Return known bits for an "any" extension of the value we're tracking,
172 /// where we don't know anything about the extended bits.
173 KnownBits anyext(unsigned BitWidth) const {
174 return KnownBits(Zero.zext(BitWidth), One.zext(BitWidth));
175 }
176
177 /// Return known bits for a zero extension of the value we're tracking.
178 KnownBits zext(unsigned BitWidth) const {
179 unsigned OldBitWidth = getBitWidth();
180 APInt NewZero = Zero.zext(BitWidth);
181 NewZero.setBitsFrom(OldBitWidth);
182 return KnownBits(NewZero, One.zext(BitWidth));
183 }
184
185 /// Return known bits for a sign extension of the value we're tracking.
186 KnownBits sext(unsigned BitWidth) const {
187 return KnownBits(Zero.sext(BitWidth), One.sext(BitWidth));
188 }
189
190 /// Return known bits for an "any" extension or truncation of the value we're
191 /// tracking.
192 KnownBits anyextOrTrunc(unsigned BitWidth) const {
193 if (BitWidth > getBitWidth())
194 return anyext(BitWidth);
195 if (BitWidth < getBitWidth())
196 return trunc(BitWidth);
197 return *this;
198 }
199
200 /// Return known bits for a zero extension or truncation of the value we're
201 /// tracking.
202 KnownBits zextOrTrunc(unsigned BitWidth) const {
203 if (BitWidth > getBitWidth())
204 return zext(BitWidth);
205 if (BitWidth < getBitWidth())
206 return trunc(BitWidth);
207 return *this;
208 }
209
210 /// Return known bits for a sign extension or truncation of the value we're
211 /// tracking.
212 KnownBits sextOrTrunc(unsigned BitWidth) const {
213 if (BitWidth > getBitWidth())
214 return sext(BitWidth);
215 if (BitWidth < getBitWidth())
216 return trunc(BitWidth);
217 return *this;
218 }
219
220 /// Truncate with signed saturation (signed input -> signed output)
221 LLVM_ABI KnownBits truncSSat(unsigned BitWidth) const;
222
223 /// Truncate with signed saturation to unsigned (signed input -> unsigned
224 /// output)
225 LLVM_ABI KnownBits truncSSatU(unsigned BitWidth) const;
226
227 /// Truncate with unsigned saturation (unsigned input -> unsigned output)
228 LLVM_ABI KnownBits truncUSat(unsigned BitWidth) const;
229
230 /// Return known bits for a in-register sign extension of the value we're
231 /// tracking.
232 LLVM_ABI KnownBits sextInReg(unsigned SrcBitWidth) const;
233
234 /// Insert the bits from a smaller known bits starting at bitPosition.
235 void insertBits(const KnownBits &SubBits, unsigned BitPosition) {
236 Zero.insertBits(SubBits.Zero, BitPosition);
237 One.insertBits(SubBits.One, BitPosition);
238 }
239
240 /// Return a subset of the known bits from [bitPosition,bitPosition+numBits).
241 KnownBits extractBits(unsigned NumBits, unsigned BitPosition) const {
242 return KnownBits(Zero.extractBits(NumBits, BitPosition),
243 One.extractBits(NumBits, BitPosition));
244 }
245
246 /// Concatenate the bits from \p Lo onto the bottom of *this. This is
247 /// equivalent to:
248 /// (this->zext(NewWidth) << Lo.getBitWidth()) | Lo.zext(NewWidth)
249 KnownBits concat(const KnownBits &Lo) const {
250 return KnownBits(Zero.concat(Lo.Zero), One.concat(Lo.One));
251 }
252
253 /// Return KnownBits based on this, but updated given that the underlying
254 /// value is known to be greater than or equal to Val.
255 LLVM_ABI KnownBits makeGE(const APInt &Val) const;
256
257 /// Returns the minimum number of trailing zero bits.
258 unsigned countMinTrailingZeros() const { return Zero.countr_one(); }
259
260 /// Returns the minimum number of trailing one bits.
261 unsigned countMinTrailingOnes() const { return One.countr_one(); }
262
263 /// Returns the minimum number of leading zero bits.
264 unsigned countMinLeadingZeros() const { return Zero.countl_one(); }
265
266 /// Returns the minimum number of leading one bits.
267 unsigned countMinLeadingOnes() const { return One.countl_one(); }
268
269 /// Returns the number of times the sign bit is replicated into the other
270 /// bits.
271 unsigned countMinSignBits() const {
272 if (isNonNegative())
273 return countMinLeadingZeros();
274 if (isNegative())
275 return countMinLeadingOnes();
276 // Every value has at least 1 sign bit.
277 return 1;
278 }
279
280 /// Returns the maximum number of bits needed to represent all possible
281 /// signed values with these known bits. This is the inverse of the minimum
282 /// number of known sign bits. Examples for bitwidth 5:
283 /// 110?? --> 4
284 /// 0000? --> 2
285 unsigned countMaxSignificantBits() const {
286 return getBitWidth() - countMinSignBits() + 1;
287 }
288
289 /// Returns the maximum number of trailing zero bits possible.
290 unsigned countMaxTrailingZeros() const { return One.countr_zero(); }
291
292 /// Returns the maximum number of trailing one bits possible.
293 unsigned countMaxTrailingOnes() const { return Zero.countr_zero(); }
294
295 /// Returns the maximum number of leading zero bits possible.
296 unsigned countMaxLeadingZeros() const { return One.countl_zero(); }
297
298 /// Returns the maximum number of leading one bits possible.
299 unsigned countMaxLeadingOnes() const { return Zero.countl_zero(); }
300
301 /// Returns the number of bits known to be one.
302 unsigned countMinPopulation() const { return One.popcount(); }
303
304 /// Returns the maximum number of bits that could be one.
305 unsigned countMaxPopulation() const {
306 return getBitWidth() - Zero.popcount();
307 }
308
309 /// Returns the maximum number of bits needed to represent all possible
310 /// unsigned values with these known bits. This is the inverse of the
311 /// minimum number of leading zeros.
312 unsigned countMaxActiveBits() const {
314 }
315
316 /// Create known bits from a known constant.
317 static KnownBits makeConstant(const APInt &C) {
318 return KnownBits(~C, C);
319 }
320
321 /// Returns KnownBits information that is known to be true for both this and
322 /// RHS.
323 ///
324 /// When an operation is known to return one of its operands, this can be used
325 /// to combine information about the known bits of the operands to get the
326 /// information that must be true about the result.
327 KnownBits intersectWith(const KnownBits &RHS) const {
328 return KnownBits(Zero & RHS.Zero, One & RHS.One);
329 }
330
331 /// Returns KnownBits information that is known to be true for either this or
332 /// RHS or both.
333 ///
334 /// This can be used to combine different sources of information about the
335 /// known bits of a single value, e.g. information about the low bits and the
336 /// high bits of the result of a multiplication.
337 KnownBits unionWith(const KnownBits &RHS) const {
338 return KnownBits(Zero | RHS.Zero, One | RHS.One);
339 }
340
341 /// Return true if LHS and RHS have no common bits set.
342 static bool haveNoCommonBitsSet(const KnownBits &LHS, const KnownBits &RHS) {
343 return (LHS.Zero | RHS.Zero).isAllOnes();
344 }
345
346 /// Compute known bits resulting from adding LHS, RHS and a 1-bit Carry.
348 const KnownBits &RHS,
349 const KnownBits &Carry);
350
351 /// Compute known bits resulting from adding LHS and RHS.
352 LLVM_ABI static KnownBits computeForAddSub(bool Add, bool NSW, bool NUW,
353 const KnownBits &LHS,
354 const KnownBits &RHS);
355
356 /// Compute known bits results from subtracting RHS from LHS with 1-bit
357 /// Borrow.
360 const KnownBits &Borrow);
361
362 /// Compute knownbits resulting from addition of LHS and RHS.
363 static KnownBits add(const KnownBits &LHS, const KnownBits &RHS,
364 bool NSW = false, bool NUW = false) {
365 return computeForAddSub(/*Add=*/true, NSW, NUW, LHS, RHS);
366 }
367
368 /// Compute knownbits resulting from subtraction of LHS and RHS.
369 static KnownBits sub(const KnownBits &LHS, const KnownBits &RHS,
370 bool NSW = false, bool NUW = false) {
371 return computeForAddSub(/*Add=*/false, NSW, NUW, LHS, RHS);
372 }
373
374 /// Compute knownbits resulting from llvm.sadd.sat(LHS, RHS)
376 const KnownBits &RHS);
377
378 /// Compute knownbits resulting from llvm.uadd.sat(LHS, RHS)
380 const KnownBits &RHS);
381
382 /// Compute knownbits resulting from llvm.ssub.sat(LHS, RHS)
384 const KnownBits &RHS);
385
386 /// Compute knownbits resulting from llvm.usub.sat(LHS, RHS)
388 const KnownBits &RHS);
389
390 /// Compute knownbits resulting from APIntOps::avgFloorS
392 const KnownBits &RHS);
393
394 /// Compute knownbits resulting from APIntOps::avgFloorU
396 const KnownBits &RHS);
397
398 /// Compute knownbits resulting from APIntOps::avgCeilS
400 const KnownBits &RHS);
401
402 /// Compute knownbits resulting from APIntOps::avgCeilU
404 const KnownBits &RHS);
405
406 /// Compute known bits resulting from multiplying LHS and RHS.
407 LLVM_ABI static KnownBits mul(const KnownBits &LHS, const KnownBits &RHS,
408 bool NoUndefSelfMultiply = false);
409
410 /// Compute known bits from sign-extended multiply-hi.
411 LLVM_ABI static KnownBits mulhs(const KnownBits &LHS, const KnownBits &RHS);
412
413 /// Compute known bits from zero-extended multiply-hi.
414 LLVM_ABI static KnownBits mulhu(const KnownBits &LHS, const KnownBits &RHS);
415
416 /// Compute known bits for sdiv(LHS, RHS).
417 LLVM_ABI static KnownBits sdiv(const KnownBits &LHS, const KnownBits &RHS,
418 bool Exact = false);
419
420 /// Compute known bits for udiv(LHS, RHS).
421 LLVM_ABI static KnownBits udiv(const KnownBits &LHS, const KnownBits &RHS,
422 bool Exact = false);
423
424 /// Compute known bits for urem(LHS, RHS).
425 LLVM_ABI static KnownBits urem(const KnownBits &LHS, const KnownBits &RHS);
426
427 /// Compute known bits for srem(LHS, RHS).
428 LLVM_ABI static KnownBits srem(const KnownBits &LHS, const KnownBits &RHS);
429
430 /// Compute known bits for umax(LHS, RHS).
431 LLVM_ABI static KnownBits umax(const KnownBits &LHS, const KnownBits &RHS);
432
433 /// Compute known bits for umin(LHS, RHS).
434 LLVM_ABI static KnownBits umin(const KnownBits &LHS, const KnownBits &RHS);
435
436 /// Compute known bits for smax(LHS, RHS).
437 LLVM_ABI static KnownBits smax(const KnownBits &LHS, const KnownBits &RHS);
438
439 /// Compute known bits for smin(LHS, RHS).
440 LLVM_ABI static KnownBits smin(const KnownBits &LHS, const KnownBits &RHS);
441
442 /// Compute known bits for abdu(LHS, RHS).
443 LLVM_ABI static KnownBits abdu(const KnownBits &LHS, const KnownBits &RHS);
444
445 /// Compute known bits for abds(LHS, RHS).
447
448 /// Compute known bits for shl(LHS, RHS).
449 /// NOTE: RHS (shift amount) bitwidth doesn't need to be the same as LHS.
450 LLVM_ABI static KnownBits shl(const KnownBits &LHS, const KnownBits &RHS,
451 bool NUW = false, bool NSW = false,
452 bool ShAmtNonZero = false);
453
454 /// Compute known bits for lshr(LHS, RHS).
455 /// NOTE: RHS (shift amount) bitwidth doesn't need to be the same as LHS.
456 LLVM_ABI static KnownBits lshr(const KnownBits &LHS, const KnownBits &RHS,
457 bool ShAmtNonZero = false, bool Exact = false);
458
459 /// Compute known bits for ashr(LHS, RHS).
460 /// NOTE: RHS (shift amount) bitwidth doesn't need to be the same as LHS.
461 LLVM_ABI static KnownBits ashr(const KnownBits &LHS, const KnownBits &RHS,
462 bool ShAmtNonZero = false, bool Exact = false);
463
464 /// Compute known bits for clmul(LHS, RHS).
465 LLVM_ABI static KnownBits clmul(const KnownBits &LHS, const KnownBits &RHS);
466
467 /// Determine if these known bits always give the same ICMP_EQ result.
468 LLVM_ABI static std::optional<bool> eq(const KnownBits &LHS,
469 const KnownBits &RHS);
470
471 /// Determine if these known bits always give the same ICMP_NE result.
472 LLVM_ABI static std::optional<bool> ne(const KnownBits &LHS,
473 const KnownBits &RHS);
474
475 /// Determine if these known bits always give the same ICMP_UGT result.
476 LLVM_ABI static std::optional<bool> ugt(const KnownBits &LHS,
477 const KnownBits &RHS);
478
479 /// Determine if these known bits always give the same ICMP_UGE result.
480 LLVM_ABI static std::optional<bool> uge(const KnownBits &LHS,
481 const KnownBits &RHS);
482
483 /// Determine if these known bits always give the same ICMP_ULT result.
484 LLVM_ABI static std::optional<bool> ult(const KnownBits &LHS,
485 const KnownBits &RHS);
486
487 /// Determine if these known bits always give the same ICMP_ULE result.
488 LLVM_ABI static std::optional<bool> ule(const KnownBits &LHS,
489 const KnownBits &RHS);
490
491 /// Determine if these known bits always give the same ICMP_SGT result.
492 LLVM_ABI static std::optional<bool> sgt(const KnownBits &LHS,
493 const KnownBits &RHS);
494
495 /// Determine if these known bits always give the same ICMP_SGE result.
496 LLVM_ABI static std::optional<bool> sge(const KnownBits &LHS,
497 const KnownBits &RHS);
498
499 /// Determine if these known bits always give the same ICMP_SLT result.
500 LLVM_ABI static std::optional<bool> slt(const KnownBits &LHS,
501 const KnownBits &RHS);
502
503 /// Determine if these known bits always give the same ICMP_SLE result.
504 LLVM_ABI static std::optional<bool> sle(const KnownBits &LHS,
505 const KnownBits &RHS);
506
507 /// Update known bits based on ANDing with RHS.
509
510 /// Update known bits based on ORing with RHS.
512
513 /// Update known bits based on XORing with RHS.
515
516 /// Shift known bits left by ShAmt. Shift in bits are unknown.
517 KnownBits &operator<<=(unsigned ShAmt) {
518 Zero <<= ShAmt;
519 One <<= ShAmt;
520 return *this;
521 }
522
523 /// Shift known bits right by ShAmt. Shifted in bits are unknown.
524 KnownBits &operator>>=(unsigned ShAmt) {
525 Zero.lshrInPlace(ShAmt);
526 One.lshrInPlace(ShAmt);
527 return *this;
528 }
529
530 /// Compute known bits for the absolute value.
531 LLVM_ABI KnownBits abs(bool IntMinIsPoison = false) const;
532
533 /// Compute known bits for horizontal add for a vector with NumElts
534 /// elements, where each element has the known bits represented by this
535 /// object.
536 LLVM_ABI KnownBits reduceAdd(unsigned NumElts) const;
537
538 KnownBits byteSwap() const {
539 return KnownBits(Zero.byteSwap(), One.byteSwap());
540 }
541
542 KnownBits reverseBits() const {
543 return KnownBits(Zero.reverseBits(), One.reverseBits());
544 }
545
546 /// Compute known bits for X & -X, which has only the lowest bit set of X set.
547 /// The name comes from the X86 BMI instruction
548 LLVM_ABI KnownBits blsi() const;
549
550 /// Compute known bits for X ^ (X - 1), which has all bits up to and including
551 /// the lowest set bit of X set. The name comes from the X86 BMI instruction.
552 LLVM_ABI KnownBits blsmsk() const;
553
554 bool operator==(const KnownBits &Other) const {
555 return Zero == Other.Zero && One == Other.One;
556 }
557
558 bool operator!=(const KnownBits &Other) const { return !(*this == Other); }
559
560 LLVM_ABI void print(raw_ostream &OS) const;
561
562#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
563 LLVM_DUMP_METHOD void dump() const;
564#endif
565
566private:
567 // Internal helper for getting the initial KnownBits for an `srem` or `urem`
568 // operation with the low-bits set.
569 static KnownBits remGetLowBits(const KnownBits &LHS, const KnownBits &RHS);
570};
571
573 LHS &= RHS;
574 return LHS;
575}
576
578 RHS &= LHS;
579 return std::move(RHS);
580}
581
583 LHS |= RHS;
584 return LHS;
585}
586
588 RHS |= LHS;
589 return std::move(RHS);
590}
591
593 LHS ^= RHS;
594 return LHS;
595}
596
598 RHS ^= LHS;
599 return std::move(RHS);
600}
601
602inline raw_ostream &operator<<(raw_ostream &OS, const KnownBits &Known) {
603 Known.print(OS);
604 return OS;
605}
606
607} // end namespace llvm
608
609#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:1400
void setSignBit()
Set the sign bit to 1.
Definition APInt.h:1355
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:2138
APInt operator^(APInt a, const APInt &b)
Definition APInt.h:2178
@ 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:1917
APInt operator|(APInt a, const APInt &b)
Definition APInt.h:2158
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:317
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:293
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:192
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:271
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:267
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:258
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:290
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:524
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:167
KnownBits byteSwap() const
Definition KnownBits.h:538
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:305
void setAllZero()
Make all bits known to be zero and discard any previous information.
Definition KnownBits.h:86
KnownBits reverseBits() const
Definition KnownBits.h:542
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:249
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:178
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:337
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:554
bool isEven() const
Return if the value is known even (the low bit is 0).
Definition KnownBits.h:164
KnownBits extractBits(unsigned NumBits, unsigned BitPosition) const
Return a subset of the known bits from [bitPosition,bitPosition+numBits).
Definition KnownBits.h:241
unsigned countMaxActiveBits() const
Returns the maximum number of bits needed to represent all possible unsigned values with these known ...
Definition KnownBits.h:312
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:327
KnownBits sext(unsigned BitWidth) const
Return known bits for a sign extension of the value we're tracking.
Definition KnownBits.h:186
unsigned countMinTrailingOnes() const
Returns the minimum number of trailing one bits.
Definition KnownBits.h:261
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:363
KnownBits & operator<<=(unsigned ShAmt)
Shift known bits left by ShAmt. Shift in bits are unknown.
Definition KnownBits.h:517
KnownBits zextOrTrunc(unsigned BitWidth) const
Return known bits for a zero extension or truncation of the value we're tracking.
Definition KnownBits.h:202
unsigned countMinLeadingZeros() const
Returns the minimum number of leading zero bits.
Definition KnownBits.h:264
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:61
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:558
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:342
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:54
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:369
unsigned countMaxLeadingZeros() const
Returns the maximum number of leading zero bits possible.
Definition KnownBits.h:296
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:235
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:173
static LLVM_ABI KnownBits clmul(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for clmul(LHS, RHS).
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:285
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:302
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:299
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:212
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