llvm / llvm-project

The LLVM Project is a collection of modular and reusable compiler and toolchain technologies.
http://llvm.org
Other
29.08k stars 11.99k forks source link

Re-order functions instead of in-lining #41159

Open llvmbot opened 5 years ago

llvmbot commented 5 years ago
Bugzilla Link 41814
Version trunk
OS Linux
Reporter LLVM Bugzilla Contributor
CC @dwblaikie,@pogo59

Extended Description

I am reporting this here so that I might get some technical feed-back on how this will interact with ldd.

There are many cases where function can be optimized by ordering them in a specific way. If a function is only called by a single other function in the same shared library, it should always be after the function that calls it.

There are fancier optimizations possible too, such as having memmove be both before and after memcpy, split on the branch, or jumping into the middle of a function when they do identical calculations (the function merger could probably be adapted to help with this), but this bug is only for simple re-ordering (more bugs can be opened when this is fixed).

pogo59 commented 5 years ago

Oh yes. I was wrong. Only if we can omit the ret and just branch to the other function.

Okay, for a tail-call, placing the callee immediately after the caller could have a benefit from icache etc. Thanks for clarifying!

llvmbot commented 5 years ago

The other case is what I was talking about with memmove----you can have a function wrap another function (or multiple), if the calls look like this

return call();

llvmbot commented 5 years ago

Oh yes. I was wrong. Only if we can omit the ret and just branch to the other function.

pogo59 commented 5 years ago

If a function is only called by a single other function in the same shared library, it should always be after the function that calls it.

Sorry, not following why this would be universally true, which is what you seem to be suggesting.

For a target with a big enough icache and small enough functions, I could see the icache hit being a win; but this varies depending on the target and the size of the functions.

I can see avoiding a TLB miss by putting the functions on the same memory page, but that requires only being on the same page, not relative order.

Can you explain the thinking behind "it should always be after" the caller? Thanks!