Skip to content

Missed optimization: Option<fieldless enum> equality #72646

Closed as duplicate of#122734
@Vlad-Shcherbina

Description

@Vlad-Shcherbina

This is similar to #49892, but different.
It's not limited to non-zero integer types. This happens for any types that have holes but no uninitialized bits.
I think comparing Option<some fieldless enum> values is not uncommon, so it adds to the severity of the issue.

To reproduce

  1. Create the following program:
    pub fn eq(x: Option<bool>, y: Option<bool>) -> bool {
        x == y
    }
  2. Inspect generated code, for example by running
    cargo rustc --release --lib -- --emit asm -Cllvm-args=--x86-asm-syntax=intel
    
    Or just visit godbolt.

Expected result

eq:
        cmp     dil, sil
        sete    al
        ret

Actual result

eq:
        cmp     dil, 2
        sete    al
        cmp     sil, 2
        setne   cl
        cmp     al, cl
        je      .LBB0_1
        mov     al, 1
        cmp     dil, 2
        je      .LBB0_5
        cmp     sil, 2
        je      .LBB0_5
        test    dil, dil
        sete    cl
        test    sil, sil
        setne   al
        xor     al, cl
.LBB0_5:
        ret
.LBB0_1:
        xor     eax, eax
        ret

Meta

This happens both on 1.43.0 stable and on the recent nightly:

> rustc --version --verbose
rustc 1.45.0-nightly (8970e8bcf 2020-05-23)
binary: rustc
commit-hash: 8970e8bcf6153d1ead2283f1a0ed7b192230eca6
commit-date: 2020-05-23
host: x86_64-pc-windows-msvc
release: 1.45.0-nightly
LLVM version: 10.0

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.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.llvm-fixed-upstreamIssue expected to be fixed by the next major LLVM upgrade, or backported fixes

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions