This is an archive of the discontinued LLVM Phabricator instance.

[PowerPC] eliminate redundant compare instruction
ClosedPublic

Authored by inouehrs on Aug 28 2017, 4:55 AM.

Details

Summary

If multiple conditional branches are executed based on the same comparison, we can execute multiple conditional branches based on the result of one comparison on PPC. For example,

if (a == 0) { ... }
else if (a < 0) { ... }

can be executed by one compare and two conditional branches instead of two pairs of a compare and a conditional branch.

This patch identifies a code sequence of the two pairs of a compare and a conditional branch and merge the compares if possible.
To maximize the opportunity, we do canonicalization of code sequence before merging compares.
For the above example, the input for this pass looks like:

cmplwi r3, 0
beq    0, .LBB0_3
cmpwi  r3, -1
bgt    0, .LBB0_4

So, before merging two compares, we canonicalize it as

cmpwi  r3, 0       ; cmplwi and cmpwi yield same result for beq
beq    0, .LBB0_3
cmpwi  r3, 0       ; greather than -1 means greater or equal to 0
bge    0, .LBB0_4

The generated code should be

cmpwi  r3, 0
beq    0, .LBB0_3
bge    0, .LBB0_4

Diff Detail

Repository
rL LLVM

Event Timeline

inouehrs created this revision.Aug 28 2017, 4:55 AM
hfinkel added inline comments.Sep 2 2017, 4:44 PM
lib/Target/PowerPC/PPCMIPeephole.cpp
436 ↗(On Diff #112884)

I think you can also handle BCCLR and BCA. BCA doesn't come up much, but we can have BCCLR in loops where this might be helpful.

465 ↗(On Diff #112884)

modify -> modifies the

527 ↗(On Diff #112884)

I think that you can run into trouble if these are physical registers (similar to the problem recently uncovered with PPCBranchCoalescing. This will work for virtual registers, but not for physical registers, unless MRI->isConstantPhysReg or TRI->isCallerPreservedPhysReg is true, or you scan the instructions in the first BB to make sure that nothing changes the register.

537 ↗(On Diff #112884)

I don't think you need this switch, but can use:

NewPredicate2 = (unsigned)PPC::getSwappedPredicate(Pred);
558 ↗(On Diff #112884)

See comment above re: if these are physical registers.

571 ↗(On Diff #112884)

Local variables should start with a capital letter.

inouehrs updated this revision to Diff 113751.Sep 4 2017, 7:22 AM

updated based on Hal's suggestions.

inouehrs updated this revision to Diff 113753.Sep 4 2017, 7:37 AM
inouehrs marked 2 inline comments as done.
inouehrs updated this revision to Diff 113754.Sep 4 2017, 7:42 AM
inouehrs marked 2 inline comments as done.
inouehrs marked 2 inline comments as done.
inouehrs added inline comments.
lib/Target/PowerPC/PPCMIPeephole.cpp
436 ↗(On Diff #112884)

I added PPC::BCCLR as a opcode to optimize. Also I added a test case that generates bgelr.
(In many case, BCCLR is not used at the time of PPC MI Peephole since BCCLR is typically generated afterward at If Converter.)

527 ↗(On Diff #112884)

I added checks of physical registers in eligibleForCompareElimination.

537 ↗(On Diff #112884)

I modified with getSwappedPredicate.

hfinkel accepted this revision.Sep 4 2017, 9:34 AM

One comment below about BCCLR, but otherwise, this LGTM.

lib/Target/PowerPC/PPCMIPeephole.cpp
436 ↗(On Diff #112884)

Ah, you're right. Those won't ever appear until later in the pipeline. I don't want dead code here. Please remove the check for BCCLR, in that case, and instead add a comment. Maybe something like:

// We check only for BCC here, not BCCLR, because BCCLR will be formed only later in the pipeline.

This way, if we ever change how this is done, someone will grep for BCCLR and find this. Thanks for adding the test case.

This revision is now accepted and ready to land.Sep 4 2017, 9:34 AM
This revision was automatically updated to reflect the committed changes.