Skip to content

Possibly Missed Optimization #150690

@brooklyn-1337

Description

@brooklyn-1337

This generates the following ASM. Godbolt

fn foo(i: i32) -> i64 {
    let i = i.abs() as i64;
    i + i
}
foo:
        mov     eax, edi
        neg     eax
        cmovs   eax, edi
        cdqe
        add     rax, rax
        ret

But, this generates the following ASM. Godbolt

fn foo(i: i32) -> i64 {
    i.abs() as i64 + i.abs() as i64
}
foo:
        mov     eax, edi
        neg     eax
        mov     ecx, edi
        cmovns  ecx, eax
        test    edi, edi
        cdqe
        mov     edx, edi
        cmovs   rdx, rax
        movsxd  rax, ecx
        add     rax, rdx
        ret

Doing a small reordering makes sure they compile down to same ASM. Godbolt

fn foo(i: i32) -> i64 {
    let i = i.abs() as i64;
    i + i
}

fn foo_other(i: i32) -> i64 {
    let i = i.abs();
    i as i64 + i as i64
}
foo:
        mov     eax, edi
        neg     eax
        cmovs   eax, edi
        cdqe
        add     rax, rax
        ret

foo_other = foo

Meta

rustc --version --verbose:

1.92.0

Metadata

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.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions