Skip to content

Loop isn't optimized in pow() with constant power #34947

Closed
@eefriedman

Description

@eefriedman

Testcase:

pub fn f(x: i32) -> i32 { x.pow(5) }

Produces the following asm in release mode:

_ZN8rust_out1f17hc690d9db51fd1099E:
    .cfi_startproc
    movl    $5, %ecx
    movl    $1, %r8d
    movl    $1, %eax
    .align  16, 0x90
.LBB0_1:
    movl    %ecx, %esi
    testb   $1, %sil
    movl    %edi, %edx
    cmovel  %r8d, %edx
    imull   %edx, %eax
    shrl    %ecx
    imull   %edi, %edi
    cmpl    $3, %esi
    ja  .LBB0_1
    cmpl    $1, %ecx
    movl    $1, %ecx
    cmovel  %edi, %ecx
    imull   %ecx, %eax
    retq

I would expect it to generate straight-line code with just three multiplies.

It's possible this will be fixed by upgrading LLVM; there have been some recent changes to unrolling heuristics.

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-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