This is an archive of the discontinued LLVM Phabricator instance.

[Thumb] Teach ISel how to lower compares of AND bitmasks efficiently
ClosedPublic

Authored by SjoerdMeijer on Dec 14 2016, 8:55 AM.

Details

Summary

This is recommit of r285893, but with a correctness fix. The problem of the original commit was that this:

bic r5, r7, #31
cbz r5, .LBB2_10

got rewritten into:

lsrs r5, r7, #5
beq .LBB2_10

The result in destination register r5 is not the same, and this is incorrect when r5 is not dead. So the fix includes checking the uses of the AND destination register.

For completeness, this was the original commit message:

For the common pattern (CMPZ (AND x, #bitmask), #0), we can do some more efficient instruction selection if the bitmask is one consecutive sequence of set bits (32 - clz(bm) - ctz(bm) == popcount(bm)).

  1. If the bitmask touches the LSB, then we can remove all the upper bits and set the flags by doing one LSLS.
  2. If the bitmask touches the MSB, then we can remove all the lower bits and set the flags with one LSRS.
  3. If the bitmask has popcount == 1 (only one set bit), we can shift that bit into the sign bit with one LSLS and change the condition query from NE/EQ to MI/PL (we could also implement this by shifting into the carry bit and branching on BCC/BCS).
  4. Otherwise, we can emit a sequence of LSLS+LSRS to remove the upper and lower zero bits of the mask.

1-3 require only one 16-bit instruction and can elide the CMP. 4 requires two 16-bit instructions but can elide the CMP and doesn't require materializing a complex immediate, so is also a win.

Diff Detail

Repository
rL LLVM

Event Timeline

SjoerdMeijer retitled this revision from to [Thumb] Teach ISel how to lower compares of AND bitmasks efficiently.
SjoerdMeijer updated this object.
SjoerdMeijer added a subscriber: llvm-commits.
jmolloy accepted this revision.Dec 15 2016, 1:00 AM
jmolloy edited edge metadata.

LGTM, based on this code being accepted by multiple reviewers in a previous incarnation and the fix being obvious.

Thanks for fixing my broken code Sjoerd!

This revision is now accepted and ready to land.Dec 15 2016, 1:00 AM
This revision was automatically updated to reflect the committed changes.