Bug 8840 – calculating minimum of longs with following comparison compiles to wrong code when enabling the optimizer
Status
RESOLVED
Resolution
FIXED
Severity
blocker
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2012-10-17T14:16:00Z
Last change time
2015-06-09T05:10:43Z
Keywords
wrong-code
Assigned to
nobody
Creator
r.sagitario
Comments
Comment #0 by r.sagitario — 2012-10-17T14:16:55Z
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.
Comment #1 by clugdbug — 2012-10-18T01:15:40Z
Raising severity to blocker. No compiler releases should occur until this is fixed.
Comment #2 by bugzilla — 2012-10-18T09:41:20Z
Wow. How has this one not shown its ugly face before?
Comment #3 by github-bugzilla — 2012-10-18T12:17:28Z