[llvm-bugs] [Bug 41980] New: Missed optimization opportunity when returning function pointers based on run-time boolean

via llvm-bugs llvm-bugs at lists.llvm.org
Wed May 22 05:29:32 PDT 2019


https://bugs.llvm.org/show_bug.cgi?id=41980

            Bug ID: 41980
           Summary: Missed optimization opportunity when returning
                    function pointers based on run-time boolean
           Product: clang
           Version: trunk
          Hardware: PC
                OS: Windows NT
            Status: NEW
          Severity: enhancement
          Priority: P
         Component: -New Bugs
          Assignee: unassignedclangbugs at nondot.org
          Reporter: vittorio.romeo at outlook.com
                CC: htmldeveloper at gmail.com, llvm-bugs at lists.llvm.org,
                    neeilans at live.com, richard-llvm at metafoo.co.uk

Given the following two functions:

    int f() { return 0; }
    int g() { return 1; }

And the following code to invoke one of them depending on a boolean `b`:

    int t0(bool b) { return (b ? &f : &g)(); }
    int t1(bool b) { return b ? f() : g(); }
    int t2(bool b) { return b ? t0(true) : t0(false); }

Both `g++ (trunk)` and `clang++ (trunk)` with `-std=c++2a -Ofast -march=native`
fail to optimize the following code:

    int main(int ac, char**) { return t0(ac & 1); }

Producing the following assembly:

>     main:
>       and edi, 1
>       mov eax, OFFSET FLAT:f()
>       mov edx, OFFSET FLAT:g()
>       cmove rax, rdx
>       jmp rax
> 


Invoking `t1` or `t2` (instead of `t0`) produces the following optimized
assembly:

>     main:
>             mov     eax, edi
>             not     eax
>             and     eax, 1
>             ret


Everything can be reproduced live on **gcc.godbolt.org**:
https://godbolt.org/z/gh7270

-- 
You are receiving this mail because:
You are on the CC list for the bug.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-bugs/attachments/20190522/34a0be39/attachment.html>


More information about the llvm-bugs mailing list