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

            Bug ID: 43915
           Summary: Missed optimizations for std::optional<int>::emplace()
           Product: clang
           Version: trunk
          Hardware: PC
                OS: Windows NT
            Status: NEW
          Severity: enhancement
          Priority: P
         Component: LLVM Codegen
          Assignee: [email protected]
          Reporter: [email protected]
                CC: [email protected], [email protected],
                    [email protected]

https://godbolt.org/z/6-0fT3

#include <optional>
std::optional<int> x;
void test() {
    x.emplace(7);
}

clang -O3 codegen: 
test():                               # @test()
        cmp     byte ptr [rip + x+4], 0
        je      .LBB0_2
        mov     byte ptr [rip + x+4], 0
.LBB0_2:
        mov     dword ptr [rip + x], 7
        mov     byte ptr [rip + x+4], 1
        ret

Note the double store to [rip + x+4]. If the initial store was eliminated, that
should also kill the unnecessary comparison+branch because the end result is
the same regardless of the initial state of the object.

gcc -O3 codegen:
test():
        mov     DWORD PTR x[rip], 7
        mov     BYTE PTR x[rip+4], 1
        ret

-- 
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