Skip to content

Unnecessary comparison and conditional branch #65333

Closed
@tspiteri

Description

@tspiteri

This is a reopening of #37114 which was fixed in 1.28.0 but seems to have regressed again since 1.30.0.

pub fn foo(v: &mut Vec<i32>) {
    let len = v.len();
    assert!(len > 1);
    v[len - 2] += 1;
    v.pop();
}

The asm for 1.38 is

example::foo:
        push    rax
        mov     rax, qword ptr [rdi + 16]
        cmp     rax, 2
        jb      .LBB7_4
        mov     rcx, qword ptr [rdi]
        add     dword ptr [rcx + 4*rax - 8], 1
; 1.28 does not generate these 3 instructions:
        mov     rax, qword ptr [rdi + 16]
        test    rax, rax
        je      .LBB7_3
; end of unnecessary code
        add     rax, -1
        mov     qword ptr [rdi + 16], rax
.LBB7_3:
        pop     rax
        ret
.LBB7_4:
        call    std::panicking::begin_panic
        ud2

https://godbolt.org/z/SLPlgo

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-LLVMArea: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues.C-bugCategory: This is a bug.I-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