Skip to content

Identity match on field-less enum compiles worse than using as #95313

Closed
@scottmcm

Description

@scottmcm

Godbolt demo: https://rust.godbolt.org/z/Y35MWYxW8

Because Ordering has explicit discriminants, I expected these two functions to compile to the same thing:

use std::cmp::Ordering;

pub fn demo_match(order: Ordering) -> std::os::raw::c_int {
    match order {
        Ordering::Less => -1,
        Ordering::Equal => 0,
        Ordering::Greater => 1,
    }
}

pub fn demo_as(order: Ordering) -> std::os::raw::c_int {
    order as _
}

They don't, however:

example::demo_match:
        add     dil, 1
        movzx   eax, dil
        add     eax, -1
        ret

example::demo_as:
        movsx   eax, dil
        ret
define i32 @_ZN7example10demo_match17h0b9579071593c5f7E(i8 noundef %0) unnamed_addr #0 !dbg !5 {
start:
  %switch.tableidx = add i8 %0, 1, !dbg !10
  %switch.idx.cast = zext i8 %switch.tableidx to i32, !dbg !10
  %switch.offset = add nsw i32 %switch.idx.cast, -1, !dbg !10
  ret i32 %switch.offset, !dbg !11
}

define i32 @_ZN7example7demo_as17h328bc074f9126c77E(i8 noundef %order) unnamed_addr #0 !dbg !12 {
start:
  %0 = sext i8 %order to i32, !dbg !13
  ret i32 %0, !dbg !14
}

Noticed while replying to https://users.rust-lang.org/t/std-ordering-as-integer/73450/5?u=scottmcm

Metadata

Metadata

Assignees

No one assigned

    Labels

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