[fpc-pascal] Efficiency of generated code [x86_64]

Peter peter at pblackman.plus.com
Fri Jun 24 19:16:29 CEST 2011


I'm puzzled by some of the code generated for x64. Came across this 
earlier post;

Compiling the simple example with the loop in a function I get much 
leaner & meaner than the [i386] assembler in the original post, but I 
had to use O3 and that separate function to get full use of xmm 
registers instead of the stack.

Program tttt;

Function loop (A,B : double) : double;
Var X : LongInt;
     For X := 0  to 10000000 do
         A := A + X;
         A := A * B;
     loop := A;

Var A,B : double;
     A := 0;
     B := 0.9;
     loop (A,B);
     WRITELN (loop (A,B):0:9);

Looking at the assembler loop code

# Var A located in register xmm0
# Var B located in register xmm1
# Var $result located in register xmm0
# Var X located in register eax    //   AND xmm2 !

# [7] For X := 0  to 10000000 do
     movl    $0,%eax
     decl    %eax
     .balign 4,0x90
     incl    %eax
# [9] A := A + X;
     cvtsi2sdl    %eax,%xmm2
     addsd    %xmm0,%xmm2
     movsd    %xmm2,%xmm0
# [10] A := A * B;
     movsd    %xmm0,%xmm2
     mulsd    %xmm1,%xmm2
     movsd    %xmm2,%xmm0
     cmpl    $10000000,%eax
     jl    .Lj7
# [14] end;
     movsd    %xmm0,%xmm0
     addq    $24,%rsp

I am wondering what is the point of all the xmm2 stuff, apart from the 
initial transfer of X from %eax?  I can't see the point of it. Not set 
any debugging options.  What is wrong with the following?

# [9] A := A + X;
     cvtsi2sdl  %eax,%xmm2
     addsd  %xmm2,%xmm0
# [10] A := A * B;
     mulsd  %xmm1,%xmm0
     cmpl    $10000000,%eax
     jl    .Lj7
# [14] end;

Also puzzled by the final
movsd %xmm0,%xmm0
What does this do?

I would really like to be able to generate optimal (ie minimal) xmm code 
from Pascal without dropping into assembler. Are there any other 
compiler switches that would help?

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-pascal/attachments/20110624/5e15c61f/attachment.html>

More information about the fpc-pascal mailing list