Skip to content

Conditional jumps equivalent to if(0 != 0) can appear in optimized assembly #72343

@Diomendius

Description

@Diomendius

I've got a reasonably minimal test case whose assembly (with opt-level=1 and above) includes this sequence of opcodes, which is effectively a no-op as far as I can tell:

xor  eax, eax
test eax, eax
jne  .LBB0_3

This is the code in question (playground):

pub fn foo(f: fn() -> i32) {
    let mut a = f();
    let mut b = f();

    loop {
        if a != 0 && b == 0 {
            a = f();
        } else if a == 0 && b != 0 {
            b = f();
        } else {
            return
        }
    }
}

I'm not sure whether this is more LLVM's fault or Rust's. Either way, this should not end up in the finished assembly, and I am fairly certain that at least in this specific case all three instructions can be removed entirely without affecting program behaviour.

rustc 1.45.0-nightly (2020-05-18 d8878868c8d7ef3779e7) is the most recent version I have tested that shows this issue, and rustc 1.25.0 on godbolt.org is the oldest. rustc 1.24.0 does not show the issue, though there's a good chance this is just coincidence.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-LLVMArea: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues.A-codegenArea: Code generationC-bugCategory: This is a bug.E-needs-bisectionCall for participation: This issue needs bisection: https://github.com/rust-lang/cargo-bisect-rustcI-slowIssue: Problems and improvements with respect to performance of generated code.P-mediumMedium priorityT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.regression-from-stable-to-stablePerformance or correctness regression from one stable version to another.

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions