Search code examples
coptimizationlanguage-lawyervolatile

Optimization allowed on volatile objects


From ISO/IEC 9899:201x section 5.1.2.3 Program execution paragraph 4:

In the abstract machine, all expressions are evaluated as specified by the semantics. An actual implementation need not evaluate part of an expression if it can deduce that its value is not used and that no needed side effects are produced (including any caused by calling a function or accessing a volatile object).

What exactly is the allowed optimization here regarding the volatile object? can someone give an example of a volatile access that CAN be optimized away?

Since volatiles access are an observable behaviour (described in paragraph 6) it seems that no optimization can take please regarding volatiles, so, I'm curious to know what optimization is allowed in section 4.


Solution

  • can someone give an example of a volatile access that CAN be optimized away?

    I think that you misinterpreted the text, IMO this paragraph means that

    volatile unsigned int bla = whatever();
    
    if (bla < 0) // the code is not evaluated even if a volatile is involved