[J3] function references and optimization

Toon Moene toon at moene.org
Thu Oct 11 17:18:50 EDT 2018


On 10/10/2018 02:26 AM, Malcolm Cohen via J3 wrote:

> Robert Corbett writes:
> 
> <<<
> 
> Suppose
> a program contains the statement
> function
> 
> f(x) = y
> 
> and the assignment statement
> 
> x = y - f(1.0)
> 
> Does the mathematical equivalence
> rule allow the expression to be
> replaced with 0.0?
> 
>  >>>
> 
> Yes.
> 
> No matter what kind of function F is, if the processor “knows” that the 
> value of the function is y, it need not evaluate the function reference 
> at all.
> 
> This reduces the statement to the well-known
> 
> x = y – y
> 
> and by the mathematical equivalence rule, y-y is zero so no need to 
> evaluate Y either, giving us x = zero.
> 
> It is absolutely standard-conforming to do mathematically equivalent 
> transformations.

On the GNU Fortran mailing list we had a similar, absolutely 
unproductive, discussion a few months ago, where some of the 
participants believed that short circuiting the following logical 
expression:

A = .FALSE.
IF (A .AND. F(X)) THEN ...

(with A and F being logicals) was a huge problem with the language, 
because Fortran now had impure functions (which meant that doing this 
with or without short circuiting would yield different results).

But this isn't a problem of functions, it's a feature of expressions.

It would just as well be a problem if the programmer depended with his 
life on the expression:

0 * IF(X)

with IF being an integer function, to lead to the evaluation of the 
function.

This has been the behavior of expressions in Fortran (as I showed by 
quoting the Fortran 77 standard, and Nick Maclaren by quoting the 
Fortran 66 standard) for a long, long time.

People who find this a problem probably are using a hammer to drive a 
screw into the wall.

Kind regards,

-- 
Toon Moene - e-mail: toon at moene.org - phone: +31 346 214290
Saturnushof 14, 3738 XG  Maartensdijk, The Netherlands
At home: http://moene.org/~toon/; weather: http://moene.org/~hirlam/
Progress of GNU Fortran: http://gcc.gnu.org/wiki/GFortran#news


More information about the J3 mailing list