<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=us-ascii">
<meta name="Generator" content="Microsoft Word 15 (filtered medium)">
<!--[if !mso]><style>v\:* {behavior:url(#default#VML);}
o\:* {behavior:url(#default#VML);}
w\:* {behavior:url(#default#VML);}
.shape {behavior:url(#default#VML);}
</style><![endif]--><style><!--
/* Font Definitions */
@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:Consolas;
        panose-1:2 11 6 9 2 2 4 3 2 4;}
/* 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.Code, li.Code, div.Code
        {mso-style-name:Code;
        mso-style-link:"Code Char";
        margin:0in;
        margin-bottom:.0001pt;
        mso-add-space:auto;
        font-size:9.0pt;
        font-family:"Courier New";}
p.CodeCxSpFirst, li.CodeCxSpFirst, div.CodeCxSpFirst
        {mso-style-name:CodeCxSpFirst;
        mso-style-link:"Code Char";
        mso-style-type:export-only;
        margin:0in;
        margin-bottom:.0001pt;
        mso-add-space:auto;
        font-size:9.0pt;
        font-family:"Courier New";}
p.CodeCxSpMiddle, li.CodeCxSpMiddle, div.CodeCxSpMiddle
        {mso-style-name:CodeCxSpMiddle;
        mso-style-link:"Code Char";
        mso-style-type:export-only;
        margin:0in;
        margin-bottom:.0001pt;
        mso-add-space:auto;
        font-size:9.0pt;
        font-family:"Courier New";}
p.CodeCxSpLast, li.CodeCxSpLast, div.CodeCxSpLast
        {mso-style-name:CodeCxSpLast;
        mso-style-link:"Code Char";
        mso-style-type:export-only;
        margin:0in;
        margin-bottom:.0001pt;
        mso-add-space:auto;
        font-size:9.0pt;
        font-family:"Courier New";}
span.CodeChar
        {mso-style-name:"Code Char";
        mso-style-link:Code;
        font-family:"Courier New";}
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.EmailStyle20
        {mso-style-type:personal-compose;
        font-family:"Calibri",sans-serif;
        color:windowtext;}
.MsoChpDefault
        {mso-style-type:export-only;
        font-size:10.0pt;
        font-family:"Calibri",sans-serif;}
@page WordSection1
        {size:8.5in 11.0in;
        margin:1.0in 1.0in 1.0in 1.0in;}
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"><span style="font-size:14.0pt;font-family:Consolas">I am analyzing a clang 10.0.0 vs gcc 7.3 performance difference that I can reproduce in the following test.
<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:14.0pt"><o:p> </o:p></span></p>
<p class="MsoNormal"><b><span style="font-size:14.0pt;font-family:Consolas">unsigned</span></b><span style="font-size:14.0pt;font-family:Consolas"> foo(<b>unsigned</b> t1,
<b>unsigned</b> t2, <b>int</b> count, <b>int</b> step) {<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:14.0pt;font-family:Consolas">    <b>unsigned</b> tmp = 0;<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:14.0pt;font-family:Consolas">    <b>int</b> state = 0;<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:14.0pt;font-family:Consolas">   <b>for</b> (<b>int</b> i = 0 ; i < count ; i += step) {<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:14.0pt;font-family:Consolas">        state++;<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:14.0pt;font-family:Consolas">        <b>
if</b> (state > 5)<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:14.0pt;font-family:Consolas">            state = 0;<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:14.0pt;font-family:Consolas">        <b>
if</b> (state == 3)<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:14.0pt;font-family:Consolas">            tmp += t2;<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:14.0pt;font-family:Consolas">    }<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:14.0pt;font-family:Consolas">    <b>return</b>  tmp;<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:14.0pt;font-family:Consolas">}<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:14.0pt;font-family:Consolas"><o:p> </o:p></span></p>
<p class="MsoNormal"><span style="font-size:14.0pt;font-family:Consolas">Clang output is about 40% slower when the function is called with t2=7, count=2000000000, step=3 (t1 is unimportant in this case as it is unused here). The attached screenshot shows the
 `perf report` annotated assembly code from clang and gcc (clang is on the left). Gcc generated code takes 0.512 sec vs clang’s 0.731 sec. The machine I am running is a Broadwell… Intel(R) Xeon(R) CPU E5-2640 v4 @ 2.40GHz.<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:14.0pt;font-family:Consolas"><o:p> </o:p></span></p>
<p class="MsoNormal"><span style="font-size:14.0pt;font-family:Consolas"><o:p> </o:p></span></p>
<p class="MsoNormal"><span style="font-size:14.0pt;font-family:Consolas">The code generated by gcc runs consistently faster for all values for `step` I tried; in some cases, the performance difference is worse than 40% seen with the aforementioned parameter
 values to `foo`. The code generated by clang is a direct result of simplifycfg that eliminates the inner branches and replaces them with `select` which is then lowered to the two `cmov` instructions.
<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:14.0pt;font-family:Consolas"><o:p> </o:p></span></p>
<p class="MsoNormal"><span style="font-size:14.0pt;font-family:Consolas">The code generated by clang takes far fewer branches but executes more instructions. `perf` reports 32.76% front-end cycles idle with the clang code compared to 24.20% for gcc generated
 code. Clang generated code seems to perform worse in branch-miss and icache events (as reported by `perf`). But it is not clear why. Are the two back-to-back cmove instructions the reason? Any comments on this?<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:14.0pt;font-family:Consolas"><o:p> </o:p></span></p>
<p class="MsoNormal"><span style="font-size:14.0pt;font-family:Consolas"><o:p> </o:p></span></p>
<p class="MsoNormal"><img width="1152" height="644" style="width:12.0in;height:6.7083in" id="Picture_x0020_1" src="cid:image002.png@01D67897.72235000" alt="cid:image002.png@01D67897.72235000"><span style="font-size:14.0pt;font-family:Consolas"><o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:14.0pt;font-family:Consolas"><o:p> </o:p></span></p>
<p class="MsoNormal"><span style="font-size:14.0pt;font-family:Consolas"><o:p> </o:p></span></p>
</div>
</body>
</html>