question on optimization
as far as i know, a c++ compiler inputs c++ code, optimizes, converts it to assembly code, optimizes again, and then assembles into opcodes.
how does optimization work? here's some code:
in assembly it's probably something close to:
a = b;
c = b;
b = a; //swaps b and c
b = b + 1;
does the optimizer just search for pre-defined patterns of logic? does it search for any kind of redundancy? how effective are the optimizations?
xchg ebx, ecx;
i always thought that the compiler outputted optimized code .... also, theres one little state that you forgot, the code is turned into and object once compiled. then its linked.
let me rephrase:
when optimizations are done, does the compiler search for redundancies of any kind, or just look for patterns of predefined redundant logic?
maybe a combination of both ...?
The compiler optimizes the code AFTER it has been converted to assembly. And the method/degree of optimization depends on the compiler, so it's hard to define what/how the optimization is being performed. I always turn off optimization for the simple fact that I would rather do it myself ;) - hey, if I write ****ty/slow code, that's my fault! See, if the compiler optimizes erroneously (ie: inside a thread, etc) then you might have some mysterious bugs on your hands! Nonetheless, the 'volatile' keyword is there to protect against these things from happening to optimized code (usually...)