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

Finkel, Hal J. via llvm-dev llvm-dev at lists.llvm.org
Tue Jun 11 05:07:35 PDT 2019


On 6/11/19 5:02 AM, Joan Lluch via llvm-dev wrote:
A few library calls are commutable by definition, for example multiplications.

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

; Function Attrs: minsize norecurse nounwind optsize readnone
define dso_local i16 @multTest(i16 %a, i16 %b) local_unnamed_addr #0 {
entry:
  %mul = mul i16 %b, %a
  ret i16 %mul
}

is lowered as it is to a  __mulhi3  library call with the arguments reversed as shown (b,a). This is suboptimal because later on this requires an intermediate register to swap them.

SelectionDAG has 9 nodes:
  t0: ch = EntryToken
      t4: i16,ch = CopyFromReg t0, Register:i16 %1
      t2: i16,ch = CopyFromReg t0, Register:i16 %0
    t5: i16 = mul t4, t2
  t7: ch,glue = CopyToReg t0, Register:i16 $r0, t5
  t8: ch = CPU74ISD::RET_FLAG t7, Register:i16 $r0, t7:1

 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

 What am I missing?,


I don't think that you're missing anything, but I don't think that we have anything that currently allows for this in a generic way. You should be able to account for this in your target's call lowering for functions that you happen to know to be commutative, at least in cases where it's possible to recognize the source of the arguments as coming from incoming function arguments (this seems similar to how tail-call lowering works).

We do have the ability to mark instructions is commutative and the register coalescer can use that capability. The problem is that, by the time we reach that stage, the call arguments have already (likely) been lowered into copies to physical registers.

 -Hal

Thanks,

John Lluch.



_______________________________________________
LLVM Developers mailing list
llvm-dev at lists.llvm.org<mailto:llvm-dev at lists.llvm.org>
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev


--
Hal Finkel
Lead, Compiler Technology and Programming Languages
Leadership Computing Facility
Argonne National Laboratory
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20190611/b5997586/attachment.html>


More information about the llvm-dev mailing list