<div dir="ltr"><br><div class="gmail_extra"><br><div class="gmail_quote">On Tue, Jun 21, 2016 at 12:31 AM, Matthias Braun <span dir="ltr"><<a href="mailto:matze@braunis.de" target="_blank">matze@braunis.de</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><div style="word-wrap:break-word"><div>I just discussed this with vivek on IRC (and I think we agreed on this):</div><div><br></div><div>Let me first state the motivation clearly to ease later discussions:</div><div>As far as the motivation for this change goes: Changing the calling convention allows us to choose whether a register is saved by the callee or the caller. Usually it is best to have a mix of both as too many caller saved registers leads to unnecessary save/restores when the called function turns out to only touch a fraction of the registers (as is typically for smaller leaf-functions of the call graph). While too many callee saved registers may lead to unnecessary saves/restores of registers even though the calling function didn't have a live value in the register anyway. With IPRA the first problem is mitigated since we propagate the actually clobbered set of registers up the callgraph instead of relying on conventions, so it is best to aim for more caller saved registers (though we should check for code size increases and store/restore code being potentially less good than the tuned sequences generated during FrameLowering).</div><div><br></div><div>To the disucssion at hand:</div><div>- Introducing a new calling convention at the IR level is the wrong approach: The calling convention is mostly a contract when calling and being called across translation unit boundaries. The details about how this contract is fulfilled are part of CodeGen IMO but do not need to be visible at the IR level.</div><div>- The only thing we want to influence here is which registers are saved by the callee. Changing TargetFrameLowering::determineCalleeSaves() is a good place to achieve this without affecting unrelated things like parameter and return value handling which would be part of the calling convention.</div></div></blockquote><div>Hello Matthias,</div><div><br></div><div>As per our discussion, the above trick will make sure that there is no callee saved registers and also we have thought that RegUsageInfoCalculator.cpp is having regmask that will make caller to save restore registers if both callee and caller is using any common register but this would require following change in RegUsageInfoCalculator.cpp :</div><div><br></div><div><div><font face="monospace, monospace">if (!F->hasLocalLinkage() || F->hasAddressTaken()) {</font></div><div><font face="monospace, monospace"> const uint32_t *CallPreservedMask =</font></div><div><font face="monospace, monospace"> TRI->getCallPreservedMask(MF, MF.getFunction()->getCallingConv());</font></div><div><font face="monospace, monospace"> // Set callee saved register as preserved.</font></div><div><font face="monospace, monospace"> for (unsigned i = 0; i < RegMaskSize; ++i)</font></div><div><font face="monospace, monospace"> RegMask[i] = RegMask[i] | CallPreservedMask[i];</font></div><div><font face="monospace, monospace"> }</font></div></div><div><br></div><div>because RegUsageInfoCalculator.cpp marks register as preserved if MF's CC preserves it. But While optimizing for callee saved register we need to skip above code so that register save/restore code is adder around call site.</div><div><br></div><div>Apart from that my hunch is that IPO inlining of static function also creates problem for this ( I have this feeling because some test case from test-suite fails when using -O > 0 ). I am still working on this. </div><div><br></div><div>Please share your thoughts on this.</div><div><br></div><div>Sincerely,</div><div>- Vivek</div><div> <br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><div style="word-wrap:break-word"><div>- We could experiment with dynamically changing the number of caller saved registers in the future. I could imagine heuristics like functions called from many places using some callee saved registers in order to avoid code size increases because of extra spills/restores at all the call sites. We can hardly create new calling conventions for these combinations of marking registers as callee/caller saved.</div><span class=""><font color="#888888"><div><br></div><div>- Matthias</div><div><br></div></font></span><div><blockquote type="cite"><div><div class="h5"><div>On Jun 20, 2016, at 7:39 AM, vivek pandya via llvm-dev <<a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a>> wrote:</div><br></div></div><div><div><div class="h5"><div dir="ltr">Dear Community,<div><br></div><div>To improve current interprocedural register allocation (IPRA) , we have planned to set callee saved registers to none for local functions, currently I am doing it in following way:</div><div><br></div><div><font face="monospace, monospace">if (F->hasLocalLinkage() <span style="font-size:12.499999046325684px"> </span><span style="font-size:12.499999046325684px">&& !F->hasAddressTaken()</span>) {</font></div><div><font face="monospace, monospace"> DEBUG(dbgs() << "Function has LocalLinkage \n");</font></div><div><font face="monospace, monospace">F->setCallingConv(CallingConv::GHC); </font></div><div><font face="monospace, monospace"> }</font></div><div><br></div><div>but we think threre should be clean and properway to do this perhaps like:</div><div><br></div><div><div><div style="font-size:12.499999046325684px"><font face="monospace, monospace">if (F->hasLocalLinkage() && !F->hasAddressTaken()) {</font></div><span style="font-size:12.499999046325684px"><font face="monospace, monospace"> DEBUG(dbgs() << "Function has LocalLinkage \n");</font></span><div style="font-size:12.499999046325684px"><font face="monospace, monospace"> F->setCallingConv(CallingConv::NO_Callee_Saved);</font></div><div style="font-size:12.499999046325684px"><font face="monospace, monospace"> }</font></div><div style="font-size:12.499999046325684px"><font face="monospace, monospace"><br></font></div><div><font face="arial, helvetica, sans-serif"><span style="font-size:12.499999046325684px">So I would like to know any better suggestions and if it is better to add a new CC for this purpose then what aspects should be considered while defining a new CC. Actually in this case the new CC does not really required to define how parameters should be passed or any special rule for return value etc , it just required to set callee saved registers to be none. So what are the minimal things required to define such a CC?</span></font></div></div></div><div><font face="arial, helvetica, sans-serif"><span style="font-size:12.499999046325684px"><br></span></font></div><div><font face="arial, helvetica, sans-serif"><span style="font-size:12.499999046325684px">Other alternative that I have thought was to add new attribute for function and use it like following in TargetFrameLowering::determineCalleeSaves()</span></font></div><div><font face="arial, helvetica, sans-serif"><span style="font-size:12.499999046325684px"><br></span></font></div><div><div style="font-size:12.499999046325684px"> <font face="monospace, monospace">// In Naked functions we aren't going to save any registers.</font></div><div style="font-size:12.499999046325684px"><font face="monospace, monospace"> if (MF.getFunction()->hasFnAttribute(Attribute::Naked))</font></div><div style="font-size:12.499999046325684px"><font face="monospace, monospace"> return;</font></div></div><div style="font-size:12.499999046325684px"><font face="monospace, monospace"><br></font></div><div style="font-size:12.499999046325684px"><font face="arial, helvetica, sans-serif">Any suggestions / thoughts are welcomed !</font></div><div style="font-size:12.499999046325684px"><font face="arial, helvetica, sans-serif"><br></font></div><div style="font-size:12.499999046325684px"><font face="arial, helvetica, sans-serif">Sincerely,</font></div><div style="font-size:12.499999046325684px"><font face="arial, helvetica, sans-serif">Vivek</font></div></div></div></div><span class="">
_______________________________________________<br>LLVM Developers mailing list<br><a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a><br><a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev" target="_blank">http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev</a><br></span></div></blockquote></div><br></div></blockquote></div><br></div></div>