[Issue 15629] [REG] wrong code with "-O -inline" but correct with "-O"

2016-02-07 Thread via Digitalmars-d-bugs
https://issues.dlang.org/show_bug.cgi?id=15629

--- Comment #3 from Kenji Hara  ---
Dustmited case code:

void main()
{
int[] a = [3];
int value = abs(a[0]);
assert(a[0] == 3);
writeln(value, " ", a);
}

Num abs(Num)(Num x)
{
return x >= 0 ? x : -x;
}

struct File
{
struct LockingTextWriter
{
this(File) {}

~this() @trusted {}
}

auto lockingTextWriter()
{
return LockingTextWriter();
}
}

File stdout;

void writeln(T...)(T args)
{
stdout.lockingTextWriter;
}

--


[Issue 15629] [REG] wrong code with "-O -inline" but correct with "-O"

2016-01-30 Thread via Digitalmars-d-bugs
https://issues.dlang.org/show_bug.cgi?id=15629

--- Comment #2 from Ivan Kazmenko  ---
Turned out that array b is also not necessary.

A bit less code (testmodule2.d):
-
import std.math;
import std.stdio;

void main ()
{
int [] a = [3];
int value = abs (a[0]);
if (a[0] != 3)
{
writeln (value, " ", a);
assert (false);
}
}
-

Disassembly:
-
__Dmain PROC NEAR
;  COMDEF __Dmain
sub esp, 36 ;  _ 83. EC, 24
mov eax, 3  ; 0003 _ B8, 0003
mov ecx, 1  ; 0008 _ B9, 0001
mov dword ptr [esp+0CH], ebx; 000D _ 89. 5C 24, 0C
mov dword ptr [esp+10H], esi; 0011 _ 89. 74 24, 10
mov dword ptr [esp+18H], eax; 0015 _ 89. 44 24, 18
mov dword ptr [esp+4H], ecx ; 0019 _ 89. 4C 24, 04
mov dword ptr [esp], offset FLAT:_D11TypeInfo_Ai6__initZ; 001D _
C7. 04 24, (segrel)
call__d_arrayliteralTX  ; 0024 _ E8,
(rel)
mov edx, eax; 0029 _ 89. C2
mov eax, dword ptr [esp+18H]; 002B _ 8B. 44 24, 18
mov dword ptr [esp+20H], edx; 002F _ 89. 54 24, 20
mov ebx, dword ptr [esp+20H]; 0033 _ 8B. 5C 24, 20
mov ecx, dword ptr [esp+14H]; 0037 _ 8B. 4C 24, 14
mov dword ptr [edx], eax; 003B _ 89. 02
mov esi, dword ptr [ebx]; 003D _ 8B. 33
mov eax, esi; 003F _ 89. F0
sar eax, 31 ; 0041 _ C1. F8, 1F
xor esi, eax; 0044 _ 33. F0
sub esi, eax; 0046 _ 2B. F0
mov dword ptr [esp+1CH], 1  ; 0048 _ C7. 44 24, 1C,
0001
cmp ecx, 3  ; 0050 _ 83. F9, 03
jz  ?_0056  ; 0053 _ 74, 24
pushesi ; 0055 _ 56
mov edx, offset FLAT:?_0001 ; 0056 _ BA,
(segrel)
mov ebx, 1  ; 005B _ BB, 0001
pushedx ; 0060 _ 52
pushebx ; 0061 _ 53
pushdword ptr [esp+2CH] ; 0062 _ FF. 74 24, 2C
pushdword ptr [esp+2CH] ; 0066 _ FF. 74 24, 2C
call_D3std5stdio21__T7writelnTiTAyaTAiZ7writelnFNfiAyaAiZv; 006A _
E8, (rel)
mov eax, 11 ; 006F _ B8, 000B
call_D11testmodule28__assertFiZv; 0074 _ E8,
(rel)
?_0056: mov ebx, dword ptr [esp+0CH]; 0079 _ 8B. 5C 24, 0C
mov esi, dword ptr [esp+10H]; 007D _ 8B. 74 24, 10
add esp, 36 ; 0081 _ 83. C4, 24
xor eax, eax; 0084 _ 31. C0
ret ; 0086 _ C3
__Dmain ENDP
-

Now the lines supposed to carry the "3" seem to be
-
mov eax, 3  ; 0003 _ B8, 0003
mov dword ptr [esp+18H], eax; 0015 _ 89. 44 24, 18
mov eax, dword ptr [esp+18H]; 002B _ 8B. 44 24, 18
mov dword ptr [edx], eax; 003B _ 89. 02
-
and then for some reason
-
mov ecx, dword ptr [esp+14H]; 0037 _ 8B. 4C 24, 14
cmp ecx, 3  ; 0050 _ 83. F9, 03
jz  ?_0056  ; 0053 _ 74, 24
-

