LLVM 19.0.0git
KnownBits.cpp
Go to the documentation of this file.
1//===-- KnownBits.cpp - Stores known zeros/ones ---------------------------===//
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
15#include "llvm/Support/Debug.h"
17#include <cassert>
18
19using namespace llvm;
20
22 const KnownBits &LHS, const KnownBits &RHS,
23 bool CarryZero, bool CarryOne) {
24 assert(!(CarryZero && CarryOne) &&
25 "Carry can't be zero and one at the same time");
26
27 APInt PossibleSumZero = LHS.getMaxValue() + RHS.getMaxValue() + !CarryZero;
28 APInt PossibleSumOne = LHS.getMinValue() + RHS.getMinValue() + CarryOne;
29
30 // Compute known bits of the carry.
31 APInt CarryKnownZero = ~(PossibleSumZero ^ LHS.Zero ^ RHS.Zero);
32 APInt CarryKnownOne = PossibleSumOne ^ LHS.One ^ RHS.One;
33
34 // Compute set of known bits (where all three relevant bits are known).
35 APInt LHSKnownUnion = LHS.Zero | LHS.One;
36 APInt RHSKnownUnion = RHS.Zero | RHS.One;
37 APInt CarryKnownUnion = std::move(CarryKnownZero) | CarryKnownOne;
38 APInt Known = std::move(LHSKnownUnion) & RHSKnownUnion & CarryKnownUnion;
39
40 assert((PossibleSumZero & Known) == (PossibleSumOne & Known) &&
41 "known bits of sum differ");
42
43 // Compute known bits of the result.
44 KnownBits KnownOut;
45 KnownOut.Zero = ~std::move(PossibleSumZero) & Known;
46 KnownOut.One = std::move(PossibleSumOne) & Known;
47 return KnownOut;
48}
49
51 const KnownBits &LHS, const KnownBits &RHS, const KnownBits &Carry) {
52 assert(Carry.getBitWidth() == 1 && "Carry must be 1-bit");
53 return ::computeForAddCarry(
54 LHS, RHS, Carry.Zero.getBoolValue(), Carry.One.getBoolValue());
55}
56
57KnownBits KnownBits::computeForAddSub(bool Add, bool NSW, bool NUW,
58 const KnownBits &LHS,
59 const KnownBits &RHS) {
60 unsigned BitWidth = LHS.getBitWidth();
61 KnownBits KnownOut(BitWidth);
62 // This can be a relatively expensive helper, so optimistically save some
63 // work.
64 if (LHS.isUnknown() && RHS.isUnknown())
65 return KnownOut;
66
67 if (!LHS.isUnknown() && !RHS.isUnknown()) {
68 if (Add) {
69 // Sum = LHS + RHS + 0
70 KnownOut = ::computeForAddCarry(LHS, RHS, /*CarryZero=*/true,
71 /*CarryOne=*/false);
72 } else {
73 // Sum = LHS + ~RHS + 1
74 KnownBits NotRHS = RHS;
75 std::swap(NotRHS.Zero, NotRHS.One);
76 KnownOut = ::computeForAddCarry(LHS, NotRHS, /*CarryZero=*/false,
77 /*CarryOne=*/true);
78 }
79 }
80
81 // Handle add/sub given nsw and/or nuw.
82 if (NUW) {
83 if (Add) {
84 // (add nuw X, Y)
85 APInt MinVal = LHS.getMinValue().uadd_sat(RHS.getMinValue());
86 // None of the adds can end up overflowing, so min consecutive highbits
87 // in minimum possible of X + Y must all remain set.
88 if (NSW) {
89 unsigned NumBits = MinVal.trunc(BitWidth - 1).countl_one();
90 // If we have NSW as well, we also know we can't overflow the signbit so
91 // can start counting from 1 bit back.
92 KnownOut.One.setBits(BitWidth - 1 - NumBits, BitWidth - 1);
93 }
94 KnownOut.One.setHighBits(MinVal.countl_one());
95 } else {
96 // (sub nuw X, Y)
97 APInt MaxVal = LHS.getMaxValue().usub_sat(RHS.getMinValue());
98 // None of the subs can overflow at any point, so any common high bits
99 // will subtract away and result in zeros.
100 if (NSW) {
101 // If we have NSW as well, we also know we can't overflow the signbit so
102 // can start counting from 1 bit back.
103 unsigned NumBits = MaxVal.trunc(BitWidth - 1).countl_zero();
104 KnownOut.Zero.setBits(BitWidth - 1 - NumBits, BitWidth - 1);
105 }
106 KnownOut.Zero.setHighBits(MaxVal.countl_zero());
107 }
108 }
109
110 if (NSW) {
111 APInt MinVal;
112 APInt MaxVal;
113 if (Add) {
114 // (add nsw X, Y)
115 MinVal = LHS.getSignedMinValue().sadd_sat(RHS.getSignedMinValue());
116 MaxVal = LHS.getSignedMaxValue().sadd_sat(RHS.getSignedMaxValue());
117 } else {
118 // (sub nsw X, Y)
119 MinVal = LHS.getSignedMinValue().ssub_sat(RHS.getSignedMaxValue());
120 MaxVal = LHS.getSignedMaxValue().ssub_sat(RHS.getSignedMinValue());
121 }
122 if (MinVal.isNonNegative()) {
123 // If min is non-negative, result will always be non-neg (can't overflow
124 // around).
125 unsigned NumBits = MinVal.trunc(BitWidth - 1).countl_one();
126 KnownOut.One.setBits(BitWidth - 1 - NumBits, BitWidth - 1);
127 KnownOut.Zero.setSignBit();
128 }
129 if (MaxVal.isNegative()) {
130 // If max is negative, result will always be neg (can't overflow around).
131 unsigned NumBits = MaxVal.trunc(BitWidth - 1).countl_zero();
132 KnownOut.Zero.setBits(BitWidth - 1 - NumBits, BitWidth - 1);
133 KnownOut.One.setSignBit();
134 }
135 }
136
137 // Just return 0 if the nsw/nuw is violated and we have poison.
138 if (KnownOut.hasConflict())
139 KnownOut.setAllZero();
140 return KnownOut;
141}
142
144 const KnownBits &Borrow) {
145 assert(Borrow.getBitWidth() == 1 && "Borrow must be 1-bit");
146
147 // LHS - RHS = LHS + ~RHS + 1
148 // Carry 1 - Borrow in ::computeForAddCarry
149 std::swap(RHS.Zero, RHS.One);
150 return ::computeForAddCarry(LHS, RHS,
151 /*CarryZero=*/Borrow.One.getBoolValue(),
152 /*CarryOne=*/Borrow.Zero.getBoolValue());
153}
154
155KnownBits KnownBits::sextInReg(unsigned SrcBitWidth) const {
156 unsigned BitWidth = getBitWidth();
157 assert(0 < SrcBitWidth && SrcBitWidth <= BitWidth &&
158 "Illegal sext-in-register");
159
160 if (SrcBitWidth == BitWidth)
161 return *this;
162
163 unsigned ExtBits = BitWidth - SrcBitWidth;
164 KnownBits Result;
165 Result.One = One << ExtBits;
166 Result.Zero = Zero << ExtBits;
167 Result.One.ashrInPlace(ExtBits);
168 Result.Zero.ashrInPlace(ExtBits);
169 return Result;
170}
171
173 // Count the number of leading bit positions where our underlying value is
174 // known to be less than or equal to Val.
175 unsigned N = (Zero | Val).countl_one();
176
177 // For each of those bit positions, if Val has a 1 in that bit then our
178 // underlying value must also have a 1.
179 APInt MaskedVal(Val);
180 MaskedVal.clearLowBits(getBitWidth() - N);
181 return KnownBits(Zero, One | MaskedVal);
182}
183
185 // If we can prove that LHS >= RHS then use LHS as the result. Likewise for
186 // RHS. Ideally our caller would already have spotted these cases and
187 // optimized away the umax operation, but we handle them here for
188 // completeness.
189 if (LHS.getMinValue().uge(RHS.getMaxValue()))
190 return LHS;
191 if (RHS.getMinValue().uge(LHS.getMaxValue()))
192 return RHS;
193
194 // If the result of the umax is LHS then it must be greater than or equal to
195 // the minimum possible value of RHS. Likewise for RHS. Any known bits that
196 // are common to these two values are also known in the result.
197 KnownBits L = LHS.makeGE(RHS.getMinValue());
198 KnownBits R = RHS.makeGE(LHS.getMinValue());
199 return L.intersectWith(R);
200}
201
203 // Flip the range of values: [0, 0xFFFFFFFF] <-> [0xFFFFFFFF, 0]
204 auto Flip = [](const KnownBits &Val) { return KnownBits(Val.One, Val.Zero); };
205 return Flip(umax(Flip(LHS), Flip(RHS)));
206}
207
209 // Flip the range of values: [-0x80000000, 0x7FFFFFFF] <-> [0, 0xFFFFFFFF]
210 auto Flip = [](const KnownBits &Val) {
211 unsigned SignBitPosition = Val.getBitWidth() - 1;
212 APInt Zero = Val.Zero;
213 APInt One = Val.One;
214 Zero.setBitVal(SignBitPosition, Val.One[SignBitPosition]);
215 One.setBitVal(SignBitPosition, Val.Zero[SignBitPosition]);
216 return KnownBits(Zero, One);
217 };
218 return Flip(umax(Flip(LHS), Flip(RHS)));
219}
220
222 // Flip the range of values: [-0x80000000, 0x7FFFFFFF] <-> [0xFFFFFFFF, 0]
223 auto Flip = [](const KnownBits &Val) {
224 unsigned SignBitPosition = Val.getBitWidth() - 1;
225 APInt Zero = Val.One;
226 APInt One = Val.Zero;
227 Zero.setBitVal(SignBitPosition, Val.Zero[SignBitPosition]);
228 One.setBitVal(SignBitPosition, Val.One[SignBitPosition]);
229 return KnownBits(Zero, One);
230 };
231 return Flip(umax(Flip(LHS), Flip(RHS)));
232}
233
235 // If we know which argument is larger, return (sub LHS, RHS) or
236 // (sub RHS, LHS) directly.
237 if (LHS.getMinValue().uge(RHS.getMaxValue()))
238 return computeForAddSub(/*Add=*/false, /*NSW=*/false, /*NUW=*/false, LHS,
239 RHS);
240 if (RHS.getMinValue().uge(LHS.getMaxValue()))
241 return computeForAddSub(/*Add=*/false, /*NSW=*/false, /*NUW=*/false, RHS,
242 LHS);
243
244 // By construction, the subtraction in abdu never has unsigned overflow.
245 // Find the common bits between (sub nuw LHS, RHS) and (sub nuw RHS, LHS).
246 KnownBits Diff0 =
247 computeForAddSub(/*Add=*/false, /*NSW=*/false, /*NUW=*/true, LHS, RHS);
248 KnownBits Diff1 =
249 computeForAddSub(/*Add=*/false, /*NSW=*/false, /*NUW=*/true, RHS, LHS);
250 return Diff0.intersectWith(Diff1);
251}
252
254 // If we know which argument is larger, return (sub LHS, RHS) or
255 // (sub RHS, LHS) directly.
256 if (LHS.getSignedMinValue().sge(RHS.getSignedMaxValue()))
257 return computeForAddSub(/*Add=*/false, /*NSW=*/false, /*NUW=*/false, LHS,
258 RHS);
259 if (RHS.getSignedMinValue().sge(LHS.getSignedMaxValue()))
260 return computeForAddSub(/*Add=*/false, /*NSW=*/false, /*NUW=*/false, RHS,
261 LHS);
262
263 // Shift both arguments from the signed range to the unsigned range, e.g. from
264 // [-0x80, 0x7F] to [0, 0xFF]. This allows us to use "sub nuw" below just like
265 // abdu does.
266 // Note that we can't just use "sub nsw" instead because abds has signed
267 // inputs but an unsigned result, which makes the overflow conditions
268 // different.
269 unsigned SignBitPosition = LHS.getBitWidth() - 1;
270 for (auto Arg : {&LHS, &RHS}) {
271 bool Tmp = Arg->Zero[SignBitPosition];
272 Arg->Zero.setBitVal(SignBitPosition, Arg->One[SignBitPosition]);
273 Arg->One.setBitVal(SignBitPosition, Tmp);
274 }
275
276 // Find the common bits between (sub nuw LHS, RHS) and (sub nuw RHS, LHS).
277 KnownBits Diff0 =
278 computeForAddSub(/*Add=*/false, /*NSW=*/false, /*NUW=*/true, LHS, RHS);
279 KnownBits Diff1 =
280 computeForAddSub(/*Add=*/false, /*NSW=*/false, /*NUW=*/true, RHS, LHS);
281 return Diff0.intersectWith(Diff1);
282}
283
284static unsigned getMaxShiftAmount(const APInt &MaxValue, unsigned BitWidth) {
286 return MaxValue.extractBitsAsZExtValue(Log2_32(BitWidth), 0);
287 // This is only an approximate upper bound.
288 return MaxValue.getLimitedValue(BitWidth - 1);
289}
290
291KnownBits KnownBits::shl(const KnownBits &LHS, const KnownBits &RHS, bool NUW,
292 bool NSW, bool ShAmtNonZero) {
293 unsigned BitWidth = LHS.getBitWidth();
294 auto ShiftByConst = [&](const KnownBits &LHS, unsigned ShiftAmt) {
295 KnownBits Known;
296 bool ShiftedOutZero, ShiftedOutOne;
297 Known.Zero = LHS.Zero.ushl_ov(ShiftAmt, ShiftedOutZero);
298 Known.Zero.setLowBits(ShiftAmt);
299 Known.One = LHS.One.ushl_ov(ShiftAmt, ShiftedOutOne);
300
301 // All cases returning poison have been handled by MaxShiftAmount already.
302 if (NSW) {
303 if (NUW && ShiftAmt != 0)
304 // NUW means we can assume anything shifted out was a zero.
305 ShiftedOutZero = true;
306
307 if (ShiftedOutZero)
308 Known.makeNonNegative();
309 else if (ShiftedOutOne)
310 Known.makeNegative();
311 }
312 return Known;
313 };
314
315 // Fast path for a common case when LHS is completely unknown.
316 KnownBits Known(BitWidth);
317 unsigned MinShiftAmount = RHS.getMinValue().getLimitedValue(BitWidth);
318 if (MinShiftAmount == 0 && ShAmtNonZero)
319 MinShiftAmount = 1;
320 if (LHS.isUnknown()) {
321 Known.Zero.setLowBits(MinShiftAmount);
322 if (NUW && NSW && MinShiftAmount != 0)
323 Known.makeNonNegative();
324 return Known;
325 }
326
327 // Determine maximum shift amount, taking NUW/NSW flags into account.
328 APInt MaxValue = RHS.getMaxValue();
329 unsigned MaxShiftAmount = getMaxShiftAmount(MaxValue, BitWidth);
330 if (NUW && NSW)
331 MaxShiftAmount = std::min(MaxShiftAmount, LHS.countMaxLeadingZeros() - 1);
332 if (NUW)
333 MaxShiftAmount = std::min(MaxShiftAmount, LHS.countMaxLeadingZeros());
334 if (NSW)
335 MaxShiftAmount = std::min(
336 MaxShiftAmount,
337 std::max(LHS.countMaxLeadingZeros(), LHS.countMaxLeadingOnes()) - 1);
338
339 // Fast path for common case where the shift amount is unknown.
340 if (MinShiftAmount == 0 && MaxShiftAmount == BitWidth - 1 &&
342 Known.Zero.setLowBits(LHS.countMinTrailingZeros());
343 if (LHS.isAllOnes())
344 Known.One.setSignBit();
345 if (NSW) {
346 if (LHS.isNonNegative())
347 Known.makeNonNegative();
348 if (LHS.isNegative())
349 Known.makeNegative();
350 }
351 return Known;
352 }
353
354 // Find the common bits from all possible shifts.
355 unsigned ShiftAmtZeroMask = RHS.Zero.zextOrTrunc(32).getZExtValue();
356 unsigned ShiftAmtOneMask = RHS.One.zextOrTrunc(32).getZExtValue();
357 Known.Zero.setAllBits();
358 Known.One.setAllBits();
359 for (unsigned ShiftAmt = MinShiftAmount; ShiftAmt <= MaxShiftAmount;
360 ++ShiftAmt) {
361 // Skip if the shift amount is impossible.
362 if ((ShiftAmtZeroMask & ShiftAmt) != 0 ||
363 (ShiftAmtOneMask | ShiftAmt) != ShiftAmt)
364 continue;
365 Known = Known.intersectWith(ShiftByConst(LHS, ShiftAmt));
366 if (Known.isUnknown())
367 break;
368 }
369
370 // All shift amounts may result in poison.
371 if (Known.hasConflict())
372 Known.setAllZero();
373 return Known;
374}
375
377 bool ShAmtNonZero, bool Exact) {
378 unsigned BitWidth = LHS.getBitWidth();
379 auto ShiftByConst = [&](const KnownBits &LHS, unsigned ShiftAmt) {
380 KnownBits Known = LHS;
381 Known.Zero.lshrInPlace(ShiftAmt);
382 Known.One.lshrInPlace(ShiftAmt);
383 // High bits are known zero.
384 Known.Zero.setHighBits(ShiftAmt);
385 return Known;
386 };
387
388 // Fast path for a common case when LHS is completely unknown.
389 KnownBits Known(BitWidth);
390 unsigned MinShiftAmount = RHS.getMinValue().getLimitedValue(BitWidth);
391 if (MinShiftAmount == 0 && ShAmtNonZero)
392 MinShiftAmount = 1;
393 if (LHS.isUnknown()) {
394 Known.Zero.setHighBits(MinShiftAmount);
395 return Known;
396 }
397
398 // Find the common bits from all possible shifts.
399 APInt MaxValue = RHS.getMaxValue();
400 unsigned MaxShiftAmount = getMaxShiftAmount(MaxValue, BitWidth);
401
402 // If exact, bound MaxShiftAmount to first known 1 in LHS.
403 if (Exact) {
404 unsigned FirstOne = LHS.countMaxTrailingZeros();
405 if (FirstOne < MinShiftAmount) {
406 // Always poison. Return zero because we don't like returning conflict.
407 Known.setAllZero();
408 return Known;
409 }
410 MaxShiftAmount = std::min(MaxShiftAmount, FirstOne);
411 }
412
413 unsigned ShiftAmtZeroMask = RHS.Zero.zextOrTrunc(32).getZExtValue();
414 unsigned ShiftAmtOneMask = RHS.One.zextOrTrunc(32).getZExtValue();
415 Known.Zero.setAllBits();
416 Known.One.setAllBits();
417 for (unsigned ShiftAmt = MinShiftAmount; ShiftAmt <= MaxShiftAmount;
418 ++ShiftAmt) {
419 // Skip if the shift amount is impossible.
420 if ((ShiftAmtZeroMask & ShiftAmt) != 0 ||
421 (ShiftAmtOneMask | ShiftAmt) != ShiftAmt)
422 continue;
423 Known = Known.intersectWith(ShiftByConst(LHS, ShiftAmt));
424 if (Known.isUnknown())
425 break;
426 }
427
428 // All shift amounts may result in poison.
429 if (Known.hasConflict())
430 Known.setAllZero();
431 return Known;
432}
433
435 bool ShAmtNonZero, bool Exact) {
436 unsigned BitWidth = LHS.getBitWidth();
437 auto ShiftByConst = [&](const KnownBits &LHS, unsigned ShiftAmt) {
438 KnownBits Known = LHS;
439 Known.Zero.ashrInPlace(ShiftAmt);
440 Known.One.ashrInPlace(ShiftAmt);
441 return Known;
442 };
443
444 // Fast path for a common case when LHS is completely unknown.
445 KnownBits Known(BitWidth);
446 unsigned MinShiftAmount = RHS.getMinValue().getLimitedValue(BitWidth);
447 if (MinShiftAmount == 0 && ShAmtNonZero)
448 MinShiftAmount = 1;
449 if (LHS.isUnknown()) {
450 if (MinShiftAmount == BitWidth) {
451 // Always poison. Return zero because we don't like returning conflict.
452 Known.setAllZero();
453 return Known;
454 }
455 return Known;
456 }
457
458 // Find the common bits from all possible shifts.
459 APInt MaxValue = RHS.getMaxValue();
460 unsigned MaxShiftAmount = getMaxShiftAmount(MaxValue, BitWidth);
461
462 // If exact, bound MaxShiftAmount to first known 1 in LHS.
463 if (Exact) {
464 unsigned FirstOne = LHS.countMaxTrailingZeros();
465 if (FirstOne < MinShiftAmount) {
466 // Always poison. Return zero because we don't like returning conflict.
467 Known.setAllZero();
468 return Known;
469 }
470 MaxShiftAmount = std::min(MaxShiftAmount, FirstOne);
471 }
472
473 unsigned ShiftAmtZeroMask = RHS.Zero.zextOrTrunc(32).getZExtValue();
474 unsigned ShiftAmtOneMask = RHS.One.zextOrTrunc(32).getZExtValue();
475 Known.Zero.setAllBits();
476 Known.One.setAllBits();
477 for (unsigned ShiftAmt = MinShiftAmount; ShiftAmt <= MaxShiftAmount;
478 ++ShiftAmt) {
479 // Skip if the shift amount is impossible.
480 if ((ShiftAmtZeroMask & ShiftAmt) != 0 ||
481 (ShiftAmtOneMask | ShiftAmt) != ShiftAmt)
482 continue;
483 Known = Known.intersectWith(ShiftByConst(LHS, ShiftAmt));
484 if (Known.isUnknown())
485 break;
486 }
487
488 // All shift amounts may result in poison.
489 if (Known.hasConflict())
490 Known.setAllZero();
491 return Known;
492}
493
494std::optional<bool> KnownBits::eq(const KnownBits &LHS, const KnownBits &RHS) {
495 if (LHS.isConstant() && RHS.isConstant())
496 return std::optional<bool>(LHS.getConstant() == RHS.getConstant());
497 if (LHS.One.intersects(RHS.Zero) || RHS.One.intersects(LHS.Zero))
498 return std::optional<bool>(false);
499 return std::nullopt;
500}
501
502std::optional<bool> KnownBits::ne(const KnownBits &LHS, const KnownBits &RHS) {
503 if (std::optional<bool> KnownEQ = eq(LHS, RHS))
504 return std::optional<bool>(!*KnownEQ);
505 return std::nullopt;
506}
507
508std::optional<bool> KnownBits::ugt(const KnownBits &LHS, const KnownBits &RHS) {
509 // LHS >u RHS -> false if umax(LHS) <= umax(RHS)
510 if (LHS.getMaxValue().ule(RHS.getMinValue()))
511 return std::optional<bool>(false);
512 // LHS >u RHS -> true if umin(LHS) > umax(RHS)
513 if (LHS.getMinValue().ugt(RHS.getMaxValue()))
514 return std::optional<bool>(true);
515 return std::nullopt;
516}
517
518std::optional<bool> KnownBits::uge(const KnownBits &LHS, const KnownBits &RHS) {
519 if (std::optional<bool> IsUGT = ugt(RHS, LHS))
520 return std::optional<bool>(!*IsUGT);
521 return std::nullopt;
522}
523
524std::optional<bool> KnownBits::ult(const KnownBits &LHS, const KnownBits &RHS) {
525 return ugt(RHS, LHS);
526}
527
528std::optional<bool> KnownBits::ule(const KnownBits &LHS, const KnownBits &RHS) {
529 return uge(RHS, LHS);
530}
531
532std::optional<bool> KnownBits::sgt(const KnownBits &LHS, const KnownBits &RHS) {
533 // LHS >s RHS -> false if smax(LHS) <= smax(RHS)
534 if (LHS.getSignedMaxValue().sle(RHS.getSignedMinValue()))
535 return std::optional<bool>(false);
536 // LHS >s RHS -> true if smin(LHS) > smax(RHS)
537 if (LHS.getSignedMinValue().sgt(RHS.getSignedMaxValue()))
538 return std::optional<bool>(true);
539 return std::nullopt;
540}
541
542std::optional<bool> KnownBits::sge(const KnownBits &LHS, const KnownBits &RHS) {
543 if (std::optional<bool> KnownSGT = sgt(RHS, LHS))
544 return std::optional<bool>(!*KnownSGT);
545 return std::nullopt;
546}
547
548std::optional<bool> KnownBits::slt(const KnownBits &LHS, const KnownBits &RHS) {
549 return sgt(RHS, LHS);
550}
551
552std::optional<bool> KnownBits::sle(const KnownBits &LHS, const KnownBits &RHS) {
553 return sge(RHS, LHS);
554}
555
556KnownBits KnownBits::abs(bool IntMinIsPoison) const {
557 // If the source's MSB is zero then we know the rest of the bits already.
558 if (isNonNegative())
559 return *this;
560
561 // Absolute value preserves trailing zero count.
562 KnownBits KnownAbs(getBitWidth());
563
564 // If the input is negative, then abs(x) == -x.
565 if (isNegative()) {
566 KnownBits Tmp = *this;
567 // Special case for IntMinIsPoison. We know the sign bit is set and we know
568 // all the rest of the bits except one to be zero. Since we have
569 // IntMinIsPoison, that final bit MUST be a one, as otherwise the input is
570 // INT_MIN.
571 if (IntMinIsPoison && (Zero.popcount() + 2) == getBitWidth())
573
574 KnownAbs = computeForAddSub(
575 /*Add*/ false, IntMinIsPoison, /*NUW=*/false,
577
578 // One more special case for IntMinIsPoison. If we don't know any ones other
579 // than the signbit, we know for certain that all the unknowns can't be
580 // zero. So if we know high zero bits, but have unknown low bits, we know
581 // for certain those high-zero bits will end up as one. This is because,
582 // the low bits can't be all zeros, so the +1 in (~x + 1) cannot carry up
583 // to the high bits. If we know a known INT_MIN input skip this. The result
584 // is poison anyways.
585 if (IntMinIsPoison && Tmp.countMinPopulation() == 1 &&
586 Tmp.countMaxPopulation() != 1) {
587 Tmp.One.clearSignBit();
588 Tmp.Zero.setSignBit();
589 KnownAbs.One.setBits(getBitWidth() - Tmp.countMinLeadingZeros(),
590 getBitWidth() - 1);
591 }
592
593 } else {
594 unsigned MaxTZ = countMaxTrailingZeros();
595 unsigned MinTZ = countMinTrailingZeros();
596
597 KnownAbs.Zero.setLowBits(MinTZ);
598 // If we know the lowest set 1, then preserve it.
599 if (MaxTZ == MinTZ && MaxTZ < getBitWidth())
600 KnownAbs.One.setBit(MaxTZ);
601
602 // We only know that the absolute values's MSB will be zero if INT_MIN is
603 // poison, or there is a set bit that isn't the sign bit (otherwise it could
604 // be INT_MIN).
605 if (IntMinIsPoison || (!One.isZero() && !One.isMinSignedValue())) {
606 KnownAbs.One.clearSignBit();
607 KnownAbs.Zero.setSignBit();
608 }
609 }
610
611 assert(!KnownAbs.hasConflict() && "Bad Output");
612 return KnownAbs;
613}
614
616 const KnownBits &LHS,
617 const KnownBits &RHS) {
618 assert(!LHS.hasConflict() && !RHS.hasConflict() && "Bad inputs");
619 // We don't see NSW even for sadd/ssub as we want to check if the result has
620 // signed overflow.
621 KnownBits Res =
622 KnownBits::computeForAddSub(Add, /*NSW=*/false, /*NUW=*/false, LHS, RHS);
623 unsigned BitWidth = Res.getBitWidth();
624 auto SignBitKnown = [&](const KnownBits &K) {
625 return K.Zero[BitWidth - 1] || K.One[BitWidth - 1];
626 };
627 std::optional<bool> Overflow;
628
629 if (Signed) {
630 // If we can actually detect overflow do so. Otherwise leave Overflow as
631 // nullopt (we assume it may have happened).
632 if (SignBitKnown(LHS) && SignBitKnown(RHS) && SignBitKnown(Res)) {
633 if (Add) {
634 // sadd.sat
635 Overflow = (LHS.isNonNegative() == RHS.isNonNegative() &&
636 Res.isNonNegative() != LHS.isNonNegative());
637 } else {
638 // ssub.sat
639 Overflow = (LHS.isNonNegative() != RHS.isNonNegative() &&
640 Res.isNonNegative() != LHS.isNonNegative());
641 }
642 }
643 } else if (Add) {
644 // uadd.sat
645 bool Of;
646 (void)LHS.getMaxValue().uadd_ov(RHS.getMaxValue(), Of);
647 if (!Of) {
648 Overflow = false;
649 } else {
650 (void)LHS.getMinValue().uadd_ov(RHS.getMinValue(), Of);
651 if (Of)
652 Overflow = true;
653 }
654 } else {
655 // usub.sat
656 bool Of;
657 (void)LHS.getMinValue().usub_ov(RHS.getMaxValue(), Of);
658 if (!Of) {
659 Overflow = false;
660 } else {
661 (void)LHS.getMaxValue().usub_ov(RHS.getMinValue(), Of);
662 if (Of)
663 Overflow = true;
664 }
665 }
666
667 if (Signed) {
668 if (Add) {
669 if (LHS.isNonNegative() && RHS.isNonNegative()) {
670 // Pos + Pos -> Pos
671 Res.One.clearSignBit();
672 Res.Zero.setSignBit();
673 }
674 if (LHS.isNegative() && RHS.isNegative()) {
675 // Neg + Neg -> Neg
676 Res.One.setSignBit();
677 Res.Zero.clearSignBit();
678 }
679 } else {
680 if (LHS.isNegative() && RHS.isNonNegative()) {
681 // Neg - Pos -> Neg
682 Res.One.setSignBit();
683 Res.Zero.clearSignBit();
684 } else if (LHS.isNonNegative() && RHS.isNegative()) {
685 // Pos - Neg -> Pos
686 Res.One.clearSignBit();
687 Res.Zero.setSignBit();
688 }
689 }
690 } else {
691 // Add: Leading ones of either operand are preserved.
692 // Sub: Leading zeros of LHS and leading ones of RHS are preserved
693 // as leading zeros in the result.
694 unsigned LeadingKnown;
695 if (Add)
696 LeadingKnown =
697 std::max(LHS.countMinLeadingOnes(), RHS.countMinLeadingOnes());
698 else
699 LeadingKnown =
700 std::max(LHS.countMinLeadingZeros(), RHS.countMinLeadingOnes());
701
702 // We select between the operation result and all-ones/zero
703 // respectively, so we can preserve known ones/zeros.
704 APInt Mask = APInt::getHighBitsSet(BitWidth, LeadingKnown);
705 if (Add) {
706 Res.One |= Mask;
707 Res.Zero &= ~Mask;
708 } else {
709 Res.Zero |= Mask;
710 Res.One &= ~Mask;
711 }
712 }
713
714 if (Overflow) {
715 // We know whether or not we overflowed.
716 if (!(*Overflow)) {
717 // No overflow.
718 assert(!Res.hasConflict() && "Bad Output");
719 return Res;
720 }
721
722 // We overflowed
723 APInt C;
724 if (Signed) {
725 // sadd.sat / ssub.sat
726 assert(SignBitKnown(LHS) &&
727 "We somehow know overflow without knowing input sign");
728 C = LHS.isNegative() ? APInt::getSignedMinValue(BitWidth)
730 } else if (Add) {
731 // uadd.sat
733 } else {
734 // uadd.sat
736 }
737
738 Res.One = C;
739 Res.Zero = ~C;
740 assert(!Res.hasConflict() && "Bad Output");
741 return Res;
742 }
743
744 // We don't know if we overflowed.
745 if (Signed) {
746 // sadd.sat/ssub.sat
747 // We can keep our information about the sign bits.
748 Res.Zero.clearLowBits(BitWidth - 1);
749 Res.One.clearLowBits(BitWidth - 1);
750 } else if (Add) {
751 // uadd.sat
752 // We need to clear all the known zeros as we can only use the leading ones.
753 Res.Zero.clearAllBits();
754 } else {
755 // usub.sat
756 // We need to clear all the known ones as we can only use the leading zero.
757 Res.One.clearAllBits();
758 }
759
760 assert(!Res.hasConflict() && "Bad Output");
761 return Res;
762}
763
765 return computeForSatAddSub(/*Add*/ true, /*Signed*/ true, LHS, RHS);
766}
768 return computeForSatAddSub(/*Add*/ false, /*Signed*/ true, LHS, RHS);
769}
771 return computeForSatAddSub(/*Add*/ true, /*Signed*/ false, LHS, RHS);
772}
774 return computeForSatAddSub(/*Add*/ false, /*Signed*/ false, LHS, RHS);
775}
776
778 bool NoUndefSelfMultiply) {
779 unsigned BitWidth = LHS.getBitWidth();
780 assert(BitWidth == RHS.getBitWidth() && !LHS.hasConflict() &&
781 !RHS.hasConflict() && "Operand mismatch");
782 assert((!NoUndefSelfMultiply || LHS == RHS) &&
783 "Self multiplication knownbits mismatch");
784
785 // Compute the high known-0 bits by multiplying the unsigned max of each side.
786 // Conservatively, M active bits * N active bits results in M + N bits in the
787 // result. But if we know a value is a power-of-2 for example, then this
788 // computes one more leading zero.
789 // TODO: This could be generalized to number of sign bits (negative numbers).
790 APInt UMaxLHS = LHS.getMaxValue();
791 APInt UMaxRHS = RHS.getMaxValue();
792
793 // For leading zeros in the result to be valid, the unsigned max product must
794 // fit in the bitwidth (it must not overflow).
795 bool HasOverflow;
796 APInt UMaxResult = UMaxLHS.umul_ov(UMaxRHS, HasOverflow);
797 unsigned LeadZ = HasOverflow ? 0 : UMaxResult.countl_zero();
798
799 // The result of the bottom bits of an integer multiply can be
800 // inferred by looking at the bottom bits of both operands and
801 // multiplying them together.
802 // We can infer at least the minimum number of known trailing bits
803 // of both operands. Depending on number of trailing zeros, we can
804 // infer more bits, because (a*b) <=> ((a/m) * (b/n)) * (m*n) assuming
805 // a and b are divisible by m and n respectively.
806 // We then calculate how many of those bits are inferrable and set
807 // the output. For example, the i8 mul:
808 // a = XXXX1100 (12)
809 // b = XXXX1110 (14)
810 // We know the bottom 3 bits are zero since the first can be divided by
811 // 4 and the second by 2, thus having ((12/4) * (14/2)) * (2*4).
812 // Applying the multiplication to the trimmed arguments gets:
813 // XX11 (3)
814 // X111 (7)
815 // -------
816 // XX11
817 // XX11
818 // XX11
819 // XX11
820 // -------
821 // XXXXX01
822 // Which allows us to infer the 2 LSBs. Since we're multiplying the result
823 // by 8, the bottom 3 bits will be 0, so we can infer a total of 5 bits.
824 // The proof for this can be described as:
825 // Pre: (C1 >= 0) && (C1 < (1 << C5)) && (C2 >= 0) && (C2 < (1 << C6)) &&
826 // (C7 == (1 << (umin(countTrailingZeros(C1), C5) +
827 // umin(countTrailingZeros(C2), C6) +
828 // umin(C5 - umin(countTrailingZeros(C1), C5),
829 // C6 - umin(countTrailingZeros(C2), C6)))) - 1)
830 // %aa = shl i8 %a, C5
831 // %bb = shl i8 %b, C6
832 // %aaa = or i8 %aa, C1
833 // %bbb = or i8 %bb, C2
834 // %mul = mul i8 %aaa, %bbb
835 // %mask = and i8 %mul, C7
836 // =>
837 // %mask = i8 ((C1*C2)&C7)
838 // Where C5, C6 describe the known bits of %a, %b
839 // C1, C2 describe the known bottom bits of %a, %b.
840 // C7 describes the mask of the known bits of the result.
841 const APInt &Bottom0 = LHS.One;
842 const APInt &Bottom1 = RHS.One;
843
844 // How many times we'd be able to divide each argument by 2 (shr by 1).
845 // This gives us the number of trailing zeros on the multiplication result.
846 unsigned TrailBitsKnown0 = (LHS.Zero | LHS.One).countr_one();
847 unsigned TrailBitsKnown1 = (RHS.Zero | RHS.One).countr_one();
848 unsigned TrailZero0 = LHS.countMinTrailingZeros();
849 unsigned TrailZero1 = RHS.countMinTrailingZeros();
850 unsigned TrailZ = TrailZero0 + TrailZero1;
851
852 // Figure out the fewest known-bits operand.
853 unsigned SmallestOperand =
854 std::min(TrailBitsKnown0 - TrailZero0, TrailBitsKnown1 - TrailZero1);
855 unsigned ResultBitsKnown = std::min(SmallestOperand + TrailZ, BitWidth);
856
857 APInt BottomKnown =
858 Bottom0.getLoBits(TrailBitsKnown0) * Bottom1.getLoBits(TrailBitsKnown1);
859
860 KnownBits Res(BitWidth);
861 Res.Zero.setHighBits(LeadZ);
862 Res.Zero |= (~BottomKnown).getLoBits(ResultBitsKnown);
863 Res.One = BottomKnown.getLoBits(ResultBitsKnown);
864
865 // If we're self-multiplying then bit[1] is guaranteed to be zero.
866 if (NoUndefSelfMultiply && BitWidth > 1) {
867 assert(Res.One[1] == 0 &&
868 "Self-multiplication failed Quadratic Reciprocity!");
869 Res.Zero.setBit(1);
870 }
871
872 return Res;
873}
874
876 unsigned BitWidth = LHS.getBitWidth();
877 assert(BitWidth == RHS.getBitWidth() && !LHS.hasConflict() &&
878 !RHS.hasConflict() && "Operand mismatch");
879 KnownBits WideLHS = LHS.sext(2 * BitWidth);
880 KnownBits WideRHS = RHS.sext(2 * BitWidth);
881 return mul(WideLHS, WideRHS).extractBits(BitWidth, BitWidth);
882}
883
885 unsigned BitWidth = LHS.getBitWidth();
886 assert(BitWidth == RHS.getBitWidth() && !LHS.hasConflict() &&
887 !RHS.hasConflict() && "Operand mismatch");
888 KnownBits WideLHS = LHS.zext(2 * BitWidth);
889 KnownBits WideRHS = RHS.zext(2 * BitWidth);
890 return mul(WideLHS, WideRHS).extractBits(BitWidth, BitWidth);
891}
892
894 const KnownBits &RHS, bool Exact) {
895
896 if (!Exact)
897 return Known;
898
899 // If LHS is Odd, the result is Odd no matter what.
900 // Odd / Odd -> Odd
901 // Odd / Even -> Impossible (because its exact division)
902 if (LHS.One[0])
903 Known.One.setBit(0);
904
905 int MinTZ =
906 (int)LHS.countMinTrailingZeros() - (int)RHS.countMaxTrailingZeros();
907 int MaxTZ =
908 (int)LHS.countMaxTrailingZeros() - (int)RHS.countMinTrailingZeros();
909 if (MinTZ >= 0) {
910 // Result has at least MinTZ trailing zeros.
911 Known.Zero.setLowBits(MinTZ);
912 if (MinTZ == MaxTZ) {
913 // Result has exactly MinTZ trailing zeros.
914 Known.One.setBit(MinTZ);
915 }
916 } else if (MaxTZ < 0) {
917 // Poison Result
918 Known.setAllZero();
919 }
920
921 // In the KnownBits exhaustive tests, we have poison inputs for exact values
922 // a LOT. If we have a conflict, just return all zeros.
923 if (Known.hasConflict())
924 Known.setAllZero();
925
926 return Known;
927}
928
930 bool Exact) {
931 // Equivalent of `udiv`. We must have caught this before it was folded.
932 if (LHS.isNonNegative() && RHS.isNonNegative())
933 return udiv(LHS, RHS, Exact);
934
935 unsigned BitWidth = LHS.getBitWidth();
936 assert(!LHS.hasConflict() && !RHS.hasConflict() && "Bad inputs");
937 KnownBits Known(BitWidth);
938
939 if (LHS.isZero() || RHS.isZero()) {
940 // Result is either known Zero or UB. Return Zero either way.
941 // Checking this earlier saves us a lot of special cases later on.
942 Known.setAllZero();
943 return Known;
944 }
945
946 std::optional<APInt> Res;
947 if (LHS.isNegative() && RHS.isNegative()) {
948 // Result non-negative.
949 APInt Denom = RHS.getSignedMaxValue();
950 APInt Num = LHS.getSignedMinValue();
951 // INT_MIN/-1 would be a poison result (impossible). Estimate the division
952 // as signed max (we will only set sign bit in the result).
953 Res = (Num.isMinSignedValue() && Denom.isAllOnes())
955 : Num.sdiv(Denom);
956 } else if (LHS.isNegative() && RHS.isNonNegative()) {
957 // Result is negative if Exact OR -LHS u>= RHS.
958 if (Exact || (-LHS.getSignedMaxValue()).uge(RHS.getSignedMaxValue())) {
959 APInt Denom = RHS.getSignedMinValue();
960 APInt Num = LHS.getSignedMinValue();
961 Res = Denom.isZero() ? Num : Num.sdiv(Denom);
962 }
963 } else if (LHS.isStrictlyPositive() && RHS.isNegative()) {
964 // Result is negative if Exact OR LHS u>= -RHS.
965 if (Exact || LHS.getSignedMinValue().uge(-RHS.getSignedMinValue())) {
966 APInt Denom = RHS.getSignedMaxValue();
967 APInt Num = LHS.getSignedMaxValue();
968 Res = Num.sdiv(Denom);
969 }
970 }
971
972 if (Res) {
973 if (Res->isNonNegative()) {
974 unsigned LeadZ = Res->countLeadingZeros();
975 Known.Zero.setHighBits(LeadZ);
976 } else {
977 unsigned LeadO = Res->countLeadingOnes();
978 Known.One.setHighBits(LeadO);
979 }
980 }
981
982 Known = divComputeLowBit(Known, LHS, RHS, Exact);
983
984 assert(!Known.hasConflict() && "Bad Output");
985 return Known;
986}
987
989 bool Exact) {
990 unsigned BitWidth = LHS.getBitWidth();
991 assert(!LHS.hasConflict() && !RHS.hasConflict());
992 KnownBits Known(BitWidth);
993
994 if (LHS.isZero() || RHS.isZero()) {
995 // Result is either known Zero or UB. Return Zero either way.
996 // Checking this earlier saves us a lot of special cases later on.
997 Known.setAllZero();
998 return Known;
999 }
1000
1001 // We can figure out the minimum number of upper zero bits by doing
1002 // MaxNumerator / MinDenominator. If the Numerator gets smaller or Denominator
1003 // gets larger, the number of upper zero bits increases.
1004 APInt MinDenom = RHS.getMinValue();
1005 APInt MaxNum = LHS.getMaxValue();
1006 APInt MaxRes = MinDenom.isZero() ? MaxNum : MaxNum.udiv(MinDenom);
1007
1008 unsigned LeadZ = MaxRes.countLeadingZeros();
1009
1010 Known.Zero.setHighBits(LeadZ);
1011 Known = divComputeLowBit(Known, LHS, RHS, Exact);
1012
1013 assert(!Known.hasConflict() && "Bad Output");
1014 return Known;
1015}
1016
1017KnownBits KnownBits::remGetLowBits(const KnownBits &LHS, const KnownBits &RHS) {
1018 unsigned BitWidth = LHS.getBitWidth();
1019 if (!RHS.isZero() && RHS.Zero[0]) {
1020 // rem X, Y where Y[0:N] is zero will preserve X[0:N] in the result.
1021 unsigned RHSZeros = RHS.countMinTrailingZeros();
1022 APInt Mask = APInt::getLowBitsSet(BitWidth, RHSZeros);
1023 APInt OnesMask = LHS.One & Mask;
1024 APInt ZerosMask = LHS.Zero & Mask;
1025 return KnownBits(ZerosMask, OnesMask);
1026 }
1027 return KnownBits(BitWidth);
1028}
1029
1031 assert(!LHS.hasConflict() && !RHS.hasConflict());
1032
1033 KnownBits Known = remGetLowBits(LHS, RHS);
1034 if (RHS.isConstant() && RHS.getConstant().isPowerOf2()) {
1035 // NB: Low bits set in `remGetLowBits`.
1036 APInt HighBits = ~(RHS.getConstant() - 1);
1037 Known.Zero |= HighBits;
1038 return Known;
1039 }
1040
1041 // Since the result is less than or equal to either operand, any leading
1042 // zero bits in either operand must also exist in the result.
1043 uint32_t Leaders =
1044 std::max(LHS.countMinLeadingZeros(), RHS.countMinLeadingZeros());
1045 Known.Zero.setHighBits(Leaders);
1046 return Known;
1047}
1048
1050 assert(!LHS.hasConflict() && !RHS.hasConflict());
1051
1052 KnownBits Known = remGetLowBits(LHS, RHS);
1053 if (RHS.isConstant() && RHS.getConstant().isPowerOf2()) {
1054 // NB: Low bits are set in `remGetLowBits`.
1055 APInt LowBits = RHS.getConstant() - 1;
1056 // If the first operand is non-negative or has all low bits zero, then
1057 // the upper bits are all zero.
1058 if (LHS.isNonNegative() || LowBits.isSubsetOf(LHS.Zero))
1059 Known.Zero |= ~LowBits;
1060
1061 // If the first operand is negative and not all low bits are zero, then
1062 // the upper bits are all one.
1063 if (LHS.isNegative() && LowBits.intersects(LHS.One))
1064 Known.One |= ~LowBits;
1065 return Known;
1066 }
1067
1068 // The sign bit is the LHS's sign bit, except when the result of the
1069 // remainder is zero. The magnitude of the result should be less than or
1070 // equal to the magnitude of the LHS. Therefore any leading zeros that exist
1071 // in the left hand side must also exist in the result.
1072 Known.Zero.setHighBits(LHS.countMinLeadingZeros());
1073 return Known;
1074}
1075
1077 // Result bit is 0 if either operand bit is 0.
1078 Zero |= RHS.Zero;
1079 // Result bit is 1 if both operand bits are 1.
1080 One &= RHS.One;
1081 return *this;
1082}
1083
1085 // Result bit is 0 if both operand bits are 0.
1086 Zero &= RHS.Zero;
1087 // Result bit is 1 if either operand bit is 1.
1088 One |= RHS.One;
1089 return *this;
1090}
1091
1093 // Result bit is 0 if both operand bits are 0 or both are 1.
1094 APInt Z = (Zero & RHS.Zero) | (One & RHS.One);
1095 // Result bit is 1 if one operand bit is 0 and the other is 1.
1096 One = (Zero & RHS.One) | (One & RHS.Zero);
1097 Zero = std::move(Z);
1098 return *this;
1099}
1100
1102 unsigned BitWidth = getBitWidth();
1103 KnownBits Known(Zero, APInt(BitWidth, 0));
1104 unsigned Max = countMaxTrailingZeros();
1105 Known.Zero.setBitsFrom(std::min(Max + 1, BitWidth));
1106 unsigned Min = countMinTrailingZeros();
1107 if (Max == Min && Max < BitWidth)
1108 Known.One.setBit(Max);
1109 return Known;
1110}
1111
1113 unsigned BitWidth = getBitWidth();
1114 KnownBits Known(BitWidth);
1115 unsigned Max = countMaxTrailingZeros();
1116 Known.Zero.setBitsFrom(std::min(Max + 1, BitWidth));
1117 unsigned Min = countMinTrailingZeros();
1118 Known.One.setLowBits(std::min(Min + 1, BitWidth));
1119 return Known;
1120}
1121
1123 unsigned BitWidth = getBitWidth();
1124 for (unsigned I = 0; I < BitWidth; ++I) {
1125 unsigned N = BitWidth - I - 1;
1126 if (Zero[N] && One[N])
1127 OS << "!";
1128 else if (Zero[N])
1129 OS << "0";
1130 else if (One[N])
1131 OS << "1";
1132 else
1133 OS << "?";
1134 }
1135}
1136void KnownBits::dump() const {
1137 print(dbgs());
1138 dbgs() << "\n";
1139}
static KnownBits computeForSatAddSub(bool Add, bool Signed, const KnownBits &LHS, const KnownBits &RHS)
Definition: KnownBits.cpp:615
static KnownBits divComputeLowBit(KnownBits Known, const KnownBits &LHS, const KnownBits &RHS, bool Exact)
Definition: KnownBits.cpp:893
static KnownBits computeForAddCarry(const KnownBits &LHS, const KnownBits &RHS, bool CarryZero, bool CarryOne)
Definition: KnownBits.cpp:21
static unsigned getMaxShiftAmount(const APInt &MaxValue, unsigned BitWidth)
Definition: KnownBits.cpp:284
#define I(x, y, z)
Definition: MD5.cpp:58
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
raw_pwrite_stream & OS
Value * RHS
Value * LHS
Class for arbitrary precision integers.
Definition: APInt.h:76
APInt umul_ov(const APInt &RHS, bool &Overflow) const
Definition: APInt.cpp:1941
APInt udiv(const APInt &RHS) const
Unsigned division operation.
Definition: APInt.cpp:1543
APInt getLoBits(unsigned numBits) const
Compute an APInt containing numBits lowbits from this APInt.
Definition: APInt.cpp:613
bool isMinSignedValue() const
Determine if this is the smallest signed value.
Definition: APInt.h:401
void setHighBits(unsigned hiBits)
Set the top hiBits bits.
Definition: APInt.h:1370
unsigned popcount() const
Count the number of bits set.
Definition: APInt.h:1620
void setBitsFrom(unsigned loBit)
Set the top bits starting from loBit.
Definition: APInt.h:1364
uint64_t extractBitsAsZExtValue(unsigned numBits, unsigned bitPosition) const
Definition: APInt.cpp:489
APInt trunc(unsigned width) const
Truncate to new width.
Definition: APInt.cpp:906
static APInt getMaxValue(unsigned numBits)
Gets maximum unsigned value of APInt for specific bit width.
Definition: APInt.h:184
void setBit(unsigned BitPosition)
Set the given bit to 1 whose position is given as "bitPosition".
Definition: APInt.h:1308
bool isAllOnes() const
Determine if all bits are set. This is true for zero-width values.
Definition: APInt.h:349
bool isZero() const
Determine if this value is zero, i.e. all bits are clear.
Definition: APInt.h:358
void setSignBit()
Set the sign bit to 1.
Definition: APInt.h:1318
static APInt getSignedMaxValue(unsigned numBits)
Gets maximum signed value of APInt for a specific bit width.
Definition: APInt.h:187
static APInt getMinValue(unsigned numBits)
Gets minimum unsigned value of APInt for a specific bit width.
Definition: APInt.h:194
bool isNegative() const
Determine sign of this APInt.
Definition: APInt.h:307
bool intersects(const APInt &RHS) const
This operation tests if there are any pairs of corresponding bits between this APInt and RHS that are...
Definition: APInt.h:1227
APInt sdiv(const APInt &RHS) const
Signed division function for APInt.
Definition: APInt.cpp:1614
void clearAllBits()
Set every bit to 0.
Definition: APInt.h:1375
void ashrInPlace(unsigned ShiftAmt)
Arithmetic right-shift this APInt by ShiftAmt in place.
Definition: APInt.h:812
unsigned countl_zero() const
The APInt version of std::countl_zero.
Definition: APInt.h:1548
static APInt getSignedMinValue(unsigned numBits)
Gets minimum signed value of APInt for a specific bit width.
Definition: APInt.h:197
unsigned countLeadingZeros() const
Definition: APInt.h:1556
unsigned countl_one() const
Count the number of leading one bits.
Definition: APInt.h:1565
void clearLowBits(unsigned loBits)
Set bottom loBits bits to 0.
Definition: APInt.h:1395
uint64_t getLimitedValue(uint64_t Limit=UINT64_MAX) const
If this value is smaller than the specified limit, return it, otherwise return the limit value.
Definition: APInt.h:453
void setAllBits()
Set every bit to 1.
Definition: APInt.h:1297
bool getBoolValue() const
Convert APInt to a boolean value.
Definition: APInt.h:449
bool isNonNegative() const
Determine if this APInt Value is non-negative (>= 0)
Definition: APInt.h:312
void setBits(unsigned loBit, unsigned hiBit)
Set the bits from loBit (inclusive) to hiBit (exclusive) to 1.
Definition: APInt.h:1345
bool isSubsetOf(const APInt &RHS) const
This operation checks that all bits set in this APInt are also set in RHS.
Definition: APInt.h:1235
static APInt getLowBitsSet(unsigned numBits, unsigned loBitsSet)
Constructs an APInt value that has the bottom loBitsSet bits set.
Definition: APInt.h:284
static APInt getHighBitsSet(unsigned numBits, unsigned hiBitsSet)
Constructs an APInt value that has the top hiBitsSet bits set.
Definition: APInt.h:274
void setLowBits(unsigned loBits)
Set the bottom loBits bits.
Definition: APInt.h:1367
void lshrInPlace(unsigned ShiftAmt)
Logical right-shift this APInt by ShiftAmt in place.
Definition: APInt.h:836
void setBitVal(unsigned BitPosition, bool BitValue)
Set a given bit to a given value.
Definition: APInt.h:1321
void clearSignBit()
Set the sign bit to 0.
Definition: APInt.h:1402
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:52
@ C
The default llvm calling convention, compatible with C.
Definition: CallingConv.h:34
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
int countr_one(T Value)
Count the number of ones from the least significant bit to the first zero bit.
Definition: bit.h:307
unsigned Log2_32(uint32_t Value)
Return the floor log base 2 of the specified value, -1 if the value is zero.
Definition: MathExtras.h:324
constexpr bool isPowerOf2_32(uint32_t Value)
Return true if the argument is a power of two > 0.
Definition: MathExtras.h:275
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:163
int countl_one(T Value)
Count the number of ones from the most significant bit to the first zero bit.
Definition: bit.h:294
@ Add
Sum of integers.
constexpr unsigned BitWidth
Definition: BitmaskEnum.h:191
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:1849
Implement std::hash so that hash_code can be used in STL containers.
Definition: BitVector.h:858
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
Definition: BitVector.h:860
#define N
static KnownBits makeConstant(const APInt &C)
Create known bits from a known constant.
Definition: KnownBits.h:297
static KnownBits sadd_sat(const KnownBits &LHS, const KnownBits &RHS)
Compute knownbits resulting from llvm.sadd.sat(LHS, RHS)
Definition: KnownBits.cpp:764
static std::optional< bool > eq(const KnownBits &LHS, const KnownBits &RHS)
Determine if these known bits always give the same ICMP_EQ result.
Definition: KnownBits.cpp:494
KnownBits sextInReg(unsigned SrcBitWidth) const
Return known bits for a in-register sign extension of the value we're tracking.
Definition: KnownBits.cpp:155
static KnownBits mulhu(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits from zero-extended multiply-hi.
Definition: KnownBits.cpp:884
static KnownBits smax(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for smax(LHS, RHS).
Definition: KnownBits.cpp:208
bool isNonNegative() const
Returns true if this value is known to be non-negative.
Definition: KnownBits.h:104
KnownBits blsi() const
Compute known bits for X & -X, which has only the lowest bit set of X set.
Definition: KnownBits.cpp:1101
void makeNonNegative()
Make this value non-negative.
Definition: KnownBits.h:120
static KnownBits usub_sat(const KnownBits &LHS, const KnownBits &RHS)
Compute knownbits resulting from llvm.usub.sat(LHS, RHS)
Definition: KnownBits.cpp:773
unsigned countMinTrailingZeros() const
Returns the minimum number of trailing zero bits.
Definition: KnownBits.h:238
static KnownBits ashr(const KnownBits &LHS, const KnownBits &RHS, bool ShAmtNonZero=false, bool Exact=false)
Compute known bits for ashr(LHS, RHS).
Definition: KnownBits.cpp:434
static KnownBits ssub_sat(const KnownBits &LHS, const KnownBits &RHS)
Compute knownbits resulting from llvm.ssub.sat(LHS, RHS)
Definition: KnownBits.cpp:767
static KnownBits urem(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for urem(LHS, RHS).
Definition: KnownBits.cpp:1030
bool isUnknown() const
Returns true if we don't know any bits.
Definition: KnownBits.h:63
unsigned countMaxTrailingZeros() const
Returns the maximum number of trailing zero bits possible.
Definition: KnownBits.h:270
static std::optional< bool > ne(const KnownBits &LHS, const KnownBits &RHS)
Determine if these known bits always give the same ICMP_NE result.
Definition: KnownBits.cpp:502
KnownBits makeGE(const APInt &Val) const
Return KnownBits based on this, but updated given that the underlying value is known to be greater th...
Definition: KnownBits.cpp:172
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...
Definition: KnownBits.cpp:1112
void makeNegative()
Make this value negative.
Definition: KnownBits.h:115
bool hasConflict() const
Returns true if there is conflicting information.
Definition: KnownBits.h:47
static std::optional< bool > sge(const KnownBits &LHS, const KnownBits &RHS)
Determine if these known bits always give the same ICMP_SGE result.
Definition: KnownBits.cpp:542
unsigned countMaxPopulation() const
Returns the maximum number of bits that could be one.
Definition: KnownBits.h:285
void setAllZero()
Make all bits known to be zero and discard any previous information.
Definition: KnownBits.h:89
KnownBits & operator|=(const KnownBits &RHS)
Update known bits based on ORing with RHS.
Definition: KnownBits.cpp:1084
void print(raw_ostream &OS) const
Definition: KnownBits.cpp:1122
unsigned getBitWidth() const
Get the bit width of this value.
Definition: KnownBits.h:40
static KnownBits umax(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for umax(LHS, RHS).
Definition: KnownBits.cpp:184
static KnownBits lshr(const KnownBits &LHS, const KnownBits &RHS, bool ShAmtNonZero=false, bool Exact=false)
Compute known bits for lshr(LHS, RHS).
Definition: KnownBits.cpp:376
static KnownBits abdu(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for abdu(LHS, RHS).
Definition: KnownBits.cpp:234
KnownBits extractBits(unsigned NumBits, unsigned BitPosition) const
Return a subset of the known bits from [bitPosition,bitPosition+numBits).
Definition: KnownBits.h:221
KnownBits intersectWith(const KnownBits &RHS) const
Returns KnownBits information that is known to be true for both this and RHS.
Definition: KnownBits.h:307
static KnownBits computeForSubBorrow(const KnownBits &LHS, KnownBits RHS, const KnownBits &Borrow)
Compute known bits results from subtracting RHS from LHS with 1-bit Borrow.
Definition: KnownBits.cpp:143
unsigned countMinLeadingZeros() const
Returns the minimum number of leading zero bits.
Definition: KnownBits.h:244
KnownBits()=default
static KnownBits abds(KnownBits LHS, KnownBits RHS)
Compute known bits for abds(LHS, RHS).
Definition: KnownBits.cpp:253
static KnownBits smin(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for smin(LHS, RHS).
Definition: KnownBits.cpp:221
KnownBits & operator&=(const KnownBits &RHS)
Update known bits based on ANDing with RHS.
Definition: KnownBits.cpp:1076
static KnownBits mulhs(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits from sign-extended multiply-hi.
Definition: KnownBits.cpp:875
static KnownBits srem(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for srem(LHS, RHS).
Definition: KnownBits.cpp:1049
static std::optional< bool > ugt(const KnownBits &LHS, const KnownBits &RHS)
Determine if these known bits always give the same ICMP_UGT result.
Definition: KnownBits.cpp:508
static KnownBits udiv(const KnownBits &LHS, const KnownBits &RHS, bool Exact=false)
Compute known bits for udiv(LHS, RHS).
Definition: KnownBits.cpp:988
static std::optional< bool > slt(const KnownBits &LHS, const KnownBits &RHS)
Determine if these known bits always give the same ICMP_SLT result.
Definition: KnownBits.cpp:548
static 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:57
void dump() const
Definition: KnownBits.cpp:1136
static KnownBits sdiv(const KnownBits &LHS, const KnownBits &RHS, bool Exact=false)
Compute known bits for sdiv(LHS, RHS).
Definition: KnownBits.cpp:929
static std::optional< bool > ult(const KnownBits &LHS, const KnownBits &RHS)
Determine if these known bits always give the same ICMP_ULT result.
Definition: KnownBits.cpp:524
static std::optional< bool > ule(const KnownBits &LHS, const KnownBits &RHS)
Determine if these known bits always give the same ICMP_ULE result.
Definition: KnownBits.cpp:528
bool isNegative() const
Returns true if this value is known to be negative.
Definition: KnownBits.h:101
static 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:50
static KnownBits uadd_sat(const KnownBits &LHS, const KnownBits &RHS)
Compute knownbits resulting from llvm.uadd.sat(LHS, RHS)
Definition: KnownBits.cpp:770
static KnownBits mul(const KnownBits &LHS, const KnownBits &RHS, bool NoUndefSelfMultiply=false)
Compute known bits resulting from multiplying LHS and RHS.
Definition: KnownBits.cpp:777
KnownBits abs(bool IntMinIsPoison=false) const
Compute known bits for the absolute value.
Definition: KnownBits.cpp:556
static std::optional< bool > sle(const KnownBits &LHS, const KnownBits &RHS)
Determine if these known bits always give the same ICMP_SLE result.
Definition: KnownBits.cpp:552
static std::optional< bool > sgt(const KnownBits &LHS, const KnownBits &RHS)
Determine if these known bits always give the same ICMP_SGT result.
Definition: KnownBits.cpp:532
unsigned countMinPopulation() const
Returns the number of bits known to be one.
Definition: KnownBits.h:282
static std::optional< bool > uge(const KnownBits &LHS, const KnownBits &RHS)
Determine if these known bits always give the same ICMP_UGE result.
Definition: KnownBits.cpp:518
KnownBits & operator^=(const KnownBits &RHS)
Update known bits based on XORing with RHS.
Definition: KnownBits.cpp:1092
static KnownBits shl(const KnownBits &LHS, const KnownBits &RHS, bool NUW=false, bool NSW=false, bool ShAmtNonZero=false)
Compute known bits for shl(LHS, RHS).
Definition: KnownBits.cpp:291
static KnownBits umin(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for umin(LHS, RHS).
Definition: KnownBits.cpp:202