Hacker News new | ask | show | jobs
by pingyong 2289 days ago
Okay and which optimization does this allow for? What's the difference in the emitted binary?
2 comments

If you propagate "some condition," that could allow dead-code elimination later on, especially if an if-statement uses "some condition" as a predicate later. This seems redundant in hand-written code, but keep in mind the if-statement may actually be from an inlined function.

If "some condition" is actually testing a variable for equality, then you could do constant-folding.

My point is that if a loop

- 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.

Because of interaction with other undefined behaviour (in the body of your loop), you might think you are in a different case than what the compiler thinks.

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.

See https://blog.regehr.org/archives/140 for a longer discussion.

(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.)

What point is there in deleting side-effect free loops though? Literally the only use for those is to trap the control flow.
The only intentional use. However when writing generic code you often have code lines that after several levels of code inline reduce to that example.
So, generic code with a loop, which, depending on parameters, sometimes has side effects and sometimes doesn't, and in the case that it doesn't have side effects it is still not easy to recognize if the loop terminates?

I suppose that is possible, although I'm having a hard time coming up with any reasonable examples.