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: [email protected]
          Reporter: [email protected]
                CC: [email protected], [email protected],
                    [email protected], [email protected]

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.
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to