<div dir="ltr">The x86 backend is extremely aggressive in turning cmov with memory operands into branches because that is often faster even for poorly predicted branches due to the forced stall in the cmov.</div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Fri, Sep 13, 2019 at 11:19 PM David Zarzycki <<a href="mailto:dave@znu.io">dave@znu.io</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">I’m struggling to find cases where __builtin_unpredictable() works at all. Even if we ignore cmp/br into switch conversion, it still doesn’t work:<br>
<br>
int test_cmov(int left, int right, int *alt) {<br>
        return __builtin_unpredictable(left < right) ? *alt : 999;<br>
}<br>
<br>
Should generate:<br>
<br>
test_cmov:<br>
        movl    $999, %eax<br>
        cmpl    %esi, %edi<br>
        cmovll  (%rdx), %eax<br>
        retq<br>
<br>
But currently generates:<br>
<br>
test_cmov:<br>
        movl    $999, %eax<br>
        cmpl    %esi, %edi<br>
        jge     .LBB0_2<br>
        movl    (%rdx), %eax<br>
.LBB0_2:<br>
        retq<br>
<br>
<br>
<br>
> On Sep 14, 2019, at 12:18 AM, Sanjay Patel <<a href="mailto:spatel@rotateright.com" target="_blank">spatel@rotateright.com</a>> wrote:<br>
> <br>
> I'm not sure if this is the entire problem, but SimplifyCFG loses the 'unpredictable' metadata when it converts a set of cmp/br into a switch:<br>
> <a href="https://godbolt.org/z/neLzN3" rel="noreferrer" target="_blank">https://godbolt.org/z/neLzN3</a><br>
> <br>
> Filed here:<br>
> <a href="https://bugs.llvm.org/show_bug.cgi?id=43313" rel="noreferrer" target="_blank">https://bugs.llvm.org/show_bug.cgi?id=43313</a><br>
> <br>
> On Fri, Sep 13, 2019 at 4:02 AM David Zarzycki via llvm-dev <<a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a>> wrote:<br>
> <br>
> <br>
>> On Sep 13, 2019, at 10:45 AM, Chandler Carruth <<a href="mailto:chandlerc@gmail.com" target="_blank">chandlerc@gmail.com</a>> wrote:<br>
>> <br>
>> On Fri, Sep 13, 2019 at 1:33 AM David Zarzycki via llvm-dev <<a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a>> wrote:<br>
>> Hi Chandler,<br>
>> <br>
>> The data-invariant feature sounds great but what about the general case? When performance tuning code, people sometimes need the ability to reliably generate CMOV, and right now the best advice is either “use inline assembly” or “keep refactoring until CMOV is emited” (and hope that future compilers continue to generate CMOV).<br>
>> <br>
>> Given that a patch already exists to reliably generate CMOV, are there any good arguments against adding the feature?<br>
>> <br>
>> For *performance* tuning, the builtin that Hal mentioned is IMO the correct design.<br>
>> <br>
>> Is there some reason why it doesn't work?<br>
> <br>
> I wasn’t aware of __builtin_unpredictable() until now and I haven’t debugged why it doesn’t work, but here are a couple examples, one using the ternary operator and one using a switch statement:<br>
> <br>
> <a href="https://godbolt.org/z/S46I_q" rel="noreferrer" target="_blank">https://godbolt.org/z/S46I_q</a><br>
> <br>
> Dave<br>
> _______________________________________________<br>
> LLVM Developers mailing list<br>
> <a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a><br>
> <a href="https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev" rel="noreferrer" target="_blank">https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev</a><br>
<br>
</blockquote></div>