[PATCH] D127801: [InstCombine] convert mask and shift of power-of-2 to cmp+select

Sanjay Patel via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Jun 14 14:45:22 PDT 2022


spatel created this revision.
spatel added reviewers: bcl5980, nikic, craig.topper, RKSimon.
Herald added subscribers: StephenFan, hiraditya, mcrosier.
Herald added a project: All.
spatel requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

When the mask is a power-of-2 constant and op0 is a shifted-power-of-2 constant, test if the shift amount equals the offset bit index:

  (ShiftC << X) & C --> X == (log2(C) - log2(ShiftC)) ? C : 0
  (ShiftC >> X) & C --> X == (log2(ShiftC) - log2(C)) ? C : 0

This is an alternate to D127610 <https://reviews.llvm.org/D127610> with a more general pattern. We match only shift+and instead of the trailing xor, so we see a few more tests diffs. I think we discussed this initially in D126617 <https://reviews.llvm.org/D126617>.

Here are proofs for shifts in both directions:
https://alive2.llvm.org/ce/z/CFrLs4

The test diffs look equal or better for IR, and this makes the patterns more uniform in IR. The backend would likely invert this in both cases if that is profitable, and there's already code in place to do that.


https://reviews.llvm.org/D127801

Files:
  llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
  llvm/test/Transforms/InstCombine/and.ll
  llvm/test/Transforms/InstCombine/icmp-and-shift.ll
  llvm/test/Transforms/InstCombine/lshr-and-signbit-icmpeq-zero.ll

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D127801.436943.patch
Type: text/x-patch
Size: 7764 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20220614/02062e5a/attachment.bin>


More information about the llvm-commits mailing list