[llvm] [KnownBits] Make nuw and nsw support in computeForAddSub optimal (PR #83382)
Jay Foad via llvm-commits
llvm-commits at lists.llvm.org
Fri Mar 1 03:16:46 PST 2024
================
@@ -54,34 +54,184 @@ KnownBits KnownBits::computeForAddCarry(
LHS, RHS, Carry.Zero.getBoolValue(), Carry.One.getBoolValue());
}
-KnownBits KnownBits::computeForAddSub(bool Add, bool NSW,
+KnownBits KnownBits::computeForAddSub(bool Add, bool NSW, bool NUW,
const KnownBits &LHS, KnownBits RHS) {
KnownBits KnownOut;
if (Add) {
// Sum = LHS + RHS + 0
- KnownOut = ::computeForAddCarry(
- LHS, RHS, /*CarryZero*/true, /*CarryOne*/false);
+ KnownOut =
+ ::computeForAddCarry(LHS, RHS, /*CarryZero*/ true, /*CarryOne*/ false);
} else {
// Sum = LHS + ~RHS + 1
- std::swap(RHS.Zero, RHS.One);
- KnownOut = ::computeForAddCarry(
- LHS, RHS, /*CarryZero*/false, /*CarryOne*/true);
+ KnownBits NotRHS = RHS;
+ std::swap(NotRHS.Zero, NotRHS.One);
+ KnownOut = ::computeForAddCarry(LHS, NotRHS, /*CarryZero*/ false,
+ /*CarryOne*/ true);
}
+ if (!NSW && !NUW)
+ return KnownOut;
- // Are we still trying to solve for the sign bit?
- if (!KnownOut.isNegative() && !KnownOut.isNonNegative()) {
+ // We truncate out the signbit during nsw handling so just handle this special
+ // case to avoid dealing with it later.
+ if (LHS.getBitWidth() == 1) {
+ return LHS | RHS;
----------------
jayfoad wrote:
If I set `Bits = 1` in the unit test, it fails.
https://github.com/llvm/llvm-project/pull/83382
More information about the llvm-commits
mailing list