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

Jay Foad via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon Jan 23 08:30:39 PST 2023


foad added a comment.

I suggest implementing this in new helper functions `KnownBits::blsi` and `KnownBits::blsmsk` (if we're happy with those names), plus comprehensive test coverage in `unittests/Support/KnownBitsTest.cpp`.



================
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
----------------
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.


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