Thread overview
[Issue 8091] New: Optimizer generates wrong code when reducing comparisons.
May 12, 2012
Kasumi Hanazuki
May 13, 2012
Kasumi Hanazuki
May 15, 2012
Don
May 16, 2012
Don
May 18, 2012
Walter Bright
May 12, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=8091

           Summary: Optimizer generates wrong code when reducing
                    comparisons.
           Product: D
           Version: D2
          Platform: All
        OS/Version: All
            Status: NEW
          Keywords: wrong-code
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: nobody@puremagic.com
        ReportedBy: k.hanazuki@gmail.com


--- Comment #0 from Kasumi Hanazuki <k.hanazuki@gmail.com> 2012-05-12 13:24:17 PDT ---
DMD optimizer generates a wrong code for the program below.
The second assertion unexpectedly fails if you specify -O flag to DMD,
while it passes as expected without optimization.

 $ dmd -m32 -run test.d      # fine
 $ dmd -m32 -O -run test.d   # AssertError@test(8): Assertion failure

Tested against DMDv2.060 (git HEAD) on 32-bit Linux.
Found by hos_lyric
<https://twitter.com/#!/hos_lyric_/status/201270597265793024>.

----

int solve(int n) {
    int a = (n % 3 == 0) ? 1 : (n % 3 == 1) ? 1 : 0;
    return (a != 0) ? a : 0;
}

void main() {
    assert(solve(0) ==  1);
    assert(solve(1) ==  1); // line 8, fails with -O
    assert(solve(2) ==  0);
}

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
May 13, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=8091



--- Comment #1 from Kasumi Hanazuki <k.hanazuki@gmail.com> 2012-05-12 23:58:16 PDT ---
more simplification:

----

int solve(int n) {
    int a = (n == 0) ? 1 : (n == 1) ? 1 : 0;
    return (a != 0) ? a : 0;
}

void main() {
    assert(solve(0) ==  1);
    assert(solve(1) ==  1); // fails with -O
    assert(solve(2) ==  0);
}

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
May 15, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=8091


Don <clugdbug@yahoo.com.au> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |clugdbug@yahoo.com.au
           Severity|normal                      |critical


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



--- Comment #2 from Don <clugdbug@yahoo.com.au> 2012-05-16 12:17:27 PDT ---
Marginally reduced (this is what the early optimizer passes turn this into):

int solve(int n) {
    int a = n ? (n>=1u) : 1;
    return a ? a : 0;
}

void main() {
    assert(solve(1) ==  1);
}

It looks as after common subexpression elimination of a, the branch address from the >= is out by 1. The generated code is:

        push    EAX
        cmp    dword ptr -4[EBP],0
        je    L1C
        mov    ECX,1
        cmp    dword ptr -4[EBP],1
        jae    L1F
        mov    ECX,0
        jmp short    L1F <--- wrong address
L1C:        xor    ECX,ECX
        inc    ECX
L1F:        je    L25        <---- shouldn't jump here
        mov    EAX,ECX    <---- should jump here instead
        jmp short    L27
L25:        xor    EAX,EAX
L27:        mov    ESP,EBP

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
May 18, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=8091



--- Comment #3 from github-bugzilla@puremagic.com 2012-05-17 19:59:04 PDT ---
Commit pushed to dmd-1.x at https://github.com/D-Programming-Language/dmd

https://github.com/D-Programming-Language/dmd/commit/8a689e26e1ba6565d76b05d045674967e83aef60 fix Issue 8091 - Optimizer generates wrong code when reducing comparisons

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
May 18, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=8091



--- Comment #4 from github-bugzilla@puremagic.com 2012-05-17 20:01:21 PDT ---
Commit pushed to master at https://github.com/D-Programming-Language/dmd

https://github.com/D-Programming-Language/dmd/commit/40f81b41931eea6768b38b44802e35cd4f32f419 fix Issue 8091 - Optimizer generates wrong code when reducing comparisons

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
May 18, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=8091


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: -------