This is an archive of the discontinued LLVM Phabricator instance.

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

Authored by jmolloy on Sep 9 2016, 9:59 AM.

Details

Summary

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

jmolloy updated this revision to Diff 70854.Sep 9 2016, 9:59 AM
jmolloy retitled this revision from to [Thumb] Teach ISel how to lower compares of AND bitmasks efficiently.
jmolloy updated this object.
jmolloy set the repository for this revision to rL LLVM.
jmolloy added a subscriber: llvm-commits.

Hi James,

Looks good to me. The only question I have is if the regression tests test all the 4 different cases in function SelectCMPZ. Looking at the changed/fixed regression tests, I obviously see a lot of new "lsls", but for example I don't see any (new) LSRri occurences which is case 2) in SelectCMPZ.

Cheers.

jmolloy updated this revision to Diff 71005.Sep 12 2016, 7:22 AM

Thanks Sjoerd,

I indeed forgot to git add a testcase - thanks for spotting that!

This should be good to go now I think?

SjoerdMeijer accepted this revision.Sep 12 2016, 7:37 AM
SjoerdMeijer added a reviewer: SjoerdMeijer.

lgtm

This revision is now accepted and ready to land.Sep 12 2016, 7:37 AM
jmolloy closed this revision.Sep 12 2016, 7:39 AM

Thanks! r281215.