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/22/2007 23:14:38
>>    x = ...something...;
>>    somefunction();
>>    y = x;

>> [...] because, in terms of the C abstract machine, somefunction()
>> could modify x.

> Uh, no, I don't think so.  In the case of:

> void
> foo(int *addr)
> {
> 	int x, y;
> 
> 	x = *addr;
> 	somefunction();
> 	y = x;
> }

This is one of the cases I mentioned where somefunction() can be
proven to not modify x (and trivially so - it cannot refer to x at
all), making the optimization of holding x's value in a register across
the call a valid one, since x is not declared volatile.

> "x" is not memory, "*addr" is memory.

x normally *is* memory; it's just on the stack (sometimes optimized
into registers when real memory can be shown to be unnecessary).  The
value stored in x has to be stored *somewhere*, after all.

Since x is provably inaccessible to somefunction(), the compiler is
allowed to use a temporary register here, a la

	temp_reg = *addr;
	x = temp_reg;
	somefunction();
	y = temp_reg;

If x were accessible, or potentially accessible, to somefunction(),
this would be an invalid optimization.  Consider, for example,

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, because x is, or at least
might be, accessible to somefunction().  (If somefunction can be proven
to not modify x, the optimization is still valid.  This normally will
require that somefunction's source be visible to the compiler at this
point, but I can imagine a few possible alternatives.)

/~\ 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