This is an archive of the discontinued LLVM Phabricator instance.

[CPG][ARM] Optimize towards branch on zero in codegenprepare
ClosedPublic

Authored by dmgreen on May 3 2021, 12:16 PM.

Details

Summary

This adds a simple fold into codegenprepare that converts comparison of branches towards comparison with zero if possible. For example:

  %c = icmp ult %x, 8
  br %c, bla, blb
  %tc = lshr %x, 3
becomes
  %tc = lshr %x, 3
  %c = icmp eq %tc, 0
  br %c, bla, blb

As a first order approximation, this can reduce the number of instructions needed to perform the branch as the shift is (often) needed anyway. At the moment this does not effect very much, as llvm tends to prefer the opposite form. But it can protect against regressions from commits like rG9423f78240a2.

Simple cases of Add and Sub are added along with Shift, equally as the comparison to zero can often be folded with cpsr flags.

Diff Detail

Event Timeline

dmgreen created this revision.May 3 2021, 12:16 PM
dmgreen requested review of this revision.May 3 2021, 12:16 PM
Herald added a project: Restricted Project. · View Herald TranscriptMay 3 2021, 12:16 PM

This should go into DAGCombine.

nikic added a subscriber: nikic.May 3 2021, 12:38 PM

@lebedev.ri This is a cross-block optimization, it cannot go into DAGCombine.

SjoerdMeijer accepted this revision.May 13 2021, 2:05 AM

Looks like a good optimisation to me. Perhaps wait a day in case others want to comment.

This revision is now accepted and ready to land.May 13 2021, 2:05 AM
nikic added inline comments.May 13 2021, 2:50 AM
llvm/lib/CodeGen/CodeGenPrepare.cpp
7710

This is going to assert for large (> 64-bit) icmps.

7731

Style nit: Cmp->isEquality()

dmgreen updated this revision to Diff 345098.May 13 2021, 5:20 AM

Thanks. I've switched it to use APInt and added a couple of extra tests.

This revision was landed with ongoing or failed builds.May 16 2021, 9:54 AM
This revision was automatically updated to reflect the committed changes.