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

Nikita Popov via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Sat May 13 11:35:19 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:
> 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.


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