--


[Issue 15629] [REG] wrong code with "-O -inline" but correct with "-O"

2016-01-30 Thread via Digitalmars-d-bugs
https://issues.dlang.org/show_bug.cgi?id=15629

--- Comment #1 from Ivan Kazmenko  ---
Here is the relevant disassembly of the object file produced 2.070.0:

-
__Dmain PROC NEAR
;  COMDEF __Dmain
sub esp, 44 ;  _ 83. EC, 2C
mov eax, 3  ; 0003 _ B8, 0003
mov ecx, 1  ; 0008 _ B9, 0001
mov dword ptr [esp+0CH], ebx; 000D _ 89. 5C 24, 0C
mov edx, offset FLAT:_D11TypeInfo_Ai6__initZ; 0011 _ BA,
(segrel)
mov dword ptr [esp+10H], esi; 0016 _ 89. 74 24, 10
mov dword ptr [esp+18H], eax; 001A _ 89. 44 24, 18
mov dword ptr [esp+4H], ecx ; 001E _ 89. 4C 24, 04
mov dword ptr [esp], edx; 0022 _ 89. 14 24
call__d_arrayliteralTX  ; 0025 _ E8,
(rel)
mov ebx, eax; 002A _ 89. C3
mov eax, dword ptr [esp+18H]; 002C _ 8B. 44 24, 18
mov dword ptr [ebx], eax; 0030 _ 89. 03
mov dword ptr [esp+20H], ebx; 0032 _ 89. 5C 24, 20
mov dword ptr [esp+1CH], 1  ; 0036 _ C7. 44 24, 1C,
0001
mov dword ptr [esp+4H], 1   ; 003E _ C7. 44 24, 04,
0001
mov dword ptr [esp], offset FLAT:_D11TypeInfo_Ai6__initZ; 0046 _
C7. 04 24, (segrel)
call__d_arrayliteralTX  ; 004D _ E8,
(rel)
mov esi, dword ptr [esp+20H]; 0052 _ 8B. 74 24, 20
mov edx, dword ptr [esp+14H]; 0056 _ 8B. 54 24, 14
mov dword ptr [eax], 9  ; 005A _ C7. 00,
0009
mov dword ptr [esp+28H], eax; 0060 _ 89. 44 24, 28
mov eax, dword ptr [esi]; 0064 _ 8B. 06
mov ecx, eax; 0066 _ 89. C1
sar ecx, 31 ; 0068 _ C1. F9, 1F
xor eax, ecx; 006B _ 33. C1
sub eax, ecx; 006D _ 2B. C1
mov dword ptr [esp+24H], 1  ; 006F _ C7. 44 24, 24,
0001
cmp edx, 3  ; 0077 _ 83. FA, 03
jz  ?_0056  ; 007A _ 74, 2E
pusheax ; 007C _ 50
mov ecx, offset FLAT:?_0001 ; 007D _ B9,
(segrel)
mov ebx, 1  ; 0082 _ BB, 0001
pushecx ; 0087 _ 51
pushebx ; 0088 _ 53
pushdword ptr [esp+2CH] ; 0089 _ FF. 74 24, 2C
pushdword ptr [esp+2CH] ; 008D _ FF. 74 24, 2C
pushecx ; 0091 _ 51
pushebx ; 0092 _ 53
pushdword ptr [esp+44H] ; 0093 _ FF. 74 24, 44
pushdword ptr [esp+44H] ; 0097 _ FF. 74 24, 44
call   
_D3std5stdio28__T7writelnTiTAyaTAiTAyaTAiZ7writelnFNfiAyaAiAyaAiZv; 009B _ E8,
(rel)
mov eax, 12 ; 00A0 _ B8, 000C
call_D10testmodule8__assertFiZv ; 00A5 _ E8,
(rel)
?_0056: mov ebx, dword ptr [esp+0CH]; 00AA _ 8B. 5C 24, 0C
mov esi, dword ptr [esp+10H]; 00AE _ 8B. 74 24, 10
add esp, 44 ; 00B2 _ 83. C4, 2C
xor eax, eax; 00B5 _ 31. C0
ret ; 00B7 _ C3
__Dmain ENDP
-

Looks like these lines are responsible:
-
mov dword ptr [esp+18H], eax; 001A
...
mov edx, dword ptr [esp+14H]; 0056
...
cmp edx, 3  ; 0077
jz  ?_0056  ; 007A
-
I don't understand why the line at 0056 uses "dword ptr [esp+14H]" and not
"dword ptr [esp+18H]".

(Disclaimer: I'm not fluent in asm.)

--