This is an archive of the discontinued LLVM Phabricator instance.

Fix AArch64ConditionOptimizer
ClosedPublic

Authored by weimingz on Jan 13 2016, 11:13 AM.

Details

Summary

This pass may modify the Cmp operands. However, the flag reg may be used by both the branch and CSEL.
Modifying CMP will have side effect on CSEL.

Diff Detail

Event Timeline

weimingz updated this revision to Diff 44770.Jan 13 2016, 11:13 AM
weimingz retitled this revision from to Fix AArch64ConditionOptimizer.
weimingz updated this object.
weimingz added a reviewer: t.p.northover.
weimingz set the repository for this revision to rL LLVM.

For example:

%cmp = icmp sgt i32 %0, 0
%m = select i1 %cmp, i64 %v, i64 0
store i64 %m, i64* %p
br i1 %cmp, label %lor.lhs.false, label %land.lhs.true

will become

...

cmp w8, #1
csel x8, x0, xzr, gt // becomes x8 = (w8 > 1) ? x0 : 0
str x8, [x1]
b.lt .LBB0_2

t.p.northover added inline comments.Jan 13 2016, 12:21 PM
lib/Target/AArch64/AArch64ConditionOptimizer.cpp
177–189

This looks overly complicated to me. From the previous clause, we only care about AArch64::NZCV so I think we can skip the outer loop entirely and just look at that register.

Then we've got 2 conditions:

  • NZCV isn't live-in to a successor (can be checked before we go looking for a CMP at all).
  • NZCV isn't used before we find our CMP (can be checked outside the switch).

This has the added advantage that it'll be correct if we ever do decide to do something with the other compares, where exactly the same issues occur. The code in this particular switch case really will be issues specific to the actual CMP instructions we're seeing.

I don't think we need the special case for Bcc in the new scheme either: I is always before the first terminator, so we should never encounter a Bcc.

weimingz updated this revision to Diff 44800.Jan 13 2016, 3:17 PM
weimingz removed rL LLVM as the repository for this revision.

Thanks for reviewing, Tim.

I move the LiveIn check to runOnMachineFunction and piggyback NZCV use check into the same loop as finding CMP because it goes backwards as well.

weimingz updated this revision to Diff 44923.Jan 14 2016, 1:51 PM

fix indentions/spaces

t.p.northover added inline comments.Jan 14 2016, 3:39 PM
lib/Target/AArch64/AArch64ConditionOptimizer.cpp
357–361

Wouldn't this be better off at the top of findSuitableCompare? We wouldn't need the fairly weird logic to look through both BBs and would get the short-circuiting automatically.

weimingz updated this revision to Diff 44941.Jan 14 2016, 3:58 PM

you're right :D

t.p.northover accepted this revision.Jan 14 2016, 4:01 PM
t.p.northover edited edge metadata.

Thanks Weiming! I think this looks good now.

Tim.

This revision is now accepted and ready to land.Jan 14 2016, 4:01 PM
weimingz closed this revision.Jan 14 2016, 4:10 PM