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