Skip to content

No bounds checks should be emitted for static arrays #553

Description

@dnadlinger

Reported by @bearophile:

I am trying the following program with both the latest dmd 2.065alpha and ldc2 2.063.2, with no optimizations:

int fun(int[3] a) {
    return a[0]+ a[1] + a[2];
}
int main() {
    int total = 0;
    foreach (immutable _; 0 .. 100_000_000)
        total += fun([1, 2, 3]);
    return total;
}
__D5test23funFG3iZi:
    subl    $28, %esp
    leal    32(%esp), %eax
    movb    $1, %cl
    testb   $1, %cl
    movl    %eax, 24(%esp)
    jne LBB0_2
    jmp LBB0_1
LBB0_1:
    leal    __D5test212__ModuleInfoZ, %eax
    movl    $2, %ecx
    movl    %eax, (%esp)
    movl    $2, 4(%esp)
    movl    %ecx, 20(%esp)
    calll   __d_array_bounds
LBB0_2:
    movb    $1, %al
    testb   $1, %al
    jne LBB0_4
    leal    __D5test212__ModuleInfoZ, %eax
    movl    $2, %ecx
    movl    %eax, (%esp)
    movl    $2, 4(%esp)
    movl    %ecx, 16(%esp)
    calll   __d_array_bounds
LBB0_4:
    movb    $1, %al
    movl    24(%esp), %ecx
    movl    (%ecx), %edx
    addl    4(%ecx), %edx
    testb   $1, %al
    movl    %edx, 12(%esp)
    jne LBB0_6
    leal    __D5test212__ModuleInfoZ, %eax
    movl    $2, %ecx
    movl    %eax, (%esp)
    movl    $2, 4(%esp)
    movl    %ecx, 8(%esp)
    calll   __d_array_bounds
LBB0_6:
    movl    24(%esp), %eax
    movl    8(%eax), %ecx
    movl    12(%esp), %edx
    addl    %ecx, %edx
    movl    %edx, %eax
    addl    $28, %esp
    ret $12

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions