This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Re: GCC warnings for unused global variables
On 02-May-2003, Gabriel Dos Reis <gdr@integrable-solutions.net> wrote:
> Fergus Henderson <fjh@cs.mu.OZ.AU> writes:
> | The "as-if" rule (C99 5.1.2.3, C++98 1.9) only applies to non-volatile
> | objects.
>
> I disagree. See below.
>
> | The part of C and C++ standards which defines the "as-if"
> | rule specifically requires implementations to not optimize away accesses
> | to volatile variables (C99 5.1.2.3, C++98 1.9 paragraphs 1 and 7), and
>
> C++98 1.9/1:
>
> The semantic descriptions in this International Standard define a
> parameterized nondeterministic abstract machine. This International
> Standard places no requirement on the structure of conforming
> implementa-tions. In particular, they need not copy or emulate the
> structure of the abstract machine. Rather, conform-ing
> implementations are required to emulate (only) the observable
> behavior of the abstract machine as explained below.5)
>
> There is nothing there that prevents the compiler to apply the "as-if"
> rule to to volatile objects. The only thinr that is required is to
> emulate the observable behavuour". The implementation is free to
> implement whatever it likes as far as the observable behaviour is
> preserved.
But reads and writes to volatile variables are defined to be *part of*
"the observable behaviour" (C++98 1.9 paragraph 6). So they can't be
optimized away under the as-if rule.
Initialization of a volatile is a write to a volatile variable, so it
is part of the "observable behaviour", and all variables with static
storage get initialized (even if it is just zero-initialization).
Hence the compiler is not allowed to optimize away any volatile variable
with static storage duration.
If you want to take a very weasely interpretation, then you could say that
although _reads and writes_ to volatile variables are part of the observable
behaviour (and hence cannot be optimize away by the "as-if" rule),
the _presence_ of volatile variables is not. However, this argument would
only work for volatile variables which are not initialized, e.g.
volatiles with automatic storage duration which are declared with
no initializer and which are never read or written. Furthermore, I
think that this would be an overly legalistic interpretation which
ignored the intent of volatile. I don't think the committee ever
intended to allow implementations to optimize away volatile variables,
and so the lack of clear wording in the current standard explicitly
prohibiting this may be just oversight.
--
Fergus Henderson <fjh@cs.mu.oz.au> | "I have always known that the pursuit
The University of Melbourne | of excellence is a lethal habit"
WWW: <http://www.cs.mu.oz.au/~fjh> | -- the last words of T. S. Garp.