* Joshua Maurice, on 30.08.2010 02:47:
Note that no-one has presented an example of actual optimization.
That's because it's impossible to present such an example, because the purported
"optimization" is *not* an optimization, it's just an idiocy.
For James' purported example above, if the compiler can prove that the condition
expression has no side effects and depends on nothing external, then it's proved
that the expression is constant, and what the value is, and so it's already
proved whether the loop terminates or not. So there's no need for undefined for
loops that nver termiante, in this purported (but not actual) examples.
There's nothing "hard" about it, at all: its *trivial*.
I wouldn't say that...
Let's take a simple pseudocode example:
for all 4-tuples (A, B, C, N) over (positive Natural Numbers)^4,
with N > 2
if (pow(A, N) == pow(B, N) + pow(C, N))
break;
print "hello world!"
Here, we're basically exhaustively testing whether Fermat's Last
Theorem is true by testing all possible couterexamples, one at a
time.
The loop can be shown to be free of "side effects", including volatile
reads and writes, io lib calls, synchronization, etc. If the loop does
terminate, then the entire loop should be optimized away. If the loop
does not terminate, then an infinite loop of some variety should
remain.
As a compiler, there's basically no way for it to determine whether
the loop terminates or not. It may terminate. It may not. (We have the
benefit of knowing that it does, but only because of some particularly
new and advanced math.)
I see some benefit in allowing a compiler to auto-parallelize the
print "hello world!" with the earlier calculation if the calculation
can be shown to terminate and does not affect the print (such as this
example). I even see some benefit requiring all loops to terminate to
facilitate this "optimization".
However, my gut reaction is that the penalties far outweigh the
benefits. This breaks past sane conforming code, this is particularly
counter-intuitive, and I have yet to see any real world examples which
would actually benefit from this.
Moreover, even if there was such a real world example, a compiler
issuing a warning that it found an infinite loop would be far
preferable to it silently removing the loop entirely.