<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="">Hi Dávid,<div class=""><br class=""></div><div class="">Looks like you can defeat the switch conversion by adding a dummy asm(“”):<div class=""><br class=""></div><div class="">#define likely(x)       __builtin_expect((x),1)<br class=""><br class=""></div><div class="">// switch like<br class="">char * b(int e) {<br class="">    if (likely(e == 0))<br class="">        return "0";<br class="">    asm("");<br class="">    if (e == 1)<br class="">        return "1";<br class="">    else return "f";<br class="">}<br class=""><br class="">Dave<br class=""><div><br class=""><blockquote type="cite" class=""><div class="">On May 9, 2018, at 2:33 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="">I did <div class=""><a href="https://bugs.llvm.org/show_bug.cgi?id=37368" class="">https://bugs.llvm.org/show_bug.cgi?id=37368</a><br class=""></div></div><div class="gmail_extra"><br class=""><div class="gmail_quote">2018-05-09 20:33 GMT+02:00 Dávid Bolvanský <span dir="ltr" class=""><<a href="mailto:david.bolvansky@gmail.com" target="_blank" class="">david.bolvansky@gmail.com</a>></span>:<br class=""><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr" class="">I did <div class=""><br class=""></div><div class=""><a href="https://bugs.llvm.org/show_bug.cgi?id=37368" target="_blank" class="">https://bugs.llvm.org/show_<wbr class="">bug.cgi?id=37368</a><br class=""></div></div><div class="HOEnZb"><div class="h5"><div class="gmail_extra"><br class=""><div class="gmail_quote">2018-05-09 20:29 GMT+02:00 David Zarzycki <span dir="ltr" class=""><<a href="mailto:dave@znu.io" target="_blank" class="">dave@znu.io</a>></span>:<br class=""><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word;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/" target="_blank" class="">https://bugs.llvm.org</a><div class=""><br class=""><div class=""><br class=""><blockquote type="cite" class=""><div class=""><div class="m_-1029147839546973149h5"><div class="">On May 9, 2018, at 1:57 PM, Dávid Bolvanský via llvm-dev <<a href="mailto:llvm-dev@lists.llvm.org" target="_blank" class="">llvm-dev@lists.llvm.org</a>> wrote:</div><br class="m_-1029147839546973149m_-4663867852525589497Apple-interchange-newline"></div></div><div class=""><div class=""><div class="m_-1029147839546973149h5"><div dir="ltr" class=""><pre class="m_-1029147839546973149m_-4663867852525589497gmail-bz_comment_text" id="m_-1029147839546973149m_-4663867852525589497gmail-comment_text_3" style="white-space:pre-wrap;width:50em">Hello,</pre><pre class="m_-1029147839546973149m_-4663867852525589497gmail-bz_comment_text" id="m_-1029147839546973149m_-4663867852525589497gmail-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="m_-1029147839546973149m_-4663867852525589497gmail-bz_comment_text" id="m_-1029147839546973149m_-4663867852525589497gmail-comment_text_3" style="white-space:pre-wrap;width:50em">GCC correctly prefers the first case:</pre><pre class="m_-1029147839546973149m_-4663867852525589497gmail-bz_comment_text" id="m_-1029147839546973149m_-4663867852525589497gmail-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="m_-1029147839546973149m_-4663867852525589497gmail-bz_comment_text" id="m_-1029147839546973149m_-4663867852525589497gmail-comment_text_3" style="white-space:pre-wrap;width:50em">But Clang seems to ignore _builtin_expect hints in this case.</pre><pre class="m_-1029147839546973149m_-4663867852525589497gmail-bz_comment_text" id="m_-1029147839546973149m_-4663867852525589497gmail-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="m_-1029147839546973149m_-4663867852525589497gmail-bz_comment_text" id="m_-1029147839546973149m_-4663867852525589497gmail-comment_text_3" style="white-space:pre-wrap;width:50em"><a href="https://godbolt.org/g/tuAVT7" target="_blank" class="">https://godbolt.org/g/tuAVT7</a></pre></div></div></div>
______________________________<wbr class="">_________________<br class="">LLVM Developers mailing list<br class=""><a href="mailto:llvm-dev@lists.llvm.org" target="_blank" class="">llvm-dev@lists.llvm.org</a><br class=""><a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev" target="_blank" class="">http://lists.llvm.org/cgi-bin/<wbr class="">mailman/listinfo/llvm-dev</a><br class=""></div></blockquote></div><br class=""></div></div></blockquote></div><br class=""></div>
</div></div></blockquote></div><br class=""></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></div></body></html>