The & (address) operator and register allocation

Doug Gwyn gwyn at smoke.BRL.MIL
Sat Dec 3 06:54:01 AEST 1988


In article <1224 at cps3xx.UUCP> rang at cpswh.cps.msu.edu (Anton Rang) writes:
>	scanf("%d", &N);
>	for (i=0; i<N; i++) { ... }
>would the final program have to read N from memory each time?  Or does
>the C standard provide that some keyword (like 'volatile') be used to
>avoid possibly unsafe optimizations (like moving N into a register in
>the above example--the address of N could have been saved in scanf() ).

Trying to answer what it is that I think you're really asking:

1.  The C compiler code generator must allocate actual (non-register)
storage for anything whose address is needed, so in the above example
N's primary definition must be in addressable memory.  However, a good
optimizer can copy that location into a register for the loop test, and
if nothing inside the loop could possibly (according to the C virtual
machine model) alter the contents of N, it need not reload that cached
value from memory for each test.  There are optimizers that do this.

2.  scanf() is required to look at its actual arguments each time it is
called -- that's the whole point of function parameters.

3.  It is true that functions, being separately compilable entities,
can be designed so that they stash away pointers for later use, and
that so-called "aliasing" (multiple possible reference paths to the
same object) is a factor that has to be dealt with when designing a
C implementation.  The best example is a function having two pointer
parameters.  What if it is called with both parameters pointing to
the same object?  The rules of C require that each access via one of
the parameters be considered as "killing" the value accessed via the
other parameter, so that the other must be "reloaded" the next time
it is used.  The ill-fated "noalias" type qualifier was intended to
provide a way for the programmer to promise that such pointers would
NOT access overlapping objects, so that the cached contents accessed
via the two pointers would be known to be independently modifiable
without interfering with each other.  That would have allowed a
higher degree of optimization, especially on vector architectures
like many supercomputers.  As it now stands (without "noalias"), a
conforming implementation must make a worst-case assumption and
handle possible aliasing correctly.  No keyword is needed to specify
this; it's the default (and now the only) behavior.

>  Are there any architectures which allow taking the "address" of a
>register (say, having a reserved page)?

Registers also have memory addresses on some architectures, for example
DEC PDP-11.  I've never heard of a compiler exploiting this.



More information about the Comp.lang.c mailing list