https://gcc.gnu.org/bugzilla/show_bug.cgi?id=117789

--- Comment #2 from Jonathan Wakely <redi at gcc dot gnu.org> ---
(In reply to Kim Nilsson from comment #0)
> https://godbolt.org/z/5xz4nxehv.

Please provide the code here, not as an external link. There's no guarantee the
CE link will remain valid indefinitely, and then this bug report becomes harder
to understand.

The example is:

#include <string>

int main() {
    std::string const greet1 = "Hello";
    std::string const greet2 = ", world!";
    return static_cast<int>(greet1.size() + greet2.size());
}

That produces a lot of code:

main:
        push    rbx
        sub     rsp, 64
        lea     rax, [rsp+16]
        lea     rdi, [rsp+32]
        mov     BYTE PTR [rsp+20], 111
        mov     QWORD PTR [rsp], rax
        lea     rax, [rsp+48]
        mov     QWORD PTR [rsp+32], rax
        movabs  rax, 2406167339674837036
        mov     QWORD PTR [rsp+48], rax
        mov     DWORD PTR [rsp+16], 1819043144
        mov     QWORD PTR [rsp+8], 5
        mov     BYTE PTR [rsp+21], 0
        mov     QWORD PTR [rsp+40], 8
        mov     BYTE PTR [rsp+56], 0
        call    std::__cxx11::basic_string<char, std::char_traits<char>,
std::allocator<char> >::_M_dispose()
        mov     rdi, rsp
        call    std::__cxx11::basic_string<char, std::char_traits<char>,
std::allocator<char> >::_M_dispose()
        add     rsp, 64
        mov     eax, 13
        pop     rbx
        ret

Without the __builtin_unreachable (and with older GCC releases) we get:

main:
        mov     eax, 13
        ret

But as Andrew noted, the problem is that the test uses `main` and that changes
GCC's inlining decisions. Renaming `main` to `f` produces identical codegen
with and without __builtin_unreachable there. Optimising the code size for a
three line program that just creates some std::string objects in main is not a
goal. For "real" functions, the codegen is fine.

Reply via email to