This is an archive of the discontinued LLVM Phabricator instance.

ARM64 - Conditional branch peephole
ClosedPublic

Authored by Gerolf on Oct 6 2014, 6:43 PM.

Details

Summary

Converts CSINC-branch sequence to single conditional branch

On ARM64 the optimization generates a single conditional branch
for csinc-branch sequences like in the examples below. This is
possible when the csinc sets or clears a register based on a condition
code and the branch checks that register. Also the condition
code may not be modified between the CINCR and the original branch.

Examples:

  1. csinc w9, wzr, wzr, <condition code> tbnz w9, #0, 0x44 to b.<inverted condition code>

    2. csinc w9, wzr, wzr, <condition code> tbz w9, #0, 0x44 to b.<condition code>

Diff Detail

Event Timeline

Gerolf updated this revision to Diff 14484.Oct 6 2014, 6:43 PM
Gerolf retitled this revision from to ARM64 - Conditional branch peephole.
Gerolf updated this object.
Gerolf edited the test plan for this revision. (Show Details)
Gerolf added a reviewer: t.p.northover.
Gerolf added a subscriber: Unknown Object (MLST).

Hi Gerolf,

Overall, the patch looks good. I think the substitution is sound, but I'd rather Tim had a look to confirm.

There are a few in-line comments that need addressing...

cheers,
--renato

lib/CodeGen/PeepholeOptimizer.cpp
506

Couldn't you just...

return TII->optimizeCondBranch(MI);

?

lib/Target/AArch64/AArch64InstrInfo.cpp
2864

Same here with comments. If you call your variables more explicitly, you don't need the one-line comment before.

Ideas are:

bool IsNegativeBranch;
bool IsTestAndBranch;
bool TargetsBBInMI;

Also, the coding standard mandates camel case for variable and function names, with caps on first letter for variables and low-caps for function names.

2868

Personal opinion, I don't like switches for this kind of thing. I think repeated ifs are more explicit to not repeat the assignments, at least it's more clear what each flag means from instructions. But I'm ok with the switch.

2877

unnecessary space

2899

I don't think it's possible for this to happen, since they're local variables and you're setting them above with an llvm_unreachable. These asserts are completely redundant.

2910

while I generally support asserts, I think you got too many of them here... It's making the rest of the code unreadable

2947

Did DefMI get inserted anywhere? I may be wrong, but I don't think this is necessary

t.p.northover edited edge metadata.Oct 9 2014, 11:19 AM

Hi Gerolf,

This looks reasonable to me, with a couple of tweaks. I agree with most of Reanto's points too.

Cheers.

Tim.

lib/Target/AArch64/AArch64InstrInfo.cpp
776

Transplanted comment is out of date.

2939–2940

We usually prefer to bail early in failure cases:

if (modifiesConditionCode(...))
  return false;
[...]
2947

It's already going to be in the basic block, isn't it? It's not one we created.

Renato and Tim thanks for your feedback. I followed up on all but the switch vs if comment.

lib/CodeGen/PeepholeOptimizer.cpp
506

done :-)

lib/Target/AArch64/AArch64InstrInfo.cpp
2864

yes, thanks.

2868

Thanks. I'm not in favor of such code either, but don't see significant improvement with the ifs. Laziness wins! :-)

2877

removed.

2899

removed.

2910

agree. Removed all but one.

2939–2940

The only reason I did this way was was to make Build happy that need a reference to MBB. But I can introduce a new variable for it and follow the preference.

2947

Yes, that line should go or be fixed: It should be dead code when MI is removed. If it is not dead code it needs an additional check that MI is the only use.

Gerolf updated this revision to Diff 14851.Oct 13 2014, 11:17 PM
Gerolf edited edge metadata.

Update based on feedback from Renato and Tim.

rengolin accepted this revision.Oct 14 2014, 1:39 PM
rengolin added a reviewer: rengolin.

Thanks Gerolf, LGTM.

This revision is now accepted and ready to land.Oct 14 2014, 1:39 PM
rengolin closed this revision.Oct 22 2014, 7:02 AM