Skip to content

Unnecessary conditional branch generated #37114

Closed
@tspiteri

Description

@tspiteri

This function

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

gives the following assembly

    .section    .text._ZN8rust_out3foo17h5b3f981e51393a1aE,"ax",@progbits
    .p2align    4, 0x90
    .type   _ZN8rust_out3foo17h5b3f981e51393a1aE,@function
_ZN8rust_out3foo17h5b3f981e51393a1aE:
    .cfi_startproc
    pushq   %rax
.Ltmp20:
    .cfi_def_cfa_offset 16
    movq    16(%rdi), %rax
    cmpq    $1, %rax
    jbe .LBB6_4
    movq    (%rdi), %rcx
    incl    -8(%rcx,%rax,4)
    movq    16(%rdi), %rax
    testq   %rax, %rax
    je  .LBB6_3
    decq    %rax
    movq    %rax, 16(%rdi)
.LBB6_3:
    popq    %rax
    retq
.LBB6_4:
    callq   _ZN3std9panicking11begin_panic17hc03e2830c2c89a5fE
.Lfunc_end6:
    .size   _ZN8rust_out3foo17h5b3f981e51393a1aE, .Lfunc_end6-_ZN8rust_out3foo17h5b3f981e51393a1aE
    .cfi_endproc

There is a conditional branch to .LBB6_4 if the length is < 2. There is no conditional branch created for v[len - 2], which is good, as the length is >= 2. However, there is a conditional branch to .LBB6_3 created for v.pop() which can never be taken.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-LLVMArea: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues.C-enhancementCategory: An issue proposing an enhancement or a PR with one.I-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