Optimization (was Re: volatile)

00704a-Liber nevin1 at ihlpf.ATT.COM
Wed May 4 11:01:07 AEST 1988


In article <511 at wsccs.UUCP> terry at wsccs.UUCP (Every system needs one) writes:

>I didn't say they shouldn't effect the generated program, I said that they
>shouldn't effect the OPERATION of the generated program.

What's the difference between your two statements?  Optimizers, almost by
definition, do effect the operation of the generated program.

Take the following example:

	int bar;
	int foo;
	int temp;
	...
	bar = temp;
	foo = bar;
	...

Unoptimized, what should this produce?

Answer:  something like:

	Take the value stored in temp
	Convert it to int
	Store it in bar
	Return the value stored in bar

	Take the value stored in bar
	Convert it to int
	Store it in foo
	Return the value stored in foo

If *any* of these steps are skipped then I would consider that code to be
optimized.

Here is what optimized code might be:

	Take the value stored in temp
	Store it in foo
	Store it in bar

Should this be an illegal optimization?  No!  

Optimizers, in a nutshell, figure out the *results* of what you coded and
then generate the best possible code to obtain those results.  Optimizers
need not generate code that performs all of the operations in the order that
you requested them, as long as the results, for a correct program, are
unchanged.

Volatile is needed to stop those optimzations since, in some circumstances,
it is required that none of the intermediate operations can be skipped
because outside forces may change some of the variables between
operations.


The problem I have with volatile, as it is defined now, is that it doesn't
really say which optimizations are allowed and which aren't (and yes, I
have read footnote 52 in the Jan 88 dpANS; this statement is not good
enough).  In my unoptimized example, assuming all the variables were
declared volatile, which statements can be legally taken out?  Am I allowed
to get rid of the 'return the value stored in xxx' since this value is
never used?  I don't know.  Also, the augmented assignment operators used
with volatile variables since in order to use them properly I need to know
how many real machine-level references are made to the volatile variable
and if the whole variable is loaded into memory at once (in the case of a
volatile struct, for instance).  The semantics for volatile seem to be
ill-defined, at best.
-- 
 _ __			NEVIN J. LIBER	..!ihnp4!ihlpf!nevin1	(312) 510-6194
' )  )				"The secret compartment of my ring I fill
 /  / _ , __o  ____		 with an Underdog super-energy pill."
/  (_</_\/ <__/ / <_	These are solely MY opinions, not AT&T's, blah blah blah



More information about the Comp.lang.c mailing list