This is an archive of the discontinued LLVM Phabricator instance.

[PowerPC] Exploit rldimi for ori with large immediates
ClosedPublic

Authored by qiucf on Apr 9 2020, 7:28 PM.

Details

Summary

This patch exploits rldimi instruction for patterns like or %a, 0b000011110000, which saves number of instructions in some cases, compared with li-ori-sldi-or.

Diff Detail

Event Timeline

qiucf created this revision.Apr 9 2020, 7:28 PM
Herald added a project: Restricted Project. · View Herald TranscriptApr 9 2020, 7:28 PM
nemanjai requested changes to this revision.Apr 10 2020, 5:03 AM

This is a good idea, thanks for working on it. Just needs a slight tweak.

llvm/lib/Target/PowerPC/PPCISelDAGToDAG.cpp
4607

s/sequent/consecutive
(in other places as well)

4611

There are probably some more conditions under which you don't want to do this. What comes to mind is checking for a single use of the input to the OR (since rldimi is destructive) and that the output is not used in a SETCC to compare against zero.

Here's a test case:

long test(long a, long b) {
  return ((a | 4294967296L) > 0) ? a : b;
}

Prior to this patch, the codegen is:

li 5, 1
sldi 5, 5, 32
or. 5, 3, 5
isel 3, 3, 4, 1

With this patch, the codegen is:

li 5, -1
mr 6, 3
rldimi. 6, 5, 32, 31
isel 3, 3, 4, 1

At first glance, the new codegen looks no worse. However it is worse for two reasons:

  1. Increased register pressure to move the value due to rldimi being destructive
  2. Record-form rotate instead of record-form or is worse because the former is a two-way cracked instruction.
This revision now requires changes to proceed.Apr 10 2020, 5:03 AM
qiucf marked 2 inline comments as done.Apr 15 2020, 3:00 AM
qiucf added inline comments.
llvm/lib/Target/PowerPC/PPCISelDAGToDAG.cpp
4611

Thanks for pointing this out. I'm not sure about: after checking the input has only one use, is it still profitable to use or. instead of rldimi. for comparison against zero? Like:

long test(long a, long b, long c) {
  return ((a | 4294967296L) > 0) ? c : b;
}

# Original result
li 6, 1
sldi 6, 6, 32
or. 3, 3, 6
isel 3, 5, 4, 1
blr

# After patch
li 6, -1
rldimi. 3, 6, 32, 31
isel 3, 5, 4, 1
blr
qiucf updated this revision to Diff 257957.Apr 15 2020, 10:18 PM

Check uses of first operand

nemanjai accepted this revision.Apr 16 2020, 3:39 AM

LGTM. Please add a test case where the input value has multiple uses for a test of where we don't do this even when the constant is conducive to doing it. I don't think such an addition requires another review.

llvm/lib/Target/PowerPC/PPCISelDAGToDAG.cpp
4611

Well, we could check something like N->use_begin()->getOpcode() == ISD::SETCC, but I don't think it is necessary. The cracked rldimi. is no worse than the increased path length of the or. sequence.

This revision is now accepted and ready to land.Apr 16 2020, 3:39 AM
This revision was automatically updated to reflect the committed changes.