<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=Windows-1252">
</head>
<body bgcolor="#FFFFFF" text="#000000">
<div class="moz-cite-prefix">On 6/11/19 5:02 AM, Joan Lluch via llvm-dev wrote:<br>
</div>
<blockquote type="cite" cite="mid:7EDF9E37-10D3-40B6-96CD-345898E7B85A@icloud.com">
A few library calls are commutable by definition, for example multiplications. 
<div class=""><br class="">
</div>
<div class="">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</div>
<div class=""><br class="">
</div>
<div class="">
<div style="margin: 0px; font-size: 11px; line-height: normal;
          font-family: Monaco; background-color: rgb(255, 255, 255);" class="">
; Function Attrs: minsize norecurse nounwind optsize readnone</div>
<div style="margin: 0px; font-size: 11px; line-height: normal;
          font-family: Monaco; background-color: rgb(255, 255, 255);" class="">
define dso_local i16 @multTest(i16 %a, i16 %b) local_unnamed_addr #0 {</div>
<div style="margin: 0px; font-size: 11px; line-height: normal;
          font-family: Monaco; background-color: rgb(255, 255, 255);" class="">
entry:</div>
<div style="margin: 0px; font-size: 11px; line-height: normal;
          font-family: Monaco; background-color: rgb(255, 255, 255);" class="">
  %mul = mul i16 %b, %a</div>
<div style="margin: 0px; font-size: 11px; line-height: normal;
          font-family: Monaco; background-color: rgb(255, 255, 255);" class="">
  ret i16 %mul</div>
<div style="margin: 0px; font-size: 11px; line-height: normal;
          font-family: Monaco; background-color: rgb(255, 255, 255);" class="">
}</div>
</div>
<div style="margin: 0px; font-size: 11px; line-height: normal;
        font-family: Monaco; background-color: rgb(255, 255, 255);" class="">
<br class="">
</div>
<div style="margin: 0px; line-height: normal; background-color:
        rgb(255, 255, 255);" class="">
is lowered as it is to a  <span style="font-family: Monaco; font-size: 11px;" class="">__mulhi3 </span> library call with the arguments reversed as shown (b,a). This is suboptimal because later on this requires an intermediate register to swap them.</div>
<div style="margin: 0px; line-height: normal; background-color:
        rgb(255, 255, 255);" class="">
<br class="">
</div>
<div style="margin: 0px; line-height: normal; background-color:
        rgb(255, 255, 255);" class="">
<div style="margin: 0px; font-size: 11px; line-height: normal;
          font-family: Menlo;" class="">
<span style="font-variant-ligatures: no-common-ligatures" class=""><b class="">SelectionDAG has 9 nodes:</b></span></div>
<div style="margin: 0px; font-size: 11px; line-height: normal;
          font-family: Menlo;" class="">
<span style="font-variant-ligatures: no-common-ligatures" class=""><b class="">  t0: ch = EntryToken</b></span></div>
<div style="margin: 0px; font-size: 11px; line-height: normal;
          font-family: Menlo;" class="">
<span style="font-variant-ligatures: no-common-ligatures" class=""><b class="">      t4: i16,ch = CopyFromReg t0, Register:i16 %1</b></span></div>
<div style="margin: 0px; font-size: 11px; line-height: normal;
          font-family: Menlo;" class="">
<span style="font-variant-ligatures: no-common-ligatures" class=""><b class="">      t2: i16,ch = CopyFromReg t0, Register:i16 %0</b></span></div>
<div style="margin: 0px; font-size: 11px; line-height: normal;
          font-family: Menlo;" class="">
<span style="font-variant-ligatures: no-common-ligatures" class=""><b class="">    t5: i16 = mul t4, t2</b></span></div>
<div style="margin: 0px; font-size: 11px; line-height: normal;
          font-family: Menlo;" class="">
<span style="font-variant-ligatures: no-common-ligatures" class=""><b class="">  t7: ch,glue = CopyToReg t0, Register:i16 $r0, t5</b></span></div>
<div style="margin: 0px; font-size: 11px; line-height: normal;
          font-family: Menlo;" class="">
<span style="font-variant-ligatures: no-common-ligatures" class=""><b class="">  t8: ch = CPU74ISD::RET_FLAG t7, Register:i16 $r0, t7:1</b></span></div>
</div>
<div style="margin: 0px; line-height: normal; background-color:
        rgb(255, 255, 255);" class="">
<br class="">
</div>
<div style="margin: 0px; line-height: normal; background-color:
        rgb(255, 255, 255);" class="">
 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</div>
<div style="margin: 0px; line-height: normal; background-color:
        rgb(255, 255, 255);" class="">
<br class="">
</div>
<div style="margin: 0px; line-height: normal; background-color:
        rgb(255, 255, 255);" class="">
 What am I missing?, <br>
</div>
</blockquote>
<p><br>
</p>
<p>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).</p>
<p>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.</p>
<p> -Hal<br>
</p>
<blockquote type="cite" cite="mid:7EDF9E37-10D3-40B6-96CD-345898E7B85A@icloud.com">
<div style="margin: 0px; line-height: normal; background-color:
        rgb(255, 255, 255);" class="">
<br class="">
</div>
<div style="margin: 0px; line-height: normal; background-color:
        rgb(255, 255, 255);" class="">
Thanks,</div>
<div style="margin: 0px; line-height: normal; background-color:
        rgb(255, 255, 255);" class="">
<br class="">
</div>
<div style="margin: 0px; line-height: normal; background-color:
        rgb(255, 255, 255);" class="">
John Lluch.</div>
<br>
<fieldset class="mimeAttachmentHeader"></fieldset>
<pre class="moz-quote-pre" wrap="">_______________________________________________
LLVM Developers mailing list
<a class="moz-txt-link-abbreviated" href="mailto:llvm-dev@lists.llvm.org">llvm-dev@lists.llvm.org</a>
<a class="moz-txt-link-freetext" href="https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev">https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev</a>
</pre>
</blockquote>
<pre class="moz-signature" cols="72">-- 
Hal Finkel
Lead, Compiler Technology and Programming Languages
Leadership Computing Facility
Argonne National Laboratory</pre>
</body>
</html>