Skip to content

Assignment in expression with short-circuiting not optimal #115649

@Rageking8

Description

@Rageking8

Given the following code:

#include <utility>

bool assignment_in_expr(bool& value)
{
    return value && !(value = false);
}

bool regular(bool& value)
{
    bool old = value;
    value = false;

    return old;
}

bool exchange(bool& value)
{
    return std::exchange(value, false);
}

Clang (trunk) with -O3 produces (godbolt):

assignment_in_expr(bool&):
        movzx   eax, byte ptr [rdi]
        cmp     al, 1
        jne     .LBB0_2
        mov     byte ptr [rdi], 0
.LBB0_2:
        ret

regular(bool&):
        movzx   eax, byte ptr [rdi]
        mov     byte ptr [rdi], 0
        ret

exchange(bool&):
        movzx   eax, byte ptr [rdi]
        mov     byte ptr [rdi], 0
        ret

Function assignment_in_expr should produce the same assembly as the other 2 variants.

Metadata

Metadata

Assignees

No one assigned

    Labels

    invalidResolved as invalid, i.e. not a bugllvm:instcombineCovers the InstCombine, InstSimplify and AggressiveInstCombine passesmissed-optimization

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions