Skip to content

Loops with inclusive ranges produce horrible assembly #75035

Closed
@MSxDOS

Description

@MSxDOS

This:

extern {
    fn f();
}

pub unsafe fn test_loop() {
    for _ in 1 ..= 7 { 
        f();
    }
}

on 1.42:

example::test_loop:
        push    rbx
        mov     rbx, qword ptr [rip + f@GOTPCREL]
        call    rbx
        call    rbx
        call    rbx
        call    rbx
        call    rbx
        call    rbx
        mov     rax, rbx
        pop     rbx
        jmp     rax

on 1.43 and above:

example::test_loop:
        push    rbp
        push    r15
        push    r14
        push    rbx
        push    rax
        mov     ebx, 1
        mov     r14d, 7
        mov     r15, qword ptr [rip + f@GOTPCREL]
.LBB0_1:
        lea     ebp, [rbx + 1]
        cmp     ebx, 7
        cmovge  ebp, r14d
        call    r15
        cmp     ebp, 7
        jg      .LBB0_3
        cmp     ebx, 7
        mov     ebx, ebp
        jl      .LBB0_1
.LBB0_3:
        add     rsp, 8
        pop     rbx
        pop     r14
        pop     r15
        pop     rbp
        ret

https://godbolt.org/z/n63seh

Metadata

Metadata

Assignees

Labels

C-enhancementCategory: An issue proposing an enhancement or a PR with one.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