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