public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
* Incrementing volatiles?
@ 1998-07-08 14:20 Matthias Urlichs
  1998-07-10  6:04 ` Andreas Schwab
  0 siblings, 1 reply; 27+ messages in thread
From: Matthias Urlichs @ 1998-07-08 14:20 UTC (permalink / raw)
  To: egcs

Hi,

The following code, using egcs-current -O2 -fomit-frame-pointer

         unsigned int foo;    afoo() { foo++; }
volatile unsigned int bar;    abar() { bar++; }

  compiles, on ix86, to

afoo:   incl foo
        ret
abar:   movl bar,%eax
        incl %eax
        movl %eax,bar
        ret

But actually, it should be perfectly valid to compile both to code using
"incr". After all, both versions read the variable, do something with it,
and then write it out.

Is there a way to convince the optimizer to do this?

(The obvious solution, i.e. "(*(unsigned int *)&bar)++", doesn't count. ;-)

-- 
Matthias Urlichs
noris network GmbH

^ permalink raw reply	[flat|nested] 27+ messages in thread
[parent not found: <egcs.199807111302.JAA10664@pincoya.inf.utfsm.cl>]
* Re: Incrementing volatiles?
@ 1998-07-17  8:59 Mike Stump
  0 siblings, 0 replies; 27+ messages in thread
From: Mike Stump @ 1998-07-17  8:59 UTC (permalink / raw)
  To: carlo, egcs

> From: Carlo Wood <carlo@runaway.xs4all.nl>
> Date: Thu, 16 Jul 1998 01:43:20 +0200 (CEST)

> Can someone explain to me what exactly the definition
> of 'volatile' is? :)

Sure, what part was unclear?  While there is some hair surrounding
volatile, I think it is fairly straight forward.

The basics: There is an implementation defined observation point of
the abstract machine that implements the required semantics of a
conforming program.  Volatile is the way to `see' what is going on
with the machine.

From a C++ working paper:

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
  implementations.   In  particular,  they  need not copy or emulate the
  structure of the abstract machine.  Rather, conforming implementations
  are required to emulate (only) the observable behavior of the abstract
  machine as explained below.3)                                           |
  __________________________
  3) This provision is sometimes  called  the  as-if  rule,  because  an
  implementation is free to disregard any requirement of the Standard as
  long as the result is  as if the requirement had been obeyed,  as  far
  as can be determined from the observable behavior of the program.

6 The observable behavior of the abstract machine  is  its  sequence  of
  reads   and   writes  to  volatile  data  and  calls  to  library  I/O
  functions.4)

That's about it.  There are more details, some of them are
obvious:

	volatile int a; a = 1;

represents a `write', whereas

	   b = a;

represents a read (of a).  Not as obvious are things like, if I have
two CPUs can I do s=++i; where i is a shared volatile between the two
CPUs, can I expect this to work?  The answer is usually no.  I say
usually, because a port maintainer is free to require that it work,
and free to ensure it does work, extending the compiler as necesary.
Though, the program that relies upon such a guarantee by a port, isn't
strictly speaking, portable.

^ permalink raw reply	[flat|nested] 27+ messages in thread

end of thread, other threads:[~1998-07-17  8:59 UTC | newest]

Thread overview: 27+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1998-07-08 14:20 Incrementing volatiles? Matthias Urlichs
1998-07-10  6:04 ` Andreas Schwab
1998-07-10 21:04   ` John Vickers
1998-07-13  2:36     ` Philippe De Muyter
1998-07-13  2:40       ` Andreas Schwab
1998-07-13  4:36         ` Philippe De Muyter
1998-07-13  8:48       ` Joe Buck
1998-07-15  6:45         ` Andi Kleen
1998-07-13 12:40       ` Franz Sirl
1998-07-11  6:03   ` Horst von Brand
1998-07-13  1:48     ` Andreas Schwab
1998-07-14 18:59       ` Bill Currie
1998-07-15  1:49         ` Jeffrey A Law
1998-07-15 17:22           ` Bill Currie
1998-07-15 17:22             ` Jeffrey A Law
1998-07-15 17:56               ` Bill Currie
1998-07-15 17:22                 ` Jeffrey A Law
1998-07-15 17:34                 ` Matthias Urlichs
1998-07-15 21:58                   ` Bill Currie
1998-07-16 11:28                   ` Carlo Wood
1998-07-17  4:06                 ` Andreas Schwab
1998-07-15  3:14         ` Joern Rennecke
1998-07-15  3:14         ` Andreas Schwab
1998-07-15 17:22           ` Carlo Wood
1998-07-17  4:10             ` Andreas Schwab
     [not found] <egcs.199807111302.JAA10664@pincoya.inf.utfsm.cl>
1998-07-13  0:06 ` Todd P. Whitesel
1998-07-17  8:59 Mike Stump

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).