Skip to content

rustc generates badly optimized for wrapping_div #34634

Closed
@eefriedman

Description

pub fn f(x: i32, y: i32) -> i32 {
  x.wrapping_div(y)
}

Gives:

_ZN8rust_out1f17h164f16efff59de66E:
    pushq   %rax
.Ltmp0:
    cmpl    $-2147483648, %edi
    jne .LBB0_2
    movl    $-2147483648, %eax
    cmpl    $-1, %esi
    je  .LBB0_5
.LBB0_2:
    cmpl    $-1, %esi
    je  .LBB0_6
    testl   %esi, %esi
    jne .LBB0_4
    leaq    panic_loc7436(%rip), %rdi
    callq   _ZN4core9panicking5panic17h907815f47e914305E@PLT
.LBB0_6:
    cmpl    $-2147483648, %edi
    je  .LBB0_7
.LBB0_4:
    movl    %edi, %eax
    cltd
    idivl   %esi
.LBB0_5:
    popq    %rcx
    retq
.LBB0_7:
    leaq    panic_loc7440(%rip), %rdi
    callq   _ZN4core9panicking5panic17h907815f47e914305E@PLT

Specifically, .LBB0_7 is unreachable, but LLVM can't quite figure that out. It's probably possible to write wrapping_div in a way which avoids this problem.

Metadata

Assignees

No one assigned

    Labels

    A-MIRArea: Mid-level IR (MIR) - https://blog.rust-lang.org/2016/04/19/MIR.htmlA-codegenArea: Code generationC-enhancementCategory: An issue proposing an enhancement or a PR with one.E-needs-testCall for participation: An issue has been fixed and does not reproduce, but no test has been added.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