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