[lld] ELF: Add branch-to-branch optimization. (PR #138366)

Peter Smith via llvm-commits llvm-commits at lists.llvm.org
Tue May 6 09:07:58 PDT 2025


https://github.com/smithp35 commented:

As a general idea this looks good to me. Some thoughts on some possible additional tests and edge-cases.

The bolt folk have been asking if the range-extension thunks can output relocations so that they can recreate the control flow more easily. It looks like this will handle emit-relocs naturally, could be worth a test case to check.

This looks like it runs before range extension thunks, which likely simplifies the logic as we don't need to check for branch range. Due to the way thunks are currently placed it could be possible that we end up removing a branch to a branch, only for the thunk generation code placing a range extension thunk such that a long-branch is generated. For example:
```
fn:
b fn2
...
fn2: // in range of fn
b fn3
...
fn3: // in range of fn2 but not fn
``` 
If we change that so that fn branches directly to fn3 our range extension thunk may not be conveniently placed, so that an indirect branch is needed.

If this is a concern for large programs it may be better to do this after range extension thunks, although it would need a range check before applying it.

Alternately running it earlier than garbage collection would in theory permit some sections (branch in its own section) to be removed, but that's likely to be a neglible saving.



https://github.com/llvm/llvm-project/pull/138366


More information about the llvm-commits mailing list