This is an archive of the discontinued LLVM Phabricator instance.

[RISCV] Avoid redundant branch-to-branch when expanding cmpxchg
ClosedPublic

Authored by asb on Jul 20 2022, 11:52 AM.

Details

Summary

If the success value of a cmpxchg is used in a branch, the expanded cmpxchg sequence ends up with a redundant branch-to-branch (as the backend atomics expansion happens as late as possible, passes to optimise such cases have already run). This patch identifies this case and avoid it when expanding the cmpxchg.

Note that a similar optimisation is possible for a BEQ on the cmpxchg success value. As it's hard to imagine a case where real-world code may do that, this patch doens't handle that case.

Diff Detail

Event Timeline

asb created this revision.Jul 20 2022, 11:52 AM
Herald added a project: Restricted Project. · View Herald TranscriptJul 20 2022, 11:52 AM
asb requested review of this revision.Jul 20 2022, 11:52 AM
Herald added a project: Restricted Project. · View Herald TranscriptJul 20 2022, 11:52 AM
craig.topper added inline comments.Jul 24 2022, 5:04 PM
llvm/lib/Target/RISCV/RISCVExpandAtomicPseudoInsts.cpp
531

I think this is skipDebugInstructionsForward?

535

Can we avoid an IsMasked parameter by checking MaskReg.isValid()?

540

80 columns

540

Might be a little better as

if (!(ANDOp1 == DestReg && ANDOp2 == MaskReg) &&
    !(ANDOp1 == MaskReg && ANDOp2 == DestReg))

but I'm not sure.

555

80 columns

555

Same Demorgan suggestion from above

583

Register(0) should be Register()

asb updated this revision to Diff 449238.Aug 2 2022, 2:58 AM
asb marked 7 inline comments as done.

Address all review comments (thanks Craig! and aplogies for missing I hadn't clang-formatted).

This revision is now accepted and ready to land.Aug 16 2022, 5:45 PM
This revision was landed with ongoing or failed builds.Aug 17 2022, 5:49 AM
This revision was automatically updated to reflect the committed changes.