[fpc-pascal] code optimization
Adrian Veith
adrian at veith-system.de
Fri Sep 24 16:24:15 CEST 2010
On 24.09.2010 14:35, Jonas Maebe wrote:
>
> On 24 Sep 2010, at 11:48, Adrian Veith wrote:
>
>> Changing to pointers reduces the amount of multiplications for accessing
>> the nth element in an array - if you compare the delphi code to th fpc
>> code on assembler base, this is the main difference in both generated
>> codes.
>
> Did you actually try replacing only the multiplications with lea's in
> the assembler code generated by FPC (one lea to multiply by 5 and then
> the times 4 during the load/store)? I did before posting my initial
> reply because it also seemed to be the most logical explanation to me.
> It turned out to be a red herring:
>
> With imull $20:
> # iterations: 26662054
> no solution found
> runtime: 10.75s
>
> With "lea (%reg,%reg,4),%reg" followed by "movl (%xxx,%reg,4),%yyy"
> (not just for mov, but for every single memory expression that depends
> on an "imull $20"):
> # iterations: 26662054
> no solution found
> runtime: 10.06s
>
> Kylix 3 (~ Delphi 6.5):
> # iterations: 26662054
> no solution found
> runtime: 6.65s
I must confess - I did not - but I will because that's interesting.
Maybe this will behave different on different CPUs, because the picture
is very different on my I7 compared to the older AMD.
fpc:
on i7:
orig: 9s
opt1: 7s = -22%
opt2: 6s = -14% total -33%
on AMD:
orig: 17s
opt1: 12s = -30%
opt2: 7s = -41% total -58%
so the older AMD suffers much more from the not optimized code than the
newer i7. At the end they are almost on the same speed.
same code with delphi (5):
on i7:
orig: 6s
opt1: 5.5s = -8%
opt2: 4.5s = -18% total -25%
on AMD:
orig: 9s
opt1: 8s = -11%
opt2: 6s = -25% total -33%
Strange picture (I did all tests 3 times and took the medium).
>
>> Register allocation is on a comparable level for both versions.
>
> Delphi keeps the "Bar" pointer in a register, while FPC spills it to
> the stack. Because Bar is used in most of the most-executed
> statements, this has a huge impact.
>
you got me ;-)
More information about the fpc-pascal
mailing list