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

Joan Lluch via llvm-dev llvm-dev at lists.llvm.org
Tue Jun 11 03:02:56 PDT 2019


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?, 

Thanks,

John Lluch.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20190611/578ae2da/attachment.html>


More information about the llvm-dev mailing list