volatile, noalias and optimization

Marcus Hall marcus at illusion.UUCP
Sat Apr 30 10:02:47 AEST 1988


In article <135 at atpal.UUCP> tneff at atpal.UUCP (Tom Neff) writes:
>In article <3637 at haddock.ISC.COM> karl at haddock.ima.isc.com (Karl Heuer) asks:
>>In article <132 at atpal.UUCP> uunet!pwcmrd!skipnyc!atpal!tneff (Tom Neff) writes:
>>>This is a wonderful feature ... but shouldn't we use
>>>        #pragma volatile(var1, var2, var3)
>>>instead of imposing a new keyword on the language grammar?
>>
>>Unless you expand on this a bit, it's not as powerful.  I've actually used the
>>declaration "char volatile * volatile p;" (a volatile pointer to volatile
>>memory) -- would your proposal handle this?
>
>You're right, Karl, the #pragma approach wouldn't work for nested volatile
>references like your example, and I can't think of a graceful way to extend
>my proposal to deal with it either.

Well, actually I would think that this could be expressed thusly:

char *p;
#pragma volatile(p, *p)

The problem with this whole approach, however, is that #pragma is explicitly
left undefined in ANSI-C.  Any compiler implementer is free to do whatever
he/she feels like for #pragma.  ANSI-C merely defines #pragma to be a standard
escape to non-standard directives.  Thus, one compiler may implement this in
the method mentioned above, but another may implement it as:

#pragma vol p; *p;

or any number of different ways.  Usage of #pragma is by definition
non-portable.  It is just there for an escape for local additions that may
be important enough to add despite the non-portableness.  A concept of
volatility is important enough (in my opinion, and in quite a few others)
that a standard, portable way of specifying this is desirable.

Most programs can just ignore volatile and everything will work just fine.
Older compilers can just ignore the keyword and assume everything is
volatile just like current compilers.  Smarter compilers can take advantage
of this to avoid re-loading a register with a variable that was already there,
avoid doing extra writes to memory, etc.

Noalias does enable many optimizations, but while most variables are normally
not volatile, most variables ARE noalias.  Variables that should be declared
volatile are easy to identify; globals that are actually memory mapped i/o
registers, variables that are accessed by signal catchers or interrupt
routines, etc.  Maybe it would make more sense to define a keyword "aliased"
which would be tagged onto variables that actually ARE referenced by
multiple means and let the compiler treat variables as noalias unless it
has its address taken or it is declared "aliased".  Of course, globals
would need to be declared "aliased" if its address were taken *anywhere*.

Anyhow, it seems that noalias should be implemented by some compiler somewhere
through a technique similar to what you have proposed before it gets worked
into the standard.  If it really works and there aren't subtle disasters that
arise from its implementation, THEN incorporate it into ANSI-C-rev2.

Marcus Hall
..!{ihnp4,mcdchg}!illusion!marcus



More information about the Comp.lang.c mailing list