[PATCH] D150100: [KnownBits] Improve implementation of `KnownBits::abs`

Nikita Popov via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Sat May 13 12:58:52 PDT 2023


nikic added inline comments.


================
Comment at: llvm/lib/Support/KnownBits.cpp:423
+    KnownBits KnownNeg = computeForAddSub(
+        /*Add*/ false, /*NSW*/ false, Zero, *this);
+
----------------
goldstein.w.n wrote:
> goldstein.w.n wrote:
> > nikic wrote:
> > > goldstein.w.n wrote:
> > > > nikic wrote:
> > > > > goldstein.w.n wrote:
> > > > > > nikic wrote:
> > > > > > > You can pass IntMinIsPoison to the NSW argument.
> > > > > > > You can pass IntMinIsPoison to the NSW argument.
> > > > > > 
> > > > > > Can we? The `0-Negative_X` is positive unless `Negative_X == INT_MIN` so if `IntMinIsPoison` we can't set `NSW`. Also would do slightly better and just do `KnownAbs.isNonNegative()` because it uses the `IntMinIsPoison` condition (and others) to try and set output sign.
> > > > > > 
> > > > > > But either way, since `KnownAbs` already has logic for setting output sign and we combine our result from negation with `KnownAbs` its kind of a non-issue.
> > > > > I don't understand. The only value for which `0-X` wraps is INT_MIN, which is exactly what the IntMinIsPoison flag controls. The entire purpose of that flag is to allow an `nsw` assumption on the negation.
> > > > Oh I mistakenly though something like `sub nsw i8 0, 129` would be `poison` but guess not.
> > > > Either way though, we 'join' with the original `abs` value to get the signbit so `nsw` is unneeded no?
> > > But can we drop that join if NSW is passed? Just making this `if (isNegative(X)) return neg(X);` would make this logic a lot cleaner.
> > > But can we drop that join if NSW is passed? Just making this `if (isNegative(X)) return neg(X);` would make this logic a lot cleaner.
> > 
> > I doesn't seem to cover all cases:
> > 
> > ```
> >     KnownBits Zero(getBitWidth());
> >     Zero.setAllZero();
> > 
> >     KnownBits KnownNeg = computeForAddSub(
> >         /*Add*/ false, KnownAbs.isNegative(), Zero, *this);
> > 
> >     // Preserve signbit from `KnownAbs` as it has additional logic for figuring
> >     // it out that we don't want to duplicate here.
> >     KnownBits KnownAbsNoSign = KnownAbs;
> >     KnownBits KnownNegNoSign = KnownNeg;
> >     KnownAbsNoSign.One.clearSignBit();
> >     KnownAbsNoSign.Zero.clearSignBit();
> >     KnownNegNoSign.One.clearSignBit();
> >     KnownNegNoSign.Zero.clearSignBit();
> > 
> >     assert(KnownAbsNoSign.One.isSubsetOf(KnownNegNoSign.One));
> >     assert(KnownAbsNoSign.Zero.isSubsetOf(KnownNegNoSign.Zero));
> >     
> >     assert(KnownAbs.One.isSubsetOf(KnownNeg.One));
> >     assert(KnownAbs.Zero.isSubsetOf(KnownNeg.Zero));
> > 
> >     KnownNeg.One.clearSignBit();
> >     KnownNeg.Zero.clearSignBit();
> >     KnownAbs.One |= KnownNeg.One;
> >     KnownAbs.Zero |= KnownNeg.Zero;
> > ```
> > 
> > On the exhaustive tests fails:
> > 
> > ```
> > KnownBits.cpp:440: llvm::KnownBits llvm::KnownBits::abs(bool) const: Assertion `KnownAbs.Zero.isSubsetOf(KnownNeg.Zero)' failed.
> > ```
> > > But can we drop that join if NSW is passed? Just making this `if (isNegative(X)) return neg(X);` would make this logic a lot cleaner.
> > 
> > I doesn't seem to cover all cases:
> > 
> > ```
> >     KnownBits Zero(getBitWidth());
> >     Zero.setAllZero();
> > 
> >     KnownBits KnownNeg = computeForAddSub(
> >         /*Add*/ false, KnownAbs.isNegative(), Zero, *this);
> 
> This should be `isNonNegative()` but still fails the assertion.
> > 
> >     // Preserve signbit from `KnownAbs` as it has additional logic for figuring
> >     // it out that we don't want to duplicate here.
> >     KnownBits KnownAbsNoSign = KnownAbs;
> >     KnownBits KnownNegNoSign = KnownNeg;
> >     KnownAbsNoSign.One.clearSignBit();
> >     KnownAbsNoSign.Zero.clearSignBit();
> >     KnownNegNoSign.One.clearSignBit();
> >     KnownNegNoSign.Zero.clearSignBit();
> > 
> >     assert(KnownAbsNoSign.One.isSubsetOf(KnownNegNoSign.One));
> >     assert(KnownAbsNoSign.Zero.isSubsetOf(KnownNegNoSign.Zero));
> >     
> >     assert(KnownAbs.One.isSubsetOf(KnownNeg.One));
> >     assert(KnownAbs.Zero.isSubsetOf(KnownNeg.Zero));
> > 
> >     KnownNeg.One.clearSignBit();
> >     KnownNeg.Zero.clearSignBit();
> >     KnownAbs.One |= KnownNeg.One;
> >     KnownAbs.Zero |= KnownNeg.Zero;
> > ```
> > 
> > On the exhaustive tests fails:
> > 
> > ```
> > KnownBits.cpp:440: llvm::KnownBits llvm::KnownBits::abs(bool) const: Assertion `KnownAbs.Zero.isSubsetOf(KnownNeg.Zero)' failed.
> > ```
> 
> 
The argument to the NSW flag should be IntMinIsPoison, not KnownAbs.isNegative(). Effectively you're assuming that the flag is always set, so I would expect that to cause exhaustive test failures.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D150100/new/

https://reviews.llvm.org/D150100



More information about the llvm-commits mailing list