<html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns:m="http://schemas.microsoft.com/office/2004/12/omml" xmlns="http://www.w3.org/TR/REC-html40">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="Generator" content="Microsoft Word 14 (filtered medium)">
<style><!--
/* Font Definitions */
@font-face
        {font-family:Calibri;
        panose-1:2 15 5 2 2 2 4 3 2 4;}
@font-face
        {font-family:Tahoma;
        panose-1:2 11 6 4 3 5 4 4 2 4;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
        {margin:0cm;
        margin-bottom:.0001pt;
        font-size:12.0pt;
        font-family:"Times New Roman","serif";}
a:link, span.MsoHyperlink
        {mso-style-priority:99;
        color:blue;
        text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
        {mso-style-priority:99;
        color:purple;
        text-decoration:underline;}
p
        {mso-style-priority:99;
        mso-margin-top-alt:auto;
        margin-right:0cm;
        mso-margin-bottom-alt:auto;
        margin-left:0cm;
        font-size:12.0pt;
        font-family:"Times New Roman","serif";}
p.MsoListParagraph, li.MsoListParagraph, div.MsoListParagraph
        {mso-style-priority:34;
        margin-top:0cm;
        margin-right:0cm;
        margin-bottom:0cm;
        margin-left:36.0pt;
        margin-bottom:.0001pt;
        font-size:12.0pt;
        font-family:"Times New Roman","serif";}
span.EmailStyle18
        {mso-style-type:personal-reply;
        font-family:"Calibri","sans-serif";
        color:#1F497D;}
.MsoChpDefault
        {mso-style-type:export-only;
        font-family:"Calibri","sans-serif";
        mso-fareast-language:EN-US;}
@page WordSection1
        {size:612.0pt 792.0pt;
        margin:72.0pt 72.0pt 72.0pt 72.0pt;}
div.WordSection1
        {page:WordSection1;}
--></style><!--[if gte mso 9]><xml>
<o:shapedefaults v:ext="edit" spidmax="1026" />
</xml><![endif]--><!--[if gte mso 9]><xml>
<o:shapelayout v:ext="edit">
<o:idmap v:ext="edit" data="1" />
</o:shapelayout></xml><![endif]-->
</head>
<body lang="EN-GB" link="blue" vlink="purple">
<div class="WordSection1">
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D">This is called rematerialization, and the register allocator can already do it but there are various constraints.<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D">For it happen:<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D">* The instruction needs to be marked as isReMaterializable in the tablegen file<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D">* TargetInstrInfo::isTriviallyReMaterializable needs to return true for the instruction<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D">* InlineSpiller and LiveRangeEdit in lib/Codegen are the things that actually do the rematerialization, and it<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D">    may be worthwhile looking there<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D">* The base register of the load needs to be guaranteed to be live at the place where it is rematerialized, e.g.<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D">    in lib/Target/ARM/ARMInstrThumb.td the tLDRpci instruction is rematerializable because the base in the<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D">    pc register which is always available, but loads in general aren’t.<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D"><o:p> </o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D">John<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D"><o:p> </o:p></span></p>
<div style="border:none;border-left:solid blue 1.5pt;padding:0cm 0cm 0cm 4.0pt">
<div>
<div style="border:none;border-top:solid #B5C4DF 1.0pt;padding:3.0pt 0cm 0cm 0cm">
<p class="MsoNormal"><b><span lang="EN-US" style="font-size:10.0pt;font-family:"Tahoma","sans-serif"">From:</span></b><span lang="EN-US" style="font-size:10.0pt;font-family:"Tahoma","sans-serif""> llvm-dev [mailto:llvm-dev-bounces@lists.llvm.org]
<b>On Behalf Of </b>Ehsan Amiri via llvm-dev<br>
<b>Sent:</b> 17 August 2017 17:49<br>
<b>To:</b> llvm-dev<br>
<b>Subject:</b> [llvm-dev] A problem with register allocator<o:p></o:p></span></p>
</div>
</div>
<p class="MsoNormal"><o:p> </o:p></p>
<div>
<div>
<div style="margin-top:12.0pt;margin-bottom:12.0pt">
<div>
<div>
<div>
<p><span style="font-size:10.5pt;font-family:"Arial","sans-serif"">Hi<o:p></o:p></span></p>
<p><span style="font-size:10.5pt;font-family:"Arial","sans-serif""> <o:p></o:p></span></p>
<p><span style="font-size:10.5pt;font-family:"Arial","sans-serif"">I have a code pattern like this:<o:p></o:p></span></p>
<p><span style="font-size:10.5pt;font-family:"Arial","sans-serif""> <o:p></o:p></span></p>
<p><span style="font-size:10.5pt;font-family:"Arial","sans-serif"">Load value 1 from mem<o:p></o:p></span></p>
<p><span style="font-size:10.5pt;font-family:"Arial","sans-serif"">Load value 2  from mem<o:p></o:p></span></p>
<p><span style="font-size:10.5pt;font-family:"Arial","sans-serif""> <o:p></o:p></span></p>
<p><span style="font-size:10.5pt;font-family:"Arial","sans-serif"">For (…) {<o:p></o:p></span></p>
<p><span style="font-size:10.5pt;font-family:"Arial","sans-serif"">  Use value 2<o:p></o:p></span></p>
<p><span style="font-size:10.5pt;font-family:"Arial","sans-serif"">  Use value 1<o:p></o:p></span></p>
<p><span style="font-size:10.5pt;font-family:"Arial","sans-serif"">}<o:p></o:p></span></p>
<p><span style="font-size:10.5pt;font-family:"Arial","sans-serif""> <o:p></o:p></span></p>
<p><span style="font-size:10.5pt;font-family:"Arial","sans-serif"">During register allocations values loaded are spilled and I end up with this pattern:<o:p></o:p></span></p>
<p><span style="font-size:10.5pt;font-family:"Arial","sans-serif""> <o:p></o:p></span></p>
<p><span style="font-size:10.5pt;font-family:"Arial","sans-serif"">Load value 1 from mem<o:p></o:p></span></p>
<p><span style="font-size:10.5pt;font-family:"Arial","sans-serif"">Store value 1 to stack<o:p></o:p></span></p>
<p><span style="font-size:10.5pt;font-family:"Arial","sans-serif"">Load value 2  from mem<o:p></o:p></span></p>
<p><span style="font-size:10.5pt;font-family:"Arial","sans-serif"">Store value 2 to stack<o:p></o:p></span></p>
<p><span style="font-size:10.5pt;font-family:"Arial","sans-serif""> <o:p></o:p></span></p>
<p><span style="font-size:10.5pt;font-family:"Arial","sans-serif"">For (…) {<o:p></o:p></span></p>
<p><span style="font-size:10.5pt;font-family:"Arial","sans-serif"">  Load value 2 from stack<o:p></o:p></span></p>
<p><span style="font-size:10.5pt;font-family:"Arial","sans-serif"">  Use value 2<o:p></o:p></span></p>
<p><span style="font-size:10.5pt;font-family:"Arial","sans-serif"">  Load value 1 from stack<o:p></o:p></span></p>
<p><span style="font-size:10.5pt;font-family:"Arial","sans-serif"">  Use value 1<o:p></o:p></span></p>
<p><span style="font-size:10.5pt;font-family:"Arial","sans-serif"">}<o:p></o:p></span></p>
<p><span style="font-size:10.5pt;font-family:"Arial","sans-serif""> <o:p></o:p></span></p>
<p><span style="font-size:10.5pt;font-family:"Arial","sans-serif"">At least for my case, I prefer to load directly from memory inside the loop. From what I have heard, this is a known issue with the register allocator. I have a few questions:<o:p></o:p></span></p>
<p><span style="font-size:10.5pt;font-family:"Arial","sans-serif""> <o:p></o:p></span></p>
<p style="mso-margin-top-alt:0cm;margin-right:0cm;margin-bottom:0cm;margin-left:36.0pt;margin-bottom:.0001pt">
<span style="font-size:11.0pt;font-family:"Calibri","sans-serif"">1-</span><span style="font-size:7.0pt">      </span><span style="font-size:11.0pt;font-family:"Calibri","sans-serif"">Has there been any attempt to fix this? If yes, what was the experience when
 dealing with tradeoffs (for example if value 1 and value 2 are far apart in memory and loop trip count is high, the current codegen might be better)<o:p></o:p></span></p>
<p style="mso-margin-top-alt:0cm;margin-right:0cm;margin-bottom:0cm;margin-left:36.0pt;margin-bottom:.0001pt">
<span style="font-size:11.0pt;font-family:"Calibri","sans-serif""><o:p> </o:p></span></p>
<p style="mso-margin-top-alt:0cm;margin-right:0cm;margin-bottom:0cm;margin-left:36.0pt;margin-bottom:.0001pt">
<span style="font-size:11.0pt;font-family:"Calibri","sans-serif"">2-</span><span style="font-size:7.0pt">      </span><span style="font-size:11.0pt;font-family:"Calibri","sans-serif"">If I want to experiment with some fixes for this, where in the register allocator
 code I should look?<o:p></o:p></span></p>
<p><span style="font-size:10.5pt;font-family:"Arial","sans-serif""> <o:p></o:p></span></p>
<p><span style="font-size:10.5pt;font-family:"Arial","sans-serif"">Thanks<o:p></o:p></span></p>
<p><span style="font-size:10.5pt;font-family:"Arial","sans-serif";color:#888888">Ehsan<o:p></o:p></span></p>
<p><span style="font-size:10.5pt;font-family:"Arial","sans-serif";color:#888888"> <o:p></o:p></span></p>
</div>
</div>
</div>
</div>
</div>
<div>
<p class="MsoNormal"><span style="font-size:10.5pt;font-family:"Arial","sans-serif""><o:p> </o:p></span></p>
</div>
</div>
</div>
</div>
</body>
</html>