Skip to content

transmute::<u8, bool>() generates bad code #104832

Closed
@ChayimFriedman2

Description

@ChayimFriedman2
pub fn bar(v: u8) -> bool {
    unsafe { std::mem::transmute(v) }
}

Generates (https://rust.godbolt.org/z/xzYdeffxq, note the redundant and):

mov     eax, edi
and     al, 1
ret

This is because we generate a redundant trunc/zext:

// NOTE(eddyb) the `from_immediate` and `to_immediate_scalar`
// conversions allow handling `bool`s the same as `u8`s.
let src = bx.from_immediate(src.immediate());
let src_as_dst = bx.bitcast(src, bx.backend_type(dst.layout));
Immediate(bx.to_immediate_scalar(src_as_dst, dst_scalar)).store(bx, dst);

define zeroext i1 @_ZN7example3bar17h1b5c1a9c0782d25bE(i8 %v) unnamed_addr #0 {
  %0 = alloca i8, align 1
  %1 = trunc i8 %v to i1
  %2 = zext i1 %1 to i8
  store i8 %2, ptr %0, align 1
  %3 = load i8, ptr %0, align 1, !range !2, !noundef !3
  %4 = trunc i8 %3 to i1
  br label %bb1

bb1:                                              ; preds = %start
  ret i1 %4
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-codegenArea: Code generationC-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