]> granicus.if.org Git - llvm/commit
[InstCombine] canonicalize check for power-of-2
authorSanjay Patel <spatel@rotateright.com>
Thu, 20 Jun 2019 17:41:15 +0000 (17:41 +0000)
committerSanjay Patel <spatel@rotateright.com>
Thu, 20 Jun 2019 17:41:15 +0000 (17:41 +0000)
commit0e1f2dd70807df8671baf1393524d0bd00dd847a
tree10e3ea80008ff0813d53fe27b0023e63b83b7642
parentfea914288b48bc8dc173e43d85f8dc194a8674c7
[InstCombine] canonicalize check for power-of-2

The form that compares against 0 is better because:
1. It removes a use of the input value.
2. It's the more standard form for this pattern: https://graphics.stanford.edu/~seander/bithacks.html#DetermineIfPowerOf2
3. It results in equal or better codegen (tested with x86, AArch64, ARM, PowerPC, MIPS).

This is a root cause for PR42314, but probably doesn't completely answer the codegen request:
https://bugs.llvm.org/show_bug.cgi?id=42314

Alive proof:
https://rise4fun.com/Alive/9kG

  Name: is power-of-2
  %neg = sub i32 0, %x
  %a = and i32 %neg, %x
  %r = icmp eq i32 %a, %x
  =>
  %dec = add i32 %x, -1
  %a2 = and i32 %dec, %x
  %r = icmp eq i32 %a2, 0

  Name: is not power-of-2
  %neg = sub i32 0, %x
  %a = and i32 %neg, %x
  %r = icmp ne i32 %a, %x
  =>
  %dec = add i32 %x, -1
  %a2 = and i32 %dec, %x
  %r = icmp ne i32 %a2, 0

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@363956 91177308-0d34-0410-b5e6-96231b3b80d8
lib/Transforms/InstCombine/InstCombineCompares.cpp
test/Transforms/InstCombine/ispow2.ll