Hacker News new | ask | show | jobs
by Pinus 2915 days ago
I, too, scratched my head over that for a while, and eventually realized that I had probably misunderstood what the authors are trying to say.

I think what they mean is that if the compiler has analyzed the code, not shown in the example, that comes before the if statement, and concluded that unless something undefined happens, cond can only be 0 or 1, then it can optimize out the condition.

They are not saying that the code actually shown is enough to conclude that cond must be 0 or 1.

1 comments

Yeah re-reading the wording now I think you're right. It's this part that throws me off:

> could determine that, in the absence of any undefined behavior

"could determine that" based on the code example shown

vs

"could determine that" based on static analysis performed on some preceding code

It would have been a lot easier to wrap my head around if it were an example where cond could be 0 or 4 or something along those lines. It would really underscore the compiler's desire to reuse the cond as the index.