Skip to content

Missed optimization with match on repr(i8) enum #106459

Closed
@jrose-signal

Description

@jrose-signal

The following functions ought to be equivalent:

use core::cmp::Ordering;

pub fn test1(call: fn() -> Ordering) -> i32 {
    match call() {
        Ordering::Less => -1,
        Ordering::Equal => 0,
        Ordering::Greater => 1,
    }
}

pub fn test2(call: fn() -> Ordering) -> i32 {
    call() as i32
}

but compile to the following x86_64 assembly on play.rust-lang.org:

playground::test1:
	pushq	%rax
	callq	*%rdi
	incb	%al
	movzbl	%al, %eax
	decl	%eax
	popq	%rcx
	retq

playground::test2:
	pushq	%rax
	callq	*%rdi
	movsbl	%al, %eax
	popq	%rcx
	retq

for both stable (1.66.0) and nightly (1.68.0-nightly 2023-01-03 c757267).

Metadata

Metadata

Assignees

Labels

A-LLVMArea: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues.A-mir-optArea: MIR optimizationsC-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