[PATCH] D142271: [ValueTracking] Add KnownBits patterns `xor(x, x - 1)` and `and(x, -x)` for knowing upper bits to be zero

Noah Goldstein via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon Jan 23 12:01:21 PST 2023


goldstein.w.n added inline comments.


================
Comment at: llvm/lib/Analysis/ValueTracking.cpp:1129
 
-    Known ^= Known2;
+    // Common idiom is blsmsk: xor(x, x + -1). This will clear all but lowest
+    // set bit. We can safely say any bit past the lowest known one must be
----------------
foad wrote:
> goldstein.w.n wrote:
> > craig.topper wrote:
> > > The description of blsmsk says "Sets all the lower bits of the destination operand to “1” up to and including lowest set bit (=1) in the source operand".
> > > The description of blsmsk says "Sets all the lower bits of the destination operand to “1” up to and including lowest set bit (=1) in the source operand".
> > 
> > Err, yeah should be 'clear all bits above the lowest set bit'. Will fix.
> 'clear all bits above the lowest set bit' isn't enough. It also sets all bits below the lowest set bit.
> 'clear all bits above the lowest set bit' isn't enough. It also sets all bits below the lowest set bit.

If we know all bits below the first known set bit, then yes we can full evaluate (although this is already handled by the normal constant propegation of bits through the `xor/sub`.

But if we only know a single bit, we don't know if there is a bit below it that is also set, so I don't see a way we can start setting the bits below it.

You are right though that we can always set `Known.One.setBit(0)`. In fact we can make it a bit more generic:
```
(xor (x, (sub x, OddC))) -> isOdd
```
https://alive2.llvm.org/ce/z/aaABdS

Will add that.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D142271



More information about the llvm-commits mailing list