[Thumb] Teach ISel how to lower compares of AND bitmasks efficiently
authorJames Molloy <james.molloy@arm.com>
Tue, 13 Sep 2016 12:12:32 +0000 (12:12 +0000)
committerJames Molloy <james.molloy@arm.com>
Tue, 13 Sep 2016 12:12:32 +0000 (12:12 +0000)
commitd246c598deef73bdd751c822cb847c0caa3a3ffb
tree3135bccaed5b2653dbf5ac954b562ba0bc327952
parent214f7bf5cc2aa7b6bb98ca2d5d79ae1f67c6b88d
[Thumb] Teach ISel how to lower compares of AND bitmasks efficiently

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.

llvm-svn: 281323
llvm/lib/Target/ARM/ARMBaseInstrInfo.cpp
llvm/lib/Target/ARM/ARMISelDAGToDAG.cpp
llvm/test/CodeGen/ARM/and-cmpz.ll [new file with mode: 0644]
llvm/test/CodeGen/ARM/arm-and-tst-peephole.ll
llvm/test/CodeGen/ARM/arm-shrink-wrapping.ll
llvm/test/CodeGen/ARM/call-tc.ll
llvm/test/CodeGen/ARM/debug-info-branch-folding.ll
llvm/test/CodeGen/Thumb/thumb-shrink-wrapping.ll
llvm/test/CodeGen/Thumb2/float-ops.ll