Hacker News new | past | comments | ask | show | jobs | submit login

Well so gcc -O2 will just replace the loop with s = NUMBER.



I also confirmed this behavior with disassembly, and now I'm wondering what the basis of this optimization is. Can anyone explain the logic that gcc would have used to do this?

As a human, I can see that the loop has no effects except to increase s by 1 each of NUMBER times, and that the result of doing so would increase s by 1 * NUMBER, and that s started as 0, so s ends up as NUMBER. (Also the loop variable i is incremented NUMBER times so it ends up as NUMBER too, but its value is never referred to again.)

I'm wondering how the optimizer observed and represented these facts, and how much more general its attempts at optimizing this loop were.


Not 100% sure but this precise optimization for LLVM is probably part of ScalarEvolution, which tries to figure out how values in a loop evolve: http://llvm.org/devmtg/2009-10/ScalarEvolutionAndLoopOptimiz...

LLVM also has a pass dedicated to detecting loop idioms: http://llvm.org/docs/doxygen/html/LoopIdiomRecognize_8cpp_so...

(I'm not at all familiar with how GCC is architected, sorry)


Cool, thanks for the references!




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: