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 14 #include "llvm/Support/KnownBits.h" 15 #include <cassert> 16 17 using namespace llvm; 18 19 static KnownBits computeForAddCarry( 20 const KnownBits &LHS, const KnownBits &RHS, 21 bool CarryZero, bool CarryOne) { 22 assert(!(CarryZero && CarryOne) && 23 "Carry can't be zero and one at the same time"); 24 25 APInt PossibleSumZero = LHS.getMaxValue() + RHS.getMaxValue() + !CarryZero; 26 APInt PossibleSumOne = LHS.getMinValue() + RHS.getMinValue() + CarryOne; 27 28 // Compute known bits of the carry. 29 APInt CarryKnownZero = ~(PossibleSumZero ^ LHS.Zero ^ RHS.Zero); 30 APInt CarryKnownOne = PossibleSumOne ^ LHS.One ^ RHS.One; 31 32 // Compute set of known bits (where all three relevant bits are known). 33 APInt LHSKnownUnion = LHS.Zero | LHS.One; 34 APInt RHSKnownUnion = RHS.Zero | RHS.One; 35 APInt CarryKnownUnion = std::move(CarryKnownZero) | CarryKnownOne; 36 APInt Known = std::move(LHSKnownUnion) & RHSKnownUnion & CarryKnownUnion; 37 38 assert((PossibleSumZero & Known) == (PossibleSumOne & Known) && 39 "known bits of sum differ"); 40 41 // Compute known bits of the result. 42 KnownBits KnownOut; 43 KnownOut.Zero = ~std::move(PossibleSumZero) & Known; 44 KnownOut.One = std::move(PossibleSumOne) & Known; 45 return KnownOut; 46 } 47 48 KnownBits KnownBits::computeForAddCarry( 49 const KnownBits &LHS, const KnownBits &RHS, const KnownBits &Carry) { 50 assert(Carry.getBitWidth() == 1 && "Carry must be 1-bit"); 51 return ::computeForAddCarry( 52 LHS, RHS, Carry.Zero.getBoolValue(), Carry.One.getBoolValue()); 53 } 54 55 KnownBits KnownBits::computeForAddSub(bool Add, bool NSW, 56 const KnownBits &LHS, KnownBits RHS) { 57 KnownBits KnownOut; 58 if (Add) { 59 // Sum = LHS + RHS + 0 60 KnownOut = ::computeForAddCarry( 61 LHS, RHS, /*CarryZero*/true, /*CarryOne*/false); 62 } else { 63 // Sum = LHS + ~RHS + 1 64 std::swap(RHS.Zero, RHS.One); 65 KnownOut = ::computeForAddCarry( 66 LHS, RHS, /*CarryZero*/false, /*CarryOne*/true); 67 } 68 69 // Are we still trying to solve for the sign bit? 70 if (!KnownOut.isNegative() && !KnownOut.isNonNegative()) { 71 if (NSW) { 72 // Adding two non-negative numbers, or subtracting a negative number from 73 // a non-negative one, can't wrap into negative. 74 if (LHS.isNonNegative() && RHS.isNonNegative()) 75 KnownOut.makeNonNegative(); 76 // Adding two negative numbers, or subtracting a non-negative number from 77 // a negative one, can't wrap into non-negative. 78 else if (LHS.isNegative() && RHS.isNegative()) 79 KnownOut.makeNegative(); 80 } 81 } 82 83 return KnownOut; 84 } 85 86 KnownBits KnownBits::sextInReg(unsigned SrcBitWidth) const { 87 unsigned BitWidth = getBitWidth(); 88 assert(0 < SrcBitWidth && SrcBitWidth <= BitWidth && 89 "Illegal sext-in-register"); 90 91 if (SrcBitWidth == BitWidth) 92 return *this; 93 94 unsigned ExtBits = BitWidth - SrcBitWidth; 95 KnownBits Result; 96 Result.One = One << ExtBits; 97 Result.Zero = Zero << ExtBits; 98 Result.One.ashrInPlace(ExtBits); 99 Result.Zero.ashrInPlace(ExtBits); 100 return Result; 101 } 102 103 KnownBits KnownBits::makeGE(const APInt &Val) const { 104 // Count the number of leading bit positions where our underlying value is 105 // known to be less than or equal to Val. 106 unsigned N = (Zero | Val).countLeadingOnes(); 107 108 // For each of those bit positions, if Val has a 1 in that bit then our 109 // underlying value must also have a 1. 110 APInt MaskedVal(Val); 111 MaskedVal.clearLowBits(getBitWidth() - N); 112 return KnownBits(Zero, One | MaskedVal); 113 } 114 115 KnownBits KnownBits::umax(const KnownBits &LHS, const KnownBits &RHS) { 116 // If we can prove that LHS >= RHS then use LHS as the result. Likewise for 117 // RHS. Ideally our caller would already have spotted these cases and 118 // optimized away the umax operation, but we handle them here for 119 // completeness. 120 if (LHS.getMinValue().uge(RHS.getMaxValue())) 121 return LHS; 122 if (RHS.getMinValue().uge(LHS.getMaxValue())) 123 return RHS; 124 125 // If the result of the umax is LHS then it must be greater than or equal to 126 // the minimum possible value of RHS. Likewise for RHS. Any known bits that 127 // are common to these two values are also known in the result. 128 KnownBits L = LHS.makeGE(RHS.getMinValue()); 129 KnownBits R = RHS.makeGE(LHS.getMinValue()); 130 return KnownBits::commonBits(L, R); 131 } 132 133 KnownBits KnownBits::umin(const KnownBits &LHS, const KnownBits &RHS) { 134 // Flip the range of values: [0, 0xFFFFFFFF] <-> [0xFFFFFFFF, 0] 135 auto Flip = [](const KnownBits &Val) { return KnownBits(Val.One, Val.Zero); }; 136 return Flip(umax(Flip(LHS), Flip(RHS))); 137 } 138 139 KnownBits KnownBits::smax(const KnownBits &LHS, const KnownBits &RHS) { 140 // Flip the range of values: [-0x80000000, 0x7FFFFFFF] <-> [0, 0xFFFFFFFF] 141 auto Flip = [](const KnownBits &Val) { 142 unsigned SignBitPosition = Val.getBitWidth() - 1; 143 APInt Zero = Val.Zero; 144 APInt One = Val.One; 145 Zero.setBitVal(SignBitPosition, Val.One[SignBitPosition]); 146 One.setBitVal(SignBitPosition, Val.Zero[SignBitPosition]); 147 return KnownBits(Zero, One); 148 }; 149 return Flip(umax(Flip(LHS), Flip(RHS))); 150 } 151 152 KnownBits KnownBits::smin(const KnownBits &LHS, const KnownBits &RHS) { 153 // Flip the range of values: [-0x80000000, 0x7FFFFFFF] <-> [0xFFFFFFFF, 0] 154 auto Flip = [](const KnownBits &Val) { 155 unsigned SignBitPosition = Val.getBitWidth() - 1; 156 APInt Zero = Val.One; 157 APInt One = Val.Zero; 158 Zero.setBitVal(SignBitPosition, Val.Zero[SignBitPosition]); 159 One.setBitVal(SignBitPosition, Val.One[SignBitPosition]); 160 return KnownBits(Zero, One); 161 }; 162 return Flip(umax(Flip(LHS), Flip(RHS))); 163 } 164 165 KnownBits KnownBits::shl(const KnownBits &LHS, const KnownBits &RHS) { 166 unsigned BitWidth = LHS.getBitWidth(); 167 KnownBits Known(BitWidth); 168 169 // If the shift amount is a valid constant then transform LHS directly. 170 if (RHS.isConstant() && RHS.getConstant().ult(BitWidth)) { 171 unsigned Shift = RHS.getConstant().getZExtValue(); 172 Known = LHS; 173 Known.Zero <<= Shift; 174 Known.One <<= Shift; 175 // Low bits are known zero. 176 Known.Zero.setLowBits(Shift); 177 return Known; 178 } 179 180 // No matter the shift amount, the trailing zeros will stay zero. 181 unsigned MinTrailingZeros = LHS.countMinTrailingZeros(); 182 183 // Minimum shift amount low bits are known zero. 184 APInt MinShiftAmount = RHS.getMinValue(); 185 if (MinShiftAmount.ult(BitWidth)) { 186 MinTrailingZeros += MinShiftAmount.getZExtValue(); 187 MinTrailingZeros = std::min(MinTrailingZeros, BitWidth); 188 } 189 190 // If the maximum shift is in range, then find the common bits from all 191 // possible shifts. 192 APInt MaxShiftAmount = RHS.getMaxValue(); 193 if (MaxShiftAmount.ult(BitWidth) && !LHS.isUnknown()) { 194 uint64_t ShiftAmtZeroMask = (~RHS.Zero).getZExtValue(); 195 uint64_t ShiftAmtOneMask = RHS.One.getZExtValue(); 196 assert(MinShiftAmount.ult(MaxShiftAmount) && "Illegal shift range"); 197 Known.Zero.setAllBits(); 198 Known.One.setAllBits(); 199 for (uint64_t ShiftAmt = MinShiftAmount.getZExtValue(), 200 MaxShiftAmt = MaxShiftAmount.getZExtValue(); 201 ShiftAmt <= MaxShiftAmt; ++ShiftAmt) { 202 // Skip if the shift amount is impossible. 203 if ((ShiftAmtZeroMask & ShiftAmt) != ShiftAmt || 204 (ShiftAmtOneMask | ShiftAmt) != ShiftAmt) 205 continue; 206 KnownBits SpecificShift; 207 SpecificShift.Zero = LHS.Zero << ShiftAmt; 208 SpecificShift.One = LHS.One << ShiftAmt; 209 Known = KnownBits::commonBits(Known, SpecificShift); 210 if (Known.isUnknown()) 211 break; 212 } 213 } 214 215 Known.Zero.setLowBits(MinTrailingZeros); 216 return Known; 217 } 218 219 KnownBits KnownBits::lshr(const KnownBits &LHS, const KnownBits &RHS) { 220 unsigned BitWidth = LHS.getBitWidth(); 221 KnownBits Known(BitWidth); 222 223 if (RHS.isConstant() && RHS.getConstant().ult(BitWidth)) { 224 unsigned Shift = RHS.getConstant().getZExtValue(); 225 Known = LHS; 226 Known.Zero.lshrInPlace(Shift); 227 Known.One.lshrInPlace(Shift); 228 // High bits are known zero. 229 Known.Zero.setHighBits(Shift); 230 return Known; 231 } 232 233 // No matter the shift amount, the leading zeros will stay zero. 234 unsigned MinLeadingZeros = LHS.countMinLeadingZeros(); 235 236 // Minimum shift amount high bits are known zero. 237 APInt MinShiftAmount = RHS.getMinValue(); 238 if (MinShiftAmount.ult(BitWidth)) { 239 MinLeadingZeros += MinShiftAmount.getZExtValue(); 240 MinLeadingZeros = std::min(MinLeadingZeros, BitWidth); 241 } 242 243 // If the maximum shift is in range, then find the common bits from all 244 // possible shifts. 245 APInt MaxShiftAmount = RHS.getMaxValue(); 246 if (MaxShiftAmount.ult(BitWidth) && !LHS.isUnknown()) { 247 uint64_t ShiftAmtZeroMask = (~RHS.Zero).getZExtValue(); 248 uint64_t ShiftAmtOneMask = RHS.One.getZExtValue(); 249 assert(MinShiftAmount.ult(MaxShiftAmount) && "Illegal shift range"); 250 Known.Zero.setAllBits(); 251 Known.One.setAllBits(); 252 for (uint64_t ShiftAmt = MinShiftAmount.getZExtValue(), 253 MaxShiftAmt = MaxShiftAmount.getZExtValue(); 254 ShiftAmt <= MaxShiftAmt; ++ShiftAmt) { 255 // Skip if the shift amount is impossible. 256 if ((ShiftAmtZeroMask & ShiftAmt) != ShiftAmt || 257 (ShiftAmtOneMask | ShiftAmt) != ShiftAmt) 258 continue; 259 KnownBits SpecificShift = LHS; 260 SpecificShift.Zero.lshrInPlace(ShiftAmt); 261 SpecificShift.One.lshrInPlace(ShiftAmt); 262 Known = KnownBits::commonBits(Known, SpecificShift); 263 if (Known.isUnknown()) 264 break; 265 } 266 } 267 268 Known.Zero.setHighBits(MinLeadingZeros); 269 return Known; 270 } 271 272 KnownBits KnownBits::ashr(const KnownBits &LHS, const KnownBits &RHS) { 273 unsigned BitWidth = LHS.getBitWidth(); 274 KnownBits Known(BitWidth); 275 276 if (RHS.isConstant() && RHS.getConstant().ult(BitWidth)) { 277 unsigned Shift = RHS.getConstant().getZExtValue(); 278 Known = LHS; 279 Known.Zero.ashrInPlace(Shift); 280 Known.One.ashrInPlace(Shift); 281 return Known; 282 } 283 284 // No matter the shift amount, the leading sign bits will stay. 285 unsigned MinLeadingZeros = LHS.countMinLeadingZeros(); 286 unsigned MinLeadingOnes = LHS.countMinLeadingOnes(); 287 288 // Minimum shift amount high bits are known sign bits. 289 APInt MinShiftAmount = RHS.getMinValue(); 290 if (MinShiftAmount.ult(BitWidth)) { 291 if (MinLeadingZeros) { 292 MinLeadingZeros += MinShiftAmount.getZExtValue(); 293 MinLeadingZeros = std::min(MinLeadingZeros, BitWidth); 294 } 295 if (MinLeadingOnes) { 296 MinLeadingOnes += MinShiftAmount.getZExtValue(); 297 MinLeadingOnes = std::min(MinLeadingOnes, BitWidth); 298 } 299 } 300 301 // If the maximum shift is in range, then find the common bits from all 302 // possible shifts. 303 APInt MaxShiftAmount = RHS.getMaxValue(); 304 if (MaxShiftAmount.ult(BitWidth) && !LHS.isUnknown()) { 305 uint64_t ShiftAmtZeroMask = (~RHS.Zero).getZExtValue(); 306 uint64_t ShiftAmtOneMask = RHS.One.getZExtValue(); 307 assert(MinShiftAmount.ult(MaxShiftAmount) && "Illegal shift range"); 308 Known.Zero.setAllBits(); 309 Known.One.setAllBits(); 310 for (uint64_t ShiftAmt = MinShiftAmount.getZExtValue(), 311 MaxShiftAmt = MaxShiftAmount.getZExtValue(); 312 ShiftAmt <= MaxShiftAmt; ++ShiftAmt) { 313 // Skip if the shift amount is impossible. 314 if ((ShiftAmtZeroMask & ShiftAmt) != ShiftAmt || 315 (ShiftAmtOneMask | ShiftAmt) != ShiftAmt) 316 continue; 317 KnownBits SpecificShift = LHS; 318 SpecificShift.Zero.ashrInPlace(ShiftAmt); 319 SpecificShift.One.ashrInPlace(ShiftAmt); 320 Known = KnownBits::commonBits(Known, SpecificShift); 321 if (Known.isUnknown()) 322 break; 323 } 324 } 325 326 Known.Zero.setHighBits(MinLeadingZeros); 327 Known.One.setHighBits(MinLeadingOnes); 328 return Known; 329 } 330 331 Optional<bool> KnownBits::eq(const KnownBits &LHS, const KnownBits &RHS) { 332 if (LHS.isConstant() && RHS.isConstant()) 333 return Optional<bool>(LHS.getConstant() == RHS.getConstant()); 334 if (LHS.One.intersects(RHS.Zero) || RHS.One.intersects(LHS.Zero)) 335 return Optional<bool>(false); 336 return None; 337 } 338 339 Optional<bool> KnownBits::ne(const KnownBits &LHS, const KnownBits &RHS) { 340 if (Optional<bool> KnownEQ = eq(LHS, RHS)) 341 return Optional<bool>(!KnownEQ.getValue()); 342 return None; 343 } 344 345 Optional<bool> KnownBits::ugt(const KnownBits &LHS, const KnownBits &RHS) { 346 // LHS >u RHS -> false if umax(LHS) <= umax(RHS) 347 if (LHS.getMaxValue().ule(RHS.getMinValue())) 348 return Optional<bool>(false); 349 // LHS >u RHS -> true if umin(LHS) > umax(RHS) 350 if (LHS.getMinValue().ugt(RHS.getMaxValue())) 351 return Optional<bool>(true); 352 return None; 353 } 354 355 Optional<bool> KnownBits::uge(const KnownBits &LHS, const KnownBits &RHS) { 356 if (Optional<bool> IsUGT = ugt(RHS, LHS)) 357 return Optional<bool>(!IsUGT.getValue()); 358 return None; 359 } 360 361 Optional<bool> KnownBits::ult(const KnownBits &LHS, const KnownBits &RHS) { 362 return ugt(RHS, LHS); 363 } 364 365 Optional<bool> KnownBits::ule(const KnownBits &LHS, const KnownBits &RHS) { 366 return uge(RHS, LHS); 367 } 368 369 Optional<bool> KnownBits::sgt(const KnownBits &LHS, const KnownBits &RHS) { 370 // LHS >s RHS -> false if smax(LHS) <= smax(RHS) 371 if (LHS.getSignedMaxValue().sle(RHS.getSignedMinValue())) 372 return Optional<bool>(false); 373 // LHS >s RHS -> true if smin(LHS) > smax(RHS) 374 if (LHS.getSignedMinValue().sgt(RHS.getSignedMaxValue())) 375 return Optional<bool>(true); 376 return None; 377 } 378 379 Optional<bool> KnownBits::sge(const KnownBits &LHS, const KnownBits &RHS) { 380 if (Optional<bool> KnownSGT = sgt(RHS, LHS)) 381 return Optional<bool>(!KnownSGT.getValue()); 382 return None; 383 } 384 385 Optional<bool> KnownBits::slt(const KnownBits &LHS, const KnownBits &RHS) { 386 return sgt(RHS, LHS); 387 } 388 389 Optional<bool> KnownBits::sle(const KnownBits &LHS, const KnownBits &RHS) { 390 return sge(RHS, LHS); 391 } 392 393 KnownBits KnownBits::abs(bool IntMinIsPoison) const { 394 // If the source's MSB is zero then we know the rest of the bits already. 395 if (isNonNegative()) 396 return *this; 397 398 // Absolute value preserves trailing zero count. 399 KnownBits KnownAbs(getBitWidth()); 400 KnownAbs.Zero.setLowBits(countMinTrailingZeros()); 401 402 // We only know that the absolute values's MSB will be zero if INT_MIN is 403 // poison, or there is a set bit that isn't the sign bit (otherwise it could 404 // be INT_MIN). 405 if (IntMinIsPoison || (!One.isNullValue() && !One.isMinSignedValue())) 406 KnownAbs.Zero.setSignBit(); 407 408 // FIXME: Handle known negative input? 409 // FIXME: Calculate the negated Known bits and combine them? 410 return KnownAbs; 411 } 412 413 KnownBits KnownBits::computeForMul(const KnownBits &LHS, const KnownBits &RHS) { 414 unsigned BitWidth = LHS.getBitWidth(); 415 416 assert(!LHS.hasConflict() && !RHS.hasConflict()); 417 // Compute a conservative estimate for high known-0 bits. 418 unsigned LeadZ = 419 std::max(LHS.countMinLeadingZeros() + RHS.countMinLeadingZeros(), 420 BitWidth) - 421 BitWidth; 422 LeadZ = std::min(LeadZ, BitWidth); 423 424 // The result of the bottom bits of an integer multiply can be 425 // inferred by looking at the bottom bits of both operands and 426 // multiplying them together. 427 // We can infer at least the minimum number of known trailing bits 428 // of both operands. Depending on number of trailing zeros, we can 429 // infer more bits, because (a*b) <=> ((a/m) * (b/n)) * (m*n) assuming 430 // a and b are divisible by m and n respectively. 431 // We then calculate how many of those bits are inferrable and set 432 // the output. For example, the i8 mul: 433 // a = XXXX1100 (12) 434 // b = XXXX1110 (14) 435 // We know the bottom 3 bits are zero since the first can be divided by 436 // 4 and the second by 2, thus having ((12/4) * (14/2)) * (2*4). 437 // Applying the multiplication to the trimmed arguments gets: 438 // XX11 (3) 439 // X111 (7) 440 // ------- 441 // XX11 442 // XX11 443 // XX11 444 // XX11 445 // ------- 446 // XXXXX01 447 // Which allows us to infer the 2 LSBs. Since we're multiplying the result 448 // by 8, the bottom 3 bits will be 0, so we can infer a total of 5 bits. 449 // The proof for this can be described as: 450 // Pre: (C1 >= 0) && (C1 < (1 << C5)) && (C2 >= 0) && (C2 < (1 << C6)) && 451 // (C7 == (1 << (umin(countTrailingZeros(C1), C5) + 452 // umin(countTrailingZeros(C2), C6) + 453 // umin(C5 - umin(countTrailingZeros(C1), C5), 454 // C6 - umin(countTrailingZeros(C2), C6)))) - 1) 455 // %aa = shl i8 %a, C5 456 // %bb = shl i8 %b, C6 457 // %aaa = or i8 %aa, C1 458 // %bbb = or i8 %bb, C2 459 // %mul = mul i8 %aaa, %bbb 460 // %mask = and i8 %mul, C7 461 // => 462 // %mask = i8 ((C1*C2)&C7) 463 // Where C5, C6 describe the known bits of %a, %b 464 // C1, C2 describe the known bottom bits of %a, %b. 465 // C7 describes the mask of the known bits of the result. 466 const APInt &Bottom0 = LHS.One; 467 const APInt &Bottom1 = RHS.One; 468 469 // How many times we'd be able to divide each argument by 2 (shr by 1). 470 // This gives us the number of trailing zeros on the multiplication result. 471 unsigned TrailBitsKnown0 = (LHS.Zero | LHS.One).countTrailingOnes(); 472 unsigned TrailBitsKnown1 = (RHS.Zero | RHS.One).countTrailingOnes(); 473 unsigned TrailZero0 = LHS.countMinTrailingZeros(); 474 unsigned TrailZero1 = RHS.countMinTrailingZeros(); 475 unsigned TrailZ = TrailZero0 + TrailZero1; 476 477 // Figure out the fewest known-bits operand. 478 unsigned SmallestOperand = 479 std::min(TrailBitsKnown0 - TrailZero0, TrailBitsKnown1 - TrailZero1); 480 unsigned ResultBitsKnown = std::min(SmallestOperand + TrailZ, BitWidth); 481 482 APInt BottomKnown = 483 Bottom0.getLoBits(TrailBitsKnown0) * Bottom1.getLoBits(TrailBitsKnown1); 484 485 KnownBits Res(BitWidth); 486 Res.Zero.setHighBits(LeadZ); 487 Res.Zero |= (~BottomKnown).getLoBits(ResultBitsKnown); 488 Res.One = BottomKnown.getLoBits(ResultBitsKnown); 489 return Res; 490 } 491 492 KnownBits KnownBits::udiv(const KnownBits &LHS, const KnownBits &RHS) { 493 unsigned BitWidth = LHS.getBitWidth(); 494 assert(!LHS.hasConflict() && !RHS.hasConflict()); 495 KnownBits Known(BitWidth); 496 497 // For the purposes of computing leading zeros we can conservatively 498 // treat a udiv as a logical right shift by the power of 2 known to 499 // be less than the denominator. 500 unsigned LeadZ = LHS.countMinLeadingZeros(); 501 unsigned RHSMaxLeadingZeros = RHS.countMaxLeadingZeros(); 502 503 if (RHSMaxLeadingZeros != BitWidth) 504 LeadZ = std::min(BitWidth, LeadZ + BitWidth - RHSMaxLeadingZeros - 1); 505 506 Known.Zero.setHighBits(LeadZ); 507 return Known; 508 } 509 510 KnownBits KnownBits::urem(const KnownBits &LHS, const KnownBits &RHS) { 511 unsigned BitWidth = LHS.getBitWidth(); 512 assert(!LHS.hasConflict() && !RHS.hasConflict()); 513 KnownBits Known(BitWidth); 514 515 if (RHS.isConstant() && RHS.getConstant().isPowerOf2()) { 516 // The upper bits are all zero, the lower ones are unchanged. 517 APInt LowBits = RHS.getConstant() - 1; 518 Known.Zero = LHS.Zero | ~LowBits; 519 Known.One = LHS.One & LowBits; 520 return Known; 521 } 522 523 // Since the result is less than or equal to either operand, any leading 524 // zero bits in either operand must also exist in the result. 525 uint32_t Leaders = 526 std::max(LHS.countMinLeadingZeros(), RHS.countMinLeadingZeros()); 527 Known.Zero.setHighBits(Leaders); 528 return Known; 529 } 530 531 KnownBits KnownBits::srem(const KnownBits &LHS, const KnownBits &RHS) { 532 unsigned BitWidth = LHS.getBitWidth(); 533 assert(!LHS.hasConflict() && !RHS.hasConflict()); 534 KnownBits Known(BitWidth); 535 536 if (RHS.isConstant() && RHS.getConstant().isPowerOf2()) { 537 // The low bits of the first operand are unchanged by the srem. 538 APInt LowBits = RHS.getConstant() - 1; 539 Known.Zero = LHS.Zero & LowBits; 540 Known.One = LHS.One & LowBits; 541 542 // If the first operand is non-negative or has all low bits zero, then 543 // the upper bits are all zero. 544 if (LHS.isNonNegative() || LowBits.isSubsetOf(LHS.Zero)) 545 Known.Zero |= ~LowBits; 546 547 // If the first operand is negative and not all low bits are zero, then 548 // the upper bits are all one. 549 if (LHS.isNegative() && LowBits.intersects(LHS.One)) 550 Known.One |= ~LowBits; 551 return Known; 552 } 553 554 // The sign bit is the LHS's sign bit, except when the result of the 555 // remainder is zero. The magnitude of the result should be less than or 556 // equal to the magnitude of the LHS. Therefore any leading zeros that exist 557 // in the left hand side must also exist in the result. 558 Known.Zero.setHighBits(LHS.countMinLeadingZeros()); 559 return Known; 560 } 561 562 KnownBits &KnownBits::operator&=(const KnownBits &RHS) { 563 // Result bit is 0 if either operand bit is 0. 564 Zero |= RHS.Zero; 565 // Result bit is 1 if both operand bits are 1. 566 One &= RHS.One; 567 return *this; 568 } 569 570 KnownBits &KnownBits::operator|=(const KnownBits &RHS) { 571 // Result bit is 0 if both operand bits are 0. 572 Zero &= RHS.Zero; 573 // Result bit is 1 if either operand bit is 1. 574 One |= RHS.One; 575 return *this; 576 } 577 578 KnownBits &KnownBits::operator^=(const KnownBits &RHS) { 579 // Result bit is 0 if both operand bits are 0 or both are 1. 580 APInt Z = (Zero & RHS.Zero) | (One & RHS.One); 581 // Result bit is 1 if one operand bit is 0 and the other is 1. 582 One = (Zero & RHS.One) | (One & RHS.Zero); 583 Zero = std::move(Z); 584 return *this; 585 } 586