Bug 9320 – optimizer should do copy propagation on structs, too

Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2013-01-14T14:08:00Z
Last change time
2015-06-09T05:11:43Z
Assigned to
nobody
Creator
dransic

Comments

Comment #0 by dransic — 2013-01-14T14:08:34Z
This codes defines a struct that is just a wrapper around a real x, with binary operators. --- struct Foo { real x; version(Constructor) { this(real x) { this.x = x; } } Foo opBinary(string op)(Foo other) { return Foo(mixin("x" ~ op ~ "other.x")); } } version(Constructor) static assert(!__traits(isPOD, Foo)); else static assert(__traits(isPOD, Foo)); Foo test(Foo a, Foo b, Foo c) { return (a + b) / (a * b) - c; } void main() {} --- When compiled with -O -inline -release, the object code for the test function is: --- push RBP mov RBP,RSP sub RSP,010h fld tbyte ptr 030h[RBP] fld tbyte ptr 020h[RBP] faddp ST(1),ST fld tbyte ptr 030h[RBP] fld tbyte ptr 020h[RBP] fmulp ST(1),ST fdivp ST(1),ST fld tbyte ptr 010h[RBP] fsubp ST(1),ST fstp tbyte ptr [RDI] mov word ptr 0Ah[RDI],0 mov dword ptr 0Ch[RDI],0 mov RAX,RDI mov RSP,RBP pop RBP ret --- When compiled with the same flags, but with -version=Constructor, I benchmarked the code as 5x slower (no data here) and the object code becomes: --- push RBP mov RBP,RSP sub RSP,0D0h mov -010h[RBP],RDI lea RSI,020h[RBP] lea RDI,-0A0h[RBP] movsd movsd mov RSI,[00h][RIP] lea RDI,-090h[RBP] movsd movsd fld tbyte ptr 030h[RBP] fld tbyte ptr -0A0h[RBP] faddp ST(1),ST fstp tbyte ptr -090h[RBP] mov word ptr -086h[RBP],0 mov dword ptr -084h[RBP],0 lea RSI,-090h[RBP] lea RDI,-0B0h[RBP] movsd movsd lea RSI,020h[RBP] lea RDI,-060h[RBP] movsd movsd mov RSI,[00h][RIP] lea RDI,-050h[RBP] movsd movsd fld tbyte ptr 030h[RBP] fld tbyte ptr -060h[RBP] fmulp ST(1),ST fstp tbyte ptr -050h[RBP] mov word ptr -046h[RBP],0 mov dword ptr -044h[RBP],0 lea RSI,-050h[RBP] lea RDI,-070h[RBP] movsd movsd lea RSI,-070h[RBP] lea RDI,-080h[RBP] movsd movsd mov RSI,[00h][RIP] lea RDI,-040h[RBP] movsd movsd fld tbyte ptr -0B0h[RBP] fld tbyte ptr -080h[RBP] fdivp ST(1),ST fstp tbyte ptr -040h[RBP] mov word ptr -036h[RBP],0 mov dword ptr -034h[RBP],0 lea RSI,-040h[RBP] lea RDI,-0C0h[RBP] movsd movsd lea RSI,010h[RBP] lea RDI,-030h[RBP] movsd movsd mov RSI,[00h][RIP] lea RDI,-020h[RBP] movsd movsd fld tbyte ptr -0C0h[RBP] fld tbyte ptr -030h[RBP] fsubp ST(1),ST fstp tbyte ptr -020h[RBP] mov word ptr -016h[RBP],0 mov dword ptr -014h[RBP],0 lea RSI,-020h[RBP] lea RDI,-0D0h[RBP] movsd movsd lea RSI,-0D0h[RBP] mov RDI,-010h[RBP] movsd movsd mov RAX,-010h[RBP] mov RSP,RBP pop RBP ret --- D2.061, OSX 10.8.2
Comment #1 by dransic — 2013-01-16T05:19:13Z
Note, LDC produces equivalent code in both cases: --- fld tbyte ptr 028h[RSP] fld tbyte ptr 018h[RSP] fld ST(0) fmul ST,ST(2) fxch ST(1) faddp ST(2),ST fdivp ST(1),ST fld tbyte ptr 8[RSP] fsubp ST(1),ST fstp tbyte ptr [RDI] mov AX,[0Eh][RIP] mov 0Eh[RDI],AX mov EAX,[0Ah][RIP] mov 0Ah[RDI],EAX ret ---
Comment #2 by bugzilla — 2013-01-24T23:13:00Z
This has nothing to do with inlining; the constructor call is inlined just fine. It also has nothing to do with POD status, although that does illuminate the problem. The problem is that the optimizer does copy propagation only on basic types, not on structs. It needs to do it on structs, too.
Comment #3 by github-bugzilla — 2013-01-26T19:51:13Z
Commit pushed to dmd-1.x at https://github.com/D-Programming-Language/dmd https://github.com/D-Programming-Language/dmd/commit/3e004c4fafcd64eda4e5ed1a257182b8d802b66f fix Issue 9320 - optimizer should do copy propagation on structs, too
Comment #4 by bugzilla — 2013-01-26T19:53:08Z
Comment #5 by github-bugzilla — 2013-01-26T22:38:23Z
Commit pushed to master at https://github.com/D-Programming-Language/dmd https://github.com/D-Programming-Language/dmd/commit/fde7475a6f1837a32977221e9eb22df180920826 Merge pull request #1559 from WalterBright/b34 fix Issue 9320 - optimizer should do copy propagation on structs, too