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


On 01-May-2003, Richard Henderson <rth@redhat.com> wrote:
> On Thu, May 01, 2003 at 08:47:03PM -0700, Geoff Keating wrote:
> > For instance, at program startup (in this case) the abstract machine
> > says the string contains a particular value, and how can it do that if
> > it is optimized away?
> 
> Ok, I'll play your game.  By the as-if rule, it *does*
> have that value.  Any time the program cares to check
> that'll be true.

The "as-if" rule (C99 5.1.2.3, C++98 1.9) only applies to non-volatile
objects.  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
to ensure that "At sequence points, volatile objects are stable in the
sense that previous evaluations/accesses are complete..." (C99 5.1.2.3
paragraph 5, C++98 1.9 paragraph 11).

In general the wording of the C and C++ standards about the semantics of
volatile is pretty vague (e.g. what constitutes "access" to a volatile
object is implementation-defined) and so there may perhaps be room for a
weasely interpretation which allows what you seem to want.  But performing
such an optimization would be a clear violation of the intent and spirit
of these standards with respect to volatile.  The purpose of volatile is
to precisely to suppress optimizations like these, and any implementation
which optimizes away volatile variables is at best greatly lacking in
quality-of-implementation, if not in outright violation of the standard.

-- 
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.


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