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

--- Comment #0 from Rainer Schuetze <> 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
_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
__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:
------- You are receiving this mail because: -------

Reply via email to