Skip to content

RangeToInclusive code-generation much worse than equivilent RangeTo #63646

Closed
@gamozolabs

Description

@gamozolabs

I have two copies of the exact same code, one using for _ in 0..65 and the other using for _ in 0..=64. The 0..65 one optimizes down to very simple code, however the 0..=64 version ends up emitting a large amount of unnecessary code.


RangeTo example

pub fn and_stuff(a: i32, mut b: i32) -> i32 {
    for _ in 0..65 {
        b &= a;
    }

    b
}

Emits (https://rust.godbolt.org/z/pcscMB):

example::and_stuff:
        mov     eax, edi
        and     eax, esi
        ret

RangeToInclusive example

pub fn and_stuff(a: i32, mut b: i32) -> i32 {
    for _ in 0..=64 {
        b &= a;
    }

    b
}

Emits (https://rust.godbolt.org/z/22JaO2):

.LCPI0_0:
        .zero   4
        .long   4294967295
        .long   4294967295
        .long   4294967295
example::and_stuff:
        movd    xmm0, esi
        movaps  xmm1, xmmword ptr [rip + .LCPI0_0]
        movss   xmm1, xmm0
        movd    xmm0, edi
        pshufd  xmm0, xmm0, 0
        pand    xmm0, xmm1
        pshufd  xmm1, xmm0, 78
        pand    xmm1, xmm0
        pshufd  xmm0, xmm1, 229
        pand    xmm0, xmm1
        movd    eax, xmm0
        and     eax, edi
        ret

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-codegenArea: Code generationC-bugCategory: This is a bug.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.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