[fpc-devel] x86-64 compilation of while loops

Sven Barth pascaldragon at googlemail.com
Mon May 28 09:13:36 CEST 2018


J. Gareth Moreton <gareth at moreton-family.com> schrieb am Mo., 28. Mai 2018,
02:55:

> In this case, the ".balign" hint adds 2 bytes to pad the loop.  However,
> my question is this... why does it immediately jump to the end of the loop
> to check the condition (which is very likely to be true on the first
> iteration), only to jump to the beginning again? Granted, in this case the
> condition is relatively simple, but why not simply check the condition
> where the "jmp" instruction is, and jumping to the line after the "jl"
> instruction if it's false? For example:
>
>     xorl    %r10d,%r10d // <-- The line that appears before "jmp .Lj6"
>     movl    $1,%r11d    // <-- Optimised from "movl %r10d,%eax", "leaq
> 1(%rax),%r11", since %r10d, and hence %rax, are known to be zero
>     movl    %edx,%eax
>     cmpq    %rax,%r11
>     jge  .Lj7 // <-- Or whatever label is given for a position right after
> "jl .Lj5"
>     .balign 8,0x90
> .Lj5:
>     ...
>
> In this case, no jumps occur upon entering the while loop when the
> condition is true.
>
> (Ignore the fact that there could be a lot of other potential
> optimizations if the constant switching between 32-bit and 64-bit registers
> can be sorted)
>

The following is only guess work of mine: I think that the idea is to avoid
a jump back once the condition is false. Also it might be too keep the
difference between "while ... do ..." and "repeat ... until ..." minimal
(namely only that initial jump).

Speaking of which: if you assume that the list isn't empty and thus the
loop is entered at least once why don't you use a repeat-loop instead of a
while-loop?

Regards,
Sven

>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-devel/attachments/20180528/5e89b1f5/attachment.html>


More information about the fpc-devel mailing list