This is an archive of the discontinued LLVM Phabricator instance.

Thumb2: When applying branch optimizations, visit branches in reverse order.
ClosedPublic

Authored by pcc on Apr 21 2015, 10:11 PM.

Details

Summary

The order in which branches appear in ImmBranches is approximately their
order within the function body. By visiting later branches first, we reduce
the distance between earlier forward branches and their targets, making it
more likely that the cbn?z optimization, which can only apply to forward
branches, will succeed for those earlier branches.

Diff Detail

Repository
rL LLVM

Event Timeline

pcc updated this revision to Diff 24196.Apr 21 2015, 10:11 PM
pcc retitled this revision from to Thumb2: When applying branch optimizations, visit branches in reverse order..
pcc updated this object.
pcc edited the test plan for this revision. (Show Details)
pcc added a reviewer: t.p.northover.
pcc added a subscriber: Unknown Object (MLST).

I can't see anything wrong with this patch, but I'm curious why visiting the later branches first makes them closer.

pcc added a comment.Apr 23 2015, 12:05 PM

The test case provides a good illustration of this. In that case, we have:

branch to L if x != 0
64 bytes of instructions
branch to L if y != 0
60 bytes of instructions
L:

If we visit the first branch first, there will be 64+4+60=128 bytes of instructions between the branch and the label, which is too large for the backend to apply the cbnz optimization. However, if we visit the second branch first, the backend can apply the cbnz optimization to that branch, shrinking it to 2 bytes, and when we visit the first branch the calculation becomes 64+2+60=126, which is just small enough that the cbnz optimization can be applied.

pcc updated this object.Apr 23 2015, 12:05 PM
rengolin accepted this revision.Apr 23 2015, 12:23 PM
rengolin added a reviewer: rengolin.

Got it! So if you have multiple jumps, you'd increase the range in which the optimization could work by a few bytes, but not much more than that. And you'd have to have a long enough branch (but not longer) and multiple branches in between. Quite the corner case. :)

But since the change makes no difference (at all) in compile time and, AFAICT, semantically equivalent, LGTM, thanks!

This revision is now accepted and ready to land.Apr 23 2015, 12:23 PM
This revision was automatically updated to reflect the committed changes.