Why aren't clang++ and g++ de-duplicating these instructions?

Viewed 36

Consider the following function:

std::string get_value(const bool b)
{
    if (b) {
        return "Hello";
    }
    else {
        return "World";
    }
}

g++ 11.0.1 20210312 compiles this (as C++17 and with maximum optimization) into:

get_value[abi:cxx11](bool):
        lea     rdx, [rdi+16]
        mov     rax, rdi
        mov     QWORD PTR [rdi], rdx
        test    sil, sil
        je      .L2
        mov     DWORD PTR [rdi+16], 1819043144
        mov     BYTE PTR [rdx+4], 111
        mov     QWORD PTR [rax+8], 5
        mov     BYTE PTR [rax+21], 0
        ret
.L2:
        mov     DWORD PTR [rdi+16], 1819438935
        mov     BYTE PTR [rdx+4], 100
        mov     QWORD PTR [rax+8], 5
        mov     BYTE PTR [rax+21], 0
        ret

Why does it not move the two replicated mov instructions up before the jump, or even before the test, reducing the code size by two instructions?

The same thing happens with clang++ and libc++, except it only has one relevant instruction to move up.

(See this also on GodBolt)

0 Answers
Related