Skip to content

Noop loop is only optimized away when the range is half-open #123845

Open
@krtab

Description

@krtab
#[no_mangle]
pub fn f() {
    for _ in 0..=100 {
        ()
    }
}

#[no_mangle]
pub fn g() {
    for _ in 0..100 {
        ()
    }
}

with -O gives

f:
        xor     ecx, ecx
        mov     eax, 100
.LBB0_1:
        lea     edx, [rcx + 1]
        cmp     ecx, 100
        mov     ecx, edx
        cmovge  ecx, eax
        jge     .LBB0_3
        cmp     ecx, 101
        jl      .LBB0_1
.LBB0_3:
        ret

g:
        ret

Meta

Exists in both 1.77 and nightly.

Backtrace

<backtrace>

@rustbot label +A-codegen +A-LLVM +C-optimization +I-slow +S-has-mcve +I-heavy +T-compiler -needs-triage

Metadata

Metadata

Labels

A-LLVMArea: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues.A-codegenArea: Code generationC-bugCategory: This is a bug.C-optimizationCategory: An issue highlighting optimization opportunities or PRs implementing suchI-heavyIssue: Problems and improvements with respect to binary size of generated code.I-slowIssue: Problems and improvements with respect to performance of generated code.S-has-mcveStatus: A Minimal Complete and Verifiable Example has been found for this issueT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.llvm-fixed-upstreamIssue expected to be fixed by the next major LLVM upgrade, or backported fixes

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions