[InstCombine] Shift amount reassociation in bittest: trunc-of-lshr (PR42399)
commit14e53307a033978cd2e10255681c9e7a3502b3a6
authorRoman Lebedev <lebedev.ri@gmail.com>
Thu, 29 Aug 2019 10:26:23 +0000 (29 10:26 +0000)
committerRoman Lebedev <lebedev.ri@gmail.com>
Thu, 29 Aug 2019 10:26:23 +0000 (29 10:26 +0000)
treedc0a64c5f653fd0f7329488e819fbb18c6d81b3c
parent532d56f1c1e5b4effc651c78c1c5958e3082359b
[InstCombine] Shift amount reassociation in bittest: trunc-of-lshr (PR42399)

Summary:
Finally, the fold i was looking forward to :)

The legality check is muddy, i doubt  i've groked the full generalization,
but it handles all the cases i care about, and can come up with:
https://rise4fun.com/Alive/26j

I.e. we can perform the fold if **any** of the following is true:
* The shift amount is either zero or one less than widest bitwidth
* Either of the values being shifted has at most lowest bit set
* The value that is being shifted by `shl` (which is not truncated) should have no less leading zeros than the total shift amount;
* The value that is being shifted by `lshr` (which **is** truncated) should have no less leading zeros than the widest bit width minus total shift amount minus one

I strongly suspect there is some better generalization, but i'm not aware of it as of right now.
For now i also avoided using actual `computeKnownBits()`, but restricted it to constants.

Reviewers: spatel, nikic, xbolva00

Reviewed By: spatel

Subscribers: hiraditya, llvm-commits

Tags: #llvm

Differential Revision: https://reviews.llvm.org/D66383

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@370324 91177308-0d34-0410-b5e6-96231b3b80d8
lib/Transforms/InstCombine/InstCombineCompares.cpp
test/Transforms/InstCombine/shift-amount-reassociation-in-bittest-with-truncation-lshr.ll
test/Transforms/InstCombine/shift-amount-reassociation-in-bittest-with-truncation-shl.ll