<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;" class="">I’d wager that the if-else chain is being converted to a "switch statement” during an optimization pass and the __builtin_expect() hint is lost. Can you file a bug? <a href="https://bugs.llvm.org" class="">https://bugs.llvm.org</a><div class=""><br class=""><div><br class=""><blockquote type="cite" class=""><div class="">On May 9, 2018, at 1:57 PM, Dávid Bolvanský via llvm-dev <<a href="mailto:llvm-dev@lists.llvm.org" class="">llvm-dev@lists.llvm.org</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><div dir="ltr" class=""><pre class="gmail-bz_comment_text" id="gmail-comment_text_3" style="white-space: pre-wrap; width: 50em;">Hello,</pre><pre class="gmail-bz_comment_text" id="gmail-comment_text_3" style="white-space: pre-wrap; width: 50em;"><div style="background-color:rgb(255,255,254)" class=""><div class=""><span style="color:rgb(0,0,255)" class="">#define</span> likely(x)       __builtin_expect((x),<span style="color:rgb(9,136,90)" class="">1</span>)</div><br class=""><div class=""><span style="color:rgb(0,128,0)" class="">// switch like</span></div><div class=""><span style="color:rgb(0,0,255)" class="">char</span> * b(<span style="color:rgb(0,0,255)" class="">int</span> e) {</div><div class="">    <span style="color:rgb(0,0,255)" class="">if</span> (likely(e == <span style="color:rgb(9,136,90)" class="">0</span>))</div><div class="">        <span style="color:rgb(0,0,255)" class="">return</span> <span style="color:rgb(163,21,21)" class="">"0"</span>;</div><div class="">    <span style="color:rgb(0,0,255)" class="">else</span> <span style="color:rgb(0,0,255)" class="">if</span> (e == <span style="color:rgb(9,136,90)" class="">1</span>)</div><div class="">        <span style="color:rgb(0,0,255)" class="">return</span> <span style="color:rgb(163,21,21)" class="">"1"</span>;</div><div class="">    <span style="color:rgb(0,0,255)" class="">else</span> <span style="color:rgb(0,0,255)" class="">return</span> <span style="color:rgb(163,21,21)" class="">"f"</span>;</div><div class="">}</div></div></pre><pre class="gmail-bz_comment_text" id="gmail-comment_text_3" style="white-space: pre-wrap; width: 50em;">GCC correctly prefers the first case:</pre><pre class="gmail-bz_comment_text" id="gmail-comment_text_3" style="white-space: pre-wrap; width: 50em;"><div style="background-color:rgb(255,255,254)" class=""><div class=""><div class=""><div class=""><span style="color:rgb(0,128,128)" class="">b(int):</span></div><div class="">  <span style="color:rgb(0,0,255)" class="">mov</span> <span style="color:rgb(72,100,170)" class="">eax</span>, <span style="color:rgb(0,128,128)" class="">OFFSET</span> <span style="color:rgb(0,128,128)" class="">FLAT</span>:<span style="color:rgb(0,128,128)" class="">.LC0</span></div><div class="">  <span style="color:rgb(0,0,255)" class="">test</span> <span style="color:rgb(72,100,170)" class="">edi</span>, <span style="color:rgb(72,100,170)" class="">edi</span></div><div class="">  <span style="color:rgb(0,0,255)" class="">jne</span> <span style="color:rgb(0,128,128)" class="">.L7</span></div><div class="">  <span style="color:rgb(0,0,255)" class="">ret</span></div></div></div><div class=""><br class=""></div></div></pre><pre class="gmail-bz_comment_text" id="gmail-comment_text_3" style="white-space: pre-wrap; width: 50em;">But Clang seems to ignore _builtin_expect hints in this case.</pre><pre class="gmail-bz_comment_text" id="gmail-comment_text_3" style="white-space: pre-wrap; width: 50em;"><div style="background-color:rgb(255,255,254)" class=""><div class=""><span style="color:rgb(0,128,128)" class="">b(int):</span> <span style="color:rgb(0,128,0)" class=""># @b(int)</span></div><div class="">  <span style="color:rgb(0,0,255)" class="">cmp</span> <span style="color:rgb(72,100,170)" class="">edi</span>, <span style="color:rgb(9,136,90)" class="">1</span></div><div class="">  <span style="color:rgb(0,0,255)" class="">mov</span> <span style="color:rgb(72,100,170)" class="">eax</span>, <span style="color:rgb(0,128,128)" class="">offset</span> <span style="color:rgb(0,128,128)" class="">.L.str.1</span></div><div class="">  <span style="color:rgb(0,0,255)" class="">mov</span> <span style="color:rgb(72,100,170)" class="">ecx</span>, <span style="color:rgb(0,128,128)" class="">offset</span> <span style="color:rgb(0,128,128)" class="">.L.str.2</span></div><div class="">  <span style="color:rgb(0,0,255)" class="">cmove</span> <span style="color:rgb(72,100,170)" class="">rcx</span>, <span style="color:rgb(72,100,170)" class="">rax</span></div><div class="">  <span style="color:rgb(0,0,255)" class="">test</span> <span style="color:rgb(72,100,170)" class="">edi</span>, <span style="color:rgb(72,100,170)" class="">edi</span></div><div class="">  <span style="color:rgb(0,0,255)" class="">mov</span> <span style="color:rgb(72,100,170)" class="">eax</span>, <span style="color:rgb(0,128,128)" class="">offset</span> <span style="color:rgb(0,128,128)" class="">.L.str</span></div><div class="">  <span style="color:rgb(0,0,255)" class="">cmovne</span> <span style="color:rgb(72,100,170)" class="">rax</span>, <span style="color:rgb(72,100,170)" class="">rcx</span></div><div class="">  <span style="color:rgb(0,0,255)" class="">ret</span></div></div></pre><pre class="gmail-bz_comment_text" id="gmail-comment_text_3" style="white-space: pre-wrap; width: 50em;"><a href="https://godbolt.org/g/tuAVT7" class="">https://godbolt.org/g/tuAVT7</a></pre></div>
_______________________________________________<br class="">LLVM Developers mailing list<br class=""><a href="mailto:llvm-dev@lists.llvm.org" class="">llvm-dev@lists.llvm.org</a><br class="">http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev<br class=""></div></blockquote></div><br class=""></div></body></html>