Compiler optimization is generally implemented using a sequence of optimizing transformations, algorithms which take a program and transform it to produce a semantically equivalent output program that uses fewer resources or executes faster.
Use the volatile keyword when declaring variables that the compiler must not optimize. If you do not use the volatile keyword where it is needed, then the compiler might optimize accesses to the variable and generate unintended code or remove intended functionality.
The volatile keyword is intended to prevent the compiler from applying any optimizations on objects that can change in ways that cannot be determined by the compiler. Objects declared as volatile are omitted from optimization because their values can be changed by code outside the scope of current code at any time.
Use -O0 to disable them and use -S to output assembly. -O3 is the highest level of optimization. Starting with gcc 4.8 the optimization level -Og is available. It enables optimizations that do not interfere with debugging and is the recommended default for the standard edit-compile-debug cycle.
While debugging some legacy code I stumbled upon surprising (for me) compiler behavior. Now I'd like to know whether any clause in the C++ spec allows the following optimization, where side effects from a function call on the for-condition are ignored:
void bar() { extern int upper_bound; upper_bound--; } void foo() { extern int upper_bound; // from some other translation unit, initially ~ 10 for (int i = 0; i < upper_bound; ) { bar(); } }
In the resulting dissambly there is a control path in which upper_bound
is preserved in a register and the decrement of upper_bound
in bar()
never takes effect.
My compiler is Microsoft Visual C++ 11.00.60610.1.
Honestly I don't see much wiggle room in 6.5.3 and 6.5.1 of N3242 but I want to be sure that I'm not missing something obvious.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With