Thread overview
[Issue 9097] New: Value range propagation to disable some array bound tests
Jan 16, 2013
yebblies
Jul 09, 2013
Walter Bright
November 29, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=9097

           Summary: Value range propagation to disable some array bound
                    tests
           Product: D
           Version: D2
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: enhancement
          Priority: P2
         Component: DMD
        AssignedTo: nobody@puremagic.com
        ReportedBy: bearophile_hugs@eml.cc


--- Comment #0 from bearophile_hugs@eml.cc 2012-11-29 11:17:33 PST ---
A little test program:


void main(string[] args) {
    size_t a = 6000 + args.length;
    int[200] array;
    array[a % 100] = 1;
}


-----------------------

Compiled with "dmd -O -inline":

__Dmain:
L0:     sub ESP, 0338h
        mov ECX, 0C8h
        push EDI
        mov EAX, 0340h[ESP]
        add EAX, 01770h
        mov 0Ch[ESP], EAX
        xor EAX, EAX
        lea EDI, 01Ch[ESP]
        rep
        stosd
        mov EAX, 0Ch[ESP]
        mov ECX, 064h
        xor EDX, EDX
        div ECX
        cmp EDX, 0C8h
        mov 8[ESP], EDX
        jb  L47
        mov EAX, 4
        call near ptr _D5test27__arrayZ
L47:    mov EDX, 8[ESP]
        xor EAX, EAX
        mov dword ptr 01Ch[EDX*4][ESP], 1
        pop EDI
        add ESP, 0338h
        ret

_D5test27__arrayZ:
L0:     push EAX
        mov ECX, offset FLAT:_D5test212__ModuleInfoZ
        push EAX
        push ECX
        call near ptr __d_array_bounds

-----------------------

Compiled with "dmd -O -inline -release -noboundscheck":

__Dmain:
        sub ESP, 0330h
        mov ECX, 0C8h
        push EDI
        mov EAX, 0338h[ESP]
        add EAX, 01770h
        mov 4[ESP], EAX
        xor EAX, EAX
        lea EDI, 014h[ESP]
        rep
        stosd
        mov EAX, 4[ESP]
        mov ECX, 064h
        xor EDX, EDX
        div ECX
        xor EAX, EAX
        mov dword ptr 014h[EDX*4][ESP], 1
        pop EDI
        add ESP, 0330h
        ret

-----------------------

Value range propagation is able to tell that the value of "a % 100" can't be outside the 0..200 bounds of the array, so there is no point in performing the bound test even in nonrelease mode.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
January 16, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=9097


yebblies <yebblies@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |pull
                 CC|                            |yebblies@gmail.com


--- Comment #1 from yebblies <yebblies@gmail.com> 2013-01-16 23:23:26 EST ---
https://github.com/D-Programming-Language/dmd/pull/1493

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
July 09, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=9097



--- Comment #2 from github-bugzilla@puremagic.com 2013-07-08 22:57:46 PDT ---
Commits pushed to master at https://github.com/D-Programming-Language/dmd

https://github.com/D-Programming-Language/dmd/commit/186acf411866c1950b215f58fd57c08e903f588c Fix Issue 9097 - Value range propagation to disable some array bound tests

Do not emit bounds check code if the index is statically known to be within bounds

https://github.com/D-Programming-Language/dmd/commit/fe9bb4aaad8b9aa132bd86d7a1168be12fec5981 Merge pull request #1493 from yebblies/issue9097

Fix Issue 9097 - Value range propagation to disable some array bound tests

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
July 09, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=9097


Walter Bright <bugzilla@digitalmars.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
                 CC|                            |bugzilla@digitalmars.com
         Resolution|                            |FIXED


-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
July 09, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=9097



--- Comment #3 from bearophile_hugs@eml.cc 2013-07-09 03:15:11 PDT ---
In past dmd generated this with "dmd -O -inline -release -noboundscheck":


__Dmain:
    sub ESP, 0330h
    mov ECX, 0C8h
    push EDI
    mov EAX, 0338h[ESP]
    add EAX, 01770h
    mov 4[ESP], EAX
    xor EAX, EAX
    lea EDI, 014h[ESP]
    rep
    stosd

    mov EAX, 4[ESP]
    mov ECX, 064h
    xor EDX, EDX
    div ECX

    xor EAX, EAX
    mov dword ptr 014h[EDX*4][ESP], 1
    pop EDI
    add ESP, 0330h
    ret


Now it generates this in both the optimized build and the less optimized build "dmd -O -inline" (beside removing the array bound tests, it also avoids the "div"):


__Dmain:
    sub ESP, 0324h
    mov ECX, 0C8h
    push EDI
    mov EAX, 032Ch[ESP]
    add EAX, 01770h
    mov 0324h[ESP], EAX
    xor EAX, EAX
    lea EDI, 4[ESP]
    rep
    stosd

    mov ECX, 0324h[ESP]
    mov EAX, ECX
    mov EDX, 051EB851Fh
    mul EDX
    shr EDX, 5
    imul EAX, EDX, 064h
    sub ECX, EAX

    xor EAX, EAX
    mov dword ptr 4[ECX*4][ESP], 1
    pop EDI
    add ESP, 0324h
    ret

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------