C optimizer

Walter Bright bright at Data-IO.COM
Thu Feb 16 05:41:03 AEST 1989


In article <515 at larry.UUCP> jwp at larry.UUCP (Jeffrey W Percival) writes:
>I have a question about how much optimizing I should worry about when
>writing programs in C.
>	x = (1 + cos(r)) / (cos(r) * sin(r));
>	y = (cos(r) - sin(r)) / (1 + sin(r));
>Now, can I expect the compiler to form only one call to sin and
>cos?

In general, function calls are *not* regarded as common subexpressions,
since the compiler usually knows nothing about the function, and it
may have side effects.

The cases where func() *might* be done only once are:
1) If func() is an inline function, like in C++, and the compiler
   recognizes the expansions as being common.
2) If func() is a compiler built-in function, so the compiler knows that
   there are no side effects, and so it's common.
3) If func() is a compiler built-in function, and the
   target machine implements func() as a small number of machine instructions,
   and a peephole optimizer in the code generator commons it.
Obviously, if func() is a user-defined function, and its implementation is
in another file, there is no way that the compiler can recognize it as
common.

Of course, the best way to determine if the compiler makes it common
is to compile it and see!



More information about the Comp.lang.c mailing list