<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 15 (filtered medium)">
<style><!--
/* Font Definitions */
@font-face
{font-family:Helvetica;
panose-1:2 11 6 4 2 2 2 2 2 4;}
@font-face
{font-family:"Cambria Math";
panose-1:2 4 5 3 5 4 6 3 2 4;}
@font-face
{font-family:Calibri;
panose-1:2 15 5 2 2 2 4 3 2 4;}
@font-face
{font-family:Monaco;
panose-1:0 0 0 0 0 0 0 0 0 0;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
{margin:0in;
margin-bottom:.0001pt;
font-size:11.0pt;
font-family:"Calibri",sans-serif;}
a:link, span.MsoHyperlink
{mso-style-priority:99;
color:#0563C1;
text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
{mso-style-priority:99;
color:#954F72;
text-decoration:underline;}
p.msonormal0, li.msonormal0, div.msonormal0
{mso-style-name:msonormal;
mso-margin-top-alt:auto;
margin-right:0in;
mso-margin-bottom-alt:auto;
margin-left:0in;
font-size:11.0pt;
font-family:"Calibri",sans-serif;}
span.EmailStyle18
{mso-style-type:personal-reply;
font-family:"Calibri",sans-serif;
color:windowtext;}
.MsoChpDefault
{mso-style-type:export-only;
font-size:10.0pt;}
@page WordSection1
{size:8.5in 11.0in;
margin:56.7pt 42.5pt 56.7pt 85.05pt;}
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-US" link="#0563C1" vlink="#954F72">
<div class="WordSection1">
<p class="MsoNormal">Hi Joan,<o:p></o:p></p>
<p class="MsoNormal">One knob you might want to adjust is TargetTransformInfo::getUserCost which is used by ComputeSpeculationCost in SimplifyCFG to determine whether it’s cheap to speculate some instruction or not.<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<div>
<p class="MsoNormal">--<o:p></o:p></p>
<p class="MsoNormal">Danila<o:p></o:p></p>
</div>
<p class="MsoNormal"><o:p> </o:p></p>
<div style="border:none;border-left:solid blue 1.5pt;padding:0in 0in 0in 4.0pt">
<div>
<div style="border:none;border-top:solid #E1E1E1 1.0pt;padding:3.0pt 0in 0in 0in">
<p class="MsoNormal"><b>From:</b> llvm-dev [mailto:llvm-dev-bounces@lists.llvm.org]
<b>On Behalf Of </b>Joan Lluch via llvm-dev<br>
<b>Sent:</b> Saturday, September 21, 2019 15:06<br>
<b>To:</b> llvm-dev <llvm-dev@lists.llvm.org><br>
<b>Subject:</b> [llvm-dev] CFG simplification question, and preservation of branching in the original code<o:p></o:p></p>
</div>
</div>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">Hi all,<o:p></o:p></p>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal">For my custom architecture, I want to relax the CFG simplification pass, and other passes replacing conditional branches.<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal">I found that the replacement of conditional jumps by “select" or other instructions is often too aggressive, and this causes inefficient code for my target, because in most cases a branch would just be cheaper.<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal">For example, considering the following c code:<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<div>
<p class="MsoNormal" style="background:white"><span style="font-size:8.5pt;font-family:"Monaco",serif;color:#BA2DA2">long</span><span style="font-size:8.5pt;font-family:"Monaco",serif"> test (<span style="color:#BA2DA2">long</span> a,
<span style="color:#BA2DA2">long</span> b)<o:p></o:p></span></p>
</div>
<div>
<p class="MsoNormal" style="background:white"><span style="font-size:8.5pt;font-family:"Monaco",serif">{<o:p></o:p></span></p>
</div>
<div>
<p class="MsoNormal" style="background:white"><span style="font-size:8.5pt;font-family:"Monaco",serif">
<span style="color:#BA2DA2">int</span> neg = <span style="color:#272AD8">0</span>;<o:p></o:p></span></p>
</div>
<div>
<p class="MsoNormal" style="background:white"><span style="font-size:8.5pt;font-family:"Monaco",serif">
<span style="color:#BA2DA2">long</span> res;<o:p></o:p></span></p>
</div>
<div>
<p class="MsoNormal" style="background:white"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal" style="background:white"><span style="font-size:8.5pt;font-family:"Monaco",serif">
<span style="color:#BA2DA2">if</span> (a < <span style="color:#272AD8">0</span>) <o:p></o:p></span></p>
</div>
<div>
<p class="MsoNormal" style="background:white"><span style="font-size:8.5pt;font-family:"Monaco",serif"> {<o:p></o:p></span></p>
</div>
<div>
<p class="MsoNormal" style="background:white"><span style="font-size:8.5pt;font-family:"Monaco",serif"> a = -a; <o:p></o:p></span></p>
</div>
<div>
<p class="MsoNormal" style="background:white"><span style="font-size:8.5pt;font-family:"Monaco",serif"> neg =
<span style="color:#272AD8">1</span>;<o:p></o:p></span></p>
</div>
<div>
<p class="MsoNormal" style="background:white"><span style="font-size:8.5pt;font-family:"Monaco",serif"> }<o:p></o:p></span></p>
</div>
<div>
<p class="MsoNormal" style="background:white"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal" style="background:white"><span style="font-size:8.5pt;font-family:"Monaco",serif;color:black"> res = a*b;</span><span style="font-size:8.5pt;font-family:"Monaco",serif;color:#008400"><o:p></o:p></span></p>
</div>
<div>
<p class="MsoNormal" style="background:white"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal" style="background:white"><span style="font-size:8.5pt;font-family:"Monaco",serif">
<span style="color:#BA2DA2">if</span> (neg)<o:p></o:p></span></p>
</div>
<div>
<p class="MsoNormal" style="background:white"><span style="font-size:8.5pt;font-family:"Monaco",serif"> res = -res;<o:p></o:p></span></p>
</div>
<div>
<p class="MsoNormal" style="background:white"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal" style="background:white"><span style="font-size:8.5pt;font-family:"Monaco",serif">
<span style="color:#BA2DA2">return</span> res;<o:p></o:p></span></p>
</div>
<div>
<p class="MsoNormal" style="background:white"><span style="font-size:8.5pt;font-family:"Monaco",serif">}<o:p></o:p></span></p>
</div>
</div>
<div>
<p class="MsoNormal" style="background:white"><span style="font-size:8.5pt;font-family:"Monaco",serif"><o:p> </o:p></span></p>
</div>
<div>
<p class="MsoNormal" style="background:white"><span style="font-size:8.5pt;font-family:"Monaco",serif"><o:p> </o:p></span></p>
</div>
<div>
<p class="MsoNormal" style="background:white"><span style="font-size:9.0pt;font-family:"Helvetica",sans-serif">This code can be obviously simplified in c, but please just consider it as an example to show the point.</span><span style="font-size:8.5pt;font-family:"Monaco",serif"><o:p></o:p></span></p>
</div>
<div>
<p class="MsoNormal" style="background:white"><span style="font-size:8.5pt;font-family:"Monaco",serif"><o:p> </o:p></span></p>
</div>
<div>
<p class="MsoNormal" style="background:white"><span style="font-size:9.0pt;font-family:"Helvetica",sans-serif">The code above gets compiled like this (-Oz flag):</span><span style="font-size:8.5pt;font-family:"Monaco",serif"><o:p></o:p></span></p>
</div>
<div>
<p class="MsoNormal" style="background:white"><span style="font-size:8.5pt;font-family:"Monaco",serif"><o:p> </o:p></span></p>
</div>
<div>
<div>
<div>
<p class="MsoNormal" style="background:white"><span style="font-size:8.5pt;font-family:"Monaco",serif">; Function Attrs: minsize norecurse nounwind optsize readnone<o:p></o:p></span></p>
</div>
<div>
<p class="MsoNormal" style="background:white"><span style="font-size:8.5pt;font-family:"Monaco",serif">define dso_local i32 @test(i32 %a, i32 %b) local_unnamed_addr #0 {<o:p></o:p></span></p>
</div>
<div>
<p class="MsoNormal" style="background:white"><span style="font-size:8.5pt;font-family:"Monaco",serif">entry:<o:p></o:p></span></p>
</div>
<div>
<p class="MsoNormal" style="background:white"><span style="font-size:8.5pt;font-family:"Monaco",serif"> %cmp = icmp slt i32 %a, 0<o:p></o:p></span></p>
</div>
<div>
<p class="MsoNormal" style="background:white"><span style="font-size:8.5pt;font-family:"Monaco",serif"> %sub = sub nsw i32 0, %a<o:p></o:p></span></p>
</div>
<div>
<p class="MsoNormal" style="background:white"><span style="font-size:8.5pt;font-family:"Monaco",serif"> %a.addr.0 = select i1 %cmp, i32 %sub, i32 %a<o:p></o:p></span></p>
</div>
<div>
<p class="MsoNormal" style="background:white"><span style="font-size:8.5pt;font-family:"Monaco",serif"> %mul = mul nsw i32 %a.addr.0, %b<o:p></o:p></span></p>
</div>
<div>
<p class="MsoNormal" style="background:white"><span style="font-size:8.5pt;font-family:"Monaco",serif"> %sub2 = sub nsw i32 0, %mul<o:p></o:p></span></p>
</div>
<div>
<p class="MsoNormal" style="background:white"><span style="font-size:8.5pt;font-family:"Monaco",serif"> %res.0 = select i1 %cmp, i32 %sub2, i32 %mul<o:p></o:p></span></p>
</div>
<div>
<p class="MsoNormal" style="background:white"><span style="font-size:8.5pt;font-family:"Monaco",serif"> ret i32 %res.0<o:p></o:p></span></p>
</div>
<div>
<p class="MsoNormal" style="background:white"><span style="font-size:8.5pt;font-family:"Monaco",serif">}<o:p></o:p></span></p>
</div>
<div>
<p class="MsoNormal" style="background:white"><span style="font-size:8.5pt;font-family:"Monaco",serif"><o:p> </o:p></span></p>
</div>
<div>
<p class="MsoNormal" style="background:white"><span style="font-size:8.5pt;font-family:"Monaco",serif"><o:p> </o:p></span></p>
</div>
<div>
<p class="MsoNormal" style="background:white">All branching was removed and replaced by ‘select’ instructions. Unfortunately, 32 bit operations are expensive on my architecture and in most cases it would be desirable to just keep the original branches, which
are relatively cheap. The case above could be converted back to branching by the backend, but for the general case, this is not always practical and misses other optimisation opportunities.<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal" style="background:white"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal" style="background:white">I tried to set 'phi-node-folding-threshold’ to 1 or even 0, and this definitely improves the situation in many cases, but Clang still creates instances of ‘select’ instructions, which are detrimental to my target.
I am unsure about where are they created, as I believe that the simplifycfg pass does not longer create them. <o:p></o:p></p>
</div>
<div>
<p class="MsoNormal" style="background:white"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal" style="background:white">So the question is: Are there any other hooks in clang, or custom code that I can implement, to relax the creation of ’select’ instructions as opposed to preserving branches in the original c code?<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal" style="background:white"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal" style="background:white">Thanks,<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal" style="background:white"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal" style="background:white">John<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal" style="background:white"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal" style="background:white"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal" style="background:white"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal" style="background:white"><o:p> </o:p></p>
</div>
</div>
</div>
</div>
</div>
</body>
</html>