This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Re: Incrementing volatiles?
- To: egcs at cygnus dot com (egcs at cygnus dot com)
- Subject: Re: Incrementing volatiles?
- From: Carlo Wood <carlo at runaway dot xs4all dot nl>
- Date: Thu, 16 Jul 1998 15:45:49 +0200 (CEST)
| > bar:
| > movl foo,%eax
| > incl foo
| > ret
| >
| Wrong. The foo++ means "read it, perhaps do something with the value,
| increment the value, put it back". Your assembly code reads the value
| twice.
|
| > Now, if I'm really off track, can someone please give me some pointers
| > to information that will set me right?
| >
| "volatile" is massively undefined. My off-the-seat-of-my-pants definition
| is that volatile variables are _always_ accessed exactly as many times, and
| in exactly that order, as described in the C source code.
|
| How to tell the backend (Intel or otherwise) that sometimes(!) it can
| combine a read-add_one-write insn sequence into one "incr", even if the
| to-be-incremented thing in question is marked as volatile, is an
| interesting question. IMHO, however, "two volatiles never match" is a bit
| too strong.
Thanks for the definition :)
Let us assume that volatile `foo' in the above example is a r/w register
of some hardware IC that generates pseudo random numbers from its own
value every time it is being read; you are allowed to write to it however
to set a 'seed'. That is an example that would make "volatile" pretty
clear, if I understood it well. It shows clearly that
volatile int foo;
int random(void) {
return foo++;
}
Should read one pseudo random number, increment it and write it back as seed.
That means indeed that
random:
movl foo,%eax
incl foo
ret
is not a correct way of generating the assembly code.
It should be generated like:
random:
movl foo,%eax
movl %eax,%edx
incl %edx
movl %edx,foo
ret
Note that currently, with -O9 -fomit-frame-pointer it generates less
optimized code:
random:
movl foo,%eax
movl %eax,%edx
incl %eax
movl %eax,foo
movl %edx,%eax
ret
And without -fomit-frame-pointer it does something totally redundant
things with %ebp and the stack :/
random:
pushl %ebp } Do we want this with -O9 ?
movl %esp,%ebp }
movl foo,%eax
movl %eax,%edx
incl %eax
movl %eax,foo
movl %edx,%eax
movl %ebp,%esp }
popl %ebp }
ret
How hard would it be to optimize:
movl %eax,%edx
incl %eax
movl %eax,foo
movl %edx,%eax
ret
to:
movl %eax,%edx
incl %edx
movl %edx,foo
ret
--
Carlo Wood <carlo@runaway.xs4all.nl>