Subject: Re: USB stack needs early review (Re: Someone should fix our USB stack...)
To: None <tech-kern@NetBSD.org>
From: der Mouse <mouse@Rodents.Montreal.QC.CA>
List: tech-kern
Date: 03/23/2007 12:02:16
>> int x;
>> void foo(void) {
>> 	int y;  x = ...;  somefunction();  y = x;
>> }
>> Optimizing this by holding the value stored into x in a register
>> across the call to somefunction is invalid here
> What about 'static int x;' and somefunction() being external to this
> compilation unit?

This is in principle very much like the case with "int x" being a
function-local variable, but in practice it is significantly harder.
It's a valid optimization only when x's address is never taken.
Compilers will often crunch on an entire function at once and thus will
know, when optimizing this sequence, whether x's address is taken
anywhere.  But they usually don't crunch on an entire file at once and
thus usually won't be able to prove, when optimizing this sequence,
that x's address is never taken anywhere in the whole file.  A compiler
that did digest a file at a time would be reasonably able to make this
optimization for file-scope x.

Actually, the condition can be not "x's address is never taken", but
"x's address can't get into somefunction's hands".  The former is just
a reasonable, and much easier to test, subset of the latter.  And if
enough information about somefunction is visible to the compiler, it
may be possible to prove that somefunction can't modify x for other
reasons, in which case the optimization is equally valid.

/~\ The ASCII				der Mouse
\ / Ribbon Campaign
 X  Against HTML	       mouse@rodents.montreal.qc.ca
/ \ Email!	     7D C8 61 52 5D E7 2D 39  4E F1 31 3E E8 B3 27 4B