Skip to content

Missed optimization in if with constant branches #129144

Open

Description

I assume the following two functions behave identically, and therefore should result in the same codegen. https://godbolt.org/z/vj9ornqbG

#[no_mangle]
pub fn cast(x: bool) -> f32 {
    x as u8 as f32
}

#[no_mangle]
pub fn ifelse(x: bool) -> f32 {
    if x { 1. } else { 0. }
}

However, ifelse fails to be optimised to a cast.

cast:
        cvtsi2ss        xmm0, edi
        ret

.LCPI1_0:
        .long   0x3f800000
ifelse:
        test    edi, edi
        jne     .LBB1_1
        xorps   xmm0, xmm0
        ret
.LBB1_1:
        movss   xmm0, dword ptr [rip + .LCPI1_0]
        ret
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    A-LLVMArea: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues.C-optimizationCategory: An issue highlighting optimization opportunities or PRs implementing suchI-slowIssue: Problems and improvements with respect to performance of generated code.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions