[fpc-devel] Optimization theory

Christo Crause christo.crause at gmail.com
Sun Jun 17 11:08:53 CEST 2018


The penalty for separate div & mod operations for your scenario is severe
on targets that require software implementation (e.g. 8 bit AVR). There is
a DivMod implementation in unit math, would be useful if this is available
in system.

On Sun, 17 Jun 2018, 00:22 J. Gareth Moreton, <gareth at moreton-family.com>
wrote:

>
> There are also situations in optimization where one might detect a repeat
> calculation that the programmer cannot eliminate themselves, but which the
> best optimization is to store the result and re-use it later... the most
> obvious situation where this arises is with div and mod with the same
> numerator and denominator.  Currently, the compiler doesn't know any better
> and has to calculate the division twice, a relatively expensive operation,
> even though DIV returns both the quotient and the remainder in RAX and RDX
> respectively.  I believe storing the mod result in a virtual register for
> later use will be far easier to manage when the registers have not yet been
> allocated, especially if it's determined that a new register has to be
> preserved in the function prologue, or there are no free registers at all
> and it has to be put on the stack, or if it's at all possible to use RDX
> itself as that temporary storage (the most ideal outcome in both speed and
> size), something that would be near impossible if RDX has been allocated
> for something in between.
>
> Gareth aka. Kit
> _______________________________________________
> fpc-devel maillist  -  fpc-devel at lists.freepascal.org
> http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-devel
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-devel/attachments/20180617/77447823/attachment.html>


More information about the fpc-devel mailing list