This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Re: GCC warnings for unused global variables
Gabriel Dos Reis <gdr@integrable-solutions.net> wrote:
> prj@po.cwru.edu (Paul Jarc) writes:
>| Gabriel Dos Reis <gdr@integrable-solutions.net> wrote:
>|> Fergus Henderson <fjh@cs.mu.OZ.AU> writes:
>|> [#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?
Because, since the object is volatile, it may affect the program in
ways unknown to the compiler.
> 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.
The compiler writers do.
>| 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.
I agree with that precise wording, but note that this is different
from "if the compiler knows that the side effect is needed, then the
compiler win't remove it".
If volatile is used, then the compiler cannot know whether accesses
and side effects are needed. (This is exactly what is expressed by
volatile.) Do you agree or disagree?
If the compiler does not know whether accesses and side effects are
needed, then it must not optimize then away. Do you agree or
disagree?
paul