Skip to content

== with Option<primitive> has two branches #13927

Closed
@huonw

Description

@huonw
pub fn foo(a: Option<u8>) -> bool {
    a == Some(2)
}

optimises to (with --opt-level=3 --crate-type=lib)

define i8 @_ZN3foo20he42f37a9b93a7ee3daa4v0.0E(%"enum.std::option::Option<u8>[#1]") unnamed_addr #0 {
entry-block:
  %.fca.0.extract8 = extractvalue %"enum.std::option::Option<u8>[#1]" %0, 0
  %cond.i = icmp eq i8 %.fca.0.extract8, 0
  br i1 %cond.i, label %"_ZN6option30Option$LT$T$GT$...std..cmp..Eq2eq20h54698946061538303014v0.0E.exit", label %match_case9.i

match_case9.i:                                    ; preds = %entry-block
  %.fca.2.0.extract10 = extractvalue %"enum.std::option::Option<u8>[#1]" %0, 2, 0
  %1 = icmp eq i8 %.fca.2.0.extract10, 2
  %2 = zext i1 %1 to i8
  br label %"_ZN6option30Option$LT$T$GT$...std..cmp..Eq2eq20h54698946061538303014v0.0E.exit"

"_ZN6option30Option$LT$T$GT$...std..cmp..Eq2eq20h54698946061538303014v0.0E.exit": ; preds = %entry-block, %match_case9.i
  %__make_return_pointer.0.i = phi i8 [ %2, %match_case9.i ], [ 0, %entry-block ]
  ret i8 %__make_return_pointer.0.i
}

In a perfect world, this would optimise to something like transmute::<_, u16>(a) == 0x0102.

Metadata

Metadata

Assignees

No one assigned

    Labels

    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