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.
Details
Diff Detail
- Repository
- rL LLVM
Event Timeline
I can't see anything wrong with this patch, but I'm curious why visiting the later branches first makes them closer.
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.
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!