But you could also have something like:
while(true) {
if(some condition) {
do stuff;
break;
}
}
In this case, a compiler might be allowed to assume that 'some condition' happens to be true eventually.But you could also have something like:
while(true) {
if(some condition) {
do stuff;
break;
}
}
In this case, a compiler might be allowed to assume that 'some condition' happens to be true eventually.If "some condition" is actually testing a variable for equality, then you could do constant-folding.
- doesn't terminate, doesn't have side effects -> optimization doesn't matter
- doesn't terminate, has side effects -> optimization isn't valid
- does terminate, doesn't has side effects -> optimization would be valid, but why would such a loop exist? One example that was brought up was generic code, but I'm not entirely convinced yet.
Also the definition of 'side effect' here is a weird one: reading and writing to variables is not considered a side effect, even though a different thread could change them while your loop is running.
(And to make it closer to our situation, assume that the loop being executed was looking for something that actually has a counterexample that could be found after a long time; instead of Fermat's last theorem.)
I suppose that is possible, although I'm having a hard time coming up with any reasonable examples.