pure functions are tricky
Doug Gwyn
gwyn at smoke.BRL.MIL
Tue Feb 21 09:37:31 AEST 1989
In article <7256 at rosevax.Rosemount.COM> merlyn at ernie.rosemount.com writes:
>Now, should it be possible to fold pure functions with constant arguments?
> x = sin(sqrt(2.0)) ; /* turns into simple assignment to a constant? */
Sure. Why not? If these functions were implemented as compiler intrinsics,
then in fact I would hope that the constant-folding optimization WOULD fold
in the effects of such functions. The only possible glitch would be if the
target environment had different arithmetic properties from the compilation
environment, since the proposed standard requires that compile-time constant
folding be performed with at least as much precision as would occur at run
time. If accurate simulation of the target run-time arithmetic was too
much bother, then I wouldn't really expect the compiler to perform this
level of optimization.
>This seems reasonable and desirable, but what about user defined functions?
>What about pure functions that can only be evaluated at run time, such as a
>function that returns the name of the machine the program is running on?
>Can the compiler determine that one pure function can be evaluated completely
>at compile-time, and another cannot? Or should only the former be considered
>"pure"? What about functions that have a first-time-through flag that, e.g.,
>precomputes a table of factorials? Does this destroy its purity?
It all depends on how intelligent the compiler is. If it is able to pre-run
your program in effect, converting it to a series of printf() statements
that produce the correct results, then more power to it.
The general constraint on optimization is that it is not allowed to affect
any input or output operations, which must occur in the sequence specified
in the original source code with the same observable effects. ANSI C also
requires that accesses to volatile-qualified data occur as specified for
the virtual C machine. Because of the ability to compile modules
separately, this constraint has far-reaching consequences for what is and
is not allowed in the way of optimization. IF a compiler were able to
determine all the modules to be linked into the application "up front",
then it could in theory perform a much higher degree of optimization than
is usually actually implemented. In general this will not be possible.
It is only the fact that in a hosted environment the C library routines
are standardized that permits the compiler to always know their exact
properties.
>Somewhat related, is this a legal optimization?
[Compiler performs specified initialization code in advance.]
Sure. I doubt that any existing production C compilers are that smart.
More information about the Comp.lang.c
mailing list