This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Re: GCC warnings for unused global variables
prj@po.cwru.edu (Paul Jarc) writes:
| Gabriel Dos Reis <gdr@integrable-solutions.net> wrote:
| > Fergus Henderson <fjh@cs.mu.OZ.AU> writes:
| >| I don't think the committee ever intended to allow implementations
| >| to optimize away volatile variables,
| >
| > Why not? The C standard clearly says:
| >
| > [#3] 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).
|
| Accessing a volatile object includes writing, and writing includes
| initializing. So the initialization cannot be optimized out.
Why? the last part of the paragraph quoted above does specifically
permit that optimization where applicable.
| Ignoring for the moment the question of what's allowed, what useful
| purpose would be served by optimizing away a static volatile variable,
| anyway?
Now, we're leaving the C definition land to enter the land of
second-guessing what may or may not be useful. That land is far less
"friendly" than "C definition" land.
When we request the compiler to optimize a program we are requesting
it to transform the program into another one with equivalent
semantics, according to some "optmization criteria" (space or speed or
whatever). The compiler has no notion of useful. So I think the
formulation of the question is not appropriate.
| If the programmer marked the variable as volatile, it seems
| extremely likely that the variable will be used somehow, and removing
| it would cause some sort of breakage.
Well, if the side effect is needed then the compiler won't remove it.
If it is not needed then removing it cannot not possibly make any
difference, any breakage. Or else, there was an unfounded expectation.
-- Gaby