This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: GCC warnings for unused global variables


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.

Ignoring for the moment the question of what's allowed, what useful
purpose would be served by optimizing away a static volatile variable,
anyway?  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.  So it seems like a bad idea to
do that, regardless of whether the standard permits it.  Certainly it
can happen that the variable really is entirely unused despite being
volatile, but gcc can't know when that is the case.


paul


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]