[llvm-dev] How to tell LLVM to treat Commutable library calls as such, for example multiplication?

Tim Northover via llvm-dev llvm-dev at lists.llvm.org
Tue Jun 11 04:57:48 PDT 2019

Hi Joan,

On Tue, 11 Jun 2019 at 11:03, Joan Lluch via llvm-dev
<llvm-dev at lists.llvm.org> wrote:
> I defined them as LibCalls for my architecture. However, I found that arguments are always passed in the order they are generated by Clang thus missing possible optimisations. For example, the following IR code

Have you tried to assess the potential benefit over a larger codebase?
It looks like it could be something that comes up in trivial tests,
but disappears in larger functions where the register allocator has
more freedom.

>  Given the commutative nature of multiplication, the arguments could be passed in the same order they are received (a,b) so that they would not need to be swapped before the __mulhi3 call

It would be pretty ugly to implement during instruction selection,
given how call lowering works in LLVM. The call's registers (if a call
even puts its arguments in registers) aren't decided until you get
into LowerCall in target-specific code. You'd probably need some kind
of target-hook to answer whether it was worthwhile.

If I had my heart set on this optimization, I'd probably implement it
as a MachineIR pass after register allocation that searches upwards
from any call to __mulhi3 etc and deletes compatible COPYs.



More information about the llvm-dev mailing list