[PATCH] D63004: [TargetLowering] Simplify (ctpop x) == 1

Dávid Bolvanský via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri Jun 7 10:21:32 PDT 2019


xbolva00 added a comment.

In D63004#1534334 <https://reviews.llvm.org/D63004#1534334>, @lebedev.ri wrote:

> In D63004#1534270 <https://reviews.llvm.org/D63004#1534270>, @xbolva00 wrote:
>
> > Or should we prefer  (x-1) < (x & -x) ? This seems like a much faster choice.
>
>
> I'm guessing lack of CTPOP also likely implies lack of BMI1? (`(x & x-1)` is a single `blsr` instruction)
>  While that second variant looks like it has simpler ir, the ask looks more complex in general, more arithmetic:
>  https://godbolt.org/z/iwIIKK


Also maybe (x << __builtin_clz (x)) == INT_MIN

So with -O3 we can have:

  bsr     ecx, edi
  xor     ecx, 31
  sal     edi, cl
  cmp     edi, -2147483648
  sete    al
  ret

But this looks slower in practise (see my previous post with small "benchmark" code example).


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

https://reviews.llvm.org/D63004





More information about the llvm-commits mailing list