Thread overview
[Issue 8840] New: calculating minimum of longs with following comparison compiles to wrong code when enabling the optimizer
Oct 17, 2012
Rainer Schuetze
Oct 18, 2012
Don
Oct 18, 2012
Walter Bright
Oct 18, 2012
Walter Bright
October 17, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=8840

           Summary: calculating minimum of longs with following comparison
                    compiles to wrong code when enabling the optimizer
           Product: D
           Version: D2
          Platform: All
        OS/Version: Windows
            Status: NEW
          Keywords: wrong-code
          Severity: major
          Priority: P2
         Component: DMD
        AssignedTo: nobody@puremagic.com
        ReportedBy: r.sagitario@gmx.de


--- Comment #0 from Rainer Schuetze <r.sagitario@gmx.de> 2012-10-17 14:16:55 PDT ---
import core.stdc.stdio;

long foo() { return 4; }

void main()
{
    long f1 = foo();
    long f2 = foo();

    long f = (f1 < f2 ? f1 : f2);
    int len = (f == 0 ? 0 : printf("%llx\n", f));
}

compiled with "dmd -o" and running produces the output "400000004" instead of just "4".

Here's the disassembly:
_D4test3fooFZl  comdat
        assume  CS:_D4test3fooFZl
                mov     EAX,4
                xor     EDX,EDX
                ret
_D4test3fooFZl  ends
__Dmain comdat
        assume  CS:__Dmain
L0:             sub     ESP,0Ch
                push    EBX
                push    ESI
                call    near ptr _D4test3fooFZl
                mov     8[ESP],EAX
                mov     0Ch[ESP],EDX
                call    near ptr _D4test3fooFZl
                cmp     EDX,0Ch[ESP]
                jl      L33
                jg      L25
                cmp     EAX,8[ESP]
                jbe     L33
L25:            mov     ECX,0Ch[ESP]
                mov     EBX,8[ESP]
                mov     ESI,ECX
                or      ESI,EBX
                jmp short       L39
L33:            mov     ECX,EDX
                mov     EBX,EAX
                or      ECX,EBX
L39:            je      L4B
                mov     EAX,offset FLAT:_DATA
                push    ECX
                push    EBX
                push    EAX
                call    near ptr _printf
                add     ESP,0Ch
L4B:            xor     EAX,EAX
                pop     ESI
                pop     EBX
                add     ESP,0Ch
                ret
__Dmain ends

Note that ECX is pushed in the call to printf, but it is the result of hiword|loword in the part after L33.

dmc suffers from the same problem.

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


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

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |clugdbug@yahoo.com.au
           Severity|major                       |blocker


--- Comment #1 from Don <clugdbug@yahoo.com.au> 2012-10-18 01:15:40 PDT ---
Raising severity to blocker. No compiler releases should occur until this is fixed.

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


Walter Bright <bugzilla@digitalmars.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |bugzilla@digitalmars.com


--- Comment #2 from Walter Bright <bugzilla@digitalmars.com> 2012-10-18 09:41:20 PDT ---
Wow. How has this one not shown its ugly face before?

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



--- Comment #3 from github-bugzilla@puremagic.com 2012-10-18 12:17:28 PDT ---
Commit pushed to dmd-1.x at https://github.com/D-Programming-Language/dmd

https://github.com/D-Programming-Language/dmd/commit/551e86b022a61064d11fa43924d18222ed366d80
fix Issue 8840 - calculating minimum of longs with following comparison
compiles to wrong code when enabling the optimizer

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



--- Comment #4 from github-bugzilla@puremagic.com 2012-10-18 12:17:35 PDT ---
Commit pushed to master at https://github.com/D-Programming-Language/dmd

https://github.com/D-Programming-Language/dmd/commit/fdb413235448e27dad0aab615837b051483de150
fix Issue 8840 - calculating minimum of longs with following comparison
compiles to wrong code when enabling the optimizer

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



--- Comment #5 from github-bugzilla@puremagic.com 2012-10-18 12:18:39 PDT ---
Commit pushed to master at https://github.com/D-Programming-Language/dmd

https://github.com/D-Programming-Language/dmd/commit/881f9fcb740516391e1c7dd1a3e39046dd21e74a test case for issue 8840

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


Walter Bright <bugzilla@digitalmars.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
            Version|D2                          |D1 & D2
         Resolution|                            |FIXED
         OS/Version|Windows                     |All


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