Trevor Pering <pering@tongass.eecs.berkeley.edu> wrote:>I am working on an optimizing phase for the Sather compiler.>(http://icsi.berkeley.edu/Sather) The Sather compiler compiles to ANSI>C, and lets a C compiler finish the job off. My optimizer is>concerned with optimizing expressions across procedure calls....>>if it can statically determine that foo() cannot in anyway change the>result of x->y (this optimization is difficult for the compiler, if>not impossible, because foo() may have side-effects)...

Many compilers are aware of this, and allow you to tell them this
difficult fact, so that the compiler doesn't have to determine it
statically.

Here is an excerpt from the SGI includes:
#pragma no side effects (log)

You might also want to look at the gcc 'const' attribute:
---
Many functions do not examine any values except their arguments,
and have no effects except the return value. Such a function can
be subject to common subexpression elimination and loop
optimization just as an arithmetic operator would be. These
functions should be declared with the attribute `const'. For
example,

int square (int) __attribute__ ((const));

says that the hypothetical function `square' is safe to call fewer
times than the program says.
---

of course, 'no side effects' is probably a bit too strong for most
uses; one is really interested in whether there are any _relevant_
side effects.

One can help the compiler out in some cases:

za = x->y;
... za ...;
foo();
... za ...;

The compiler now knows it can safely register-ize za (assuming it's
address is never taken). Generally, tho, C code generated from other
languages is tough to optimize since the 'primitives' of the language
being generated are often function calls, giving the optimizer fits,
and and the optimizer has a much harder time detecting the 'structure'
of the code as this has been lost in the conversion.