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