May too many register variables hurt? (was Re: Novice question.)

Martin Weitzel martin at mwtech.UUCP
Sat Nov 24 12:37:26 AEST 1990


In article <14538 at smoke.brl.mil> gwyn at smoke.brl.mil (Doug Gwyn) writes:
>In article <967 at mwtech.UUCP> martin at mwtech.UUCP (I) wrote:
>>In general, my advice is to use no more than two register variables, and
>>only in the *outmost* blocklevel in the body of any function.
>
>Mine is the opposite.
>
>>The order in which the variables are declared is a-b-c, so c will not
>>profit from its storage class.
>
>Sure it will.  Since b and c are declared in separate parallel blocks,
>older-technology compilers such as PCC will share the explicit register
>that is assigned for these two variables.  This is in fact a good way
>to exploit "register" in such compilers.

Though it might be not wise to argue with one of the network gods,
I beg to differ here. (For those who tuned in late: The question is:
If you write code for an unknown compiler, may *too many* register
declarations hurt overall performance.)

My argument was that *without* some internal knowledge of the inner
workings of some (non-optimizing) compiler, it is *not* possible to
choose the appropriate places for more than two or maybe three register 
variables declared in the outmost (function) block.

K&R-1, pg. 193 states concerning register:

	... only the first few of such declarations are effective ...

and on page 81

	... on the PDP-11, only the first three register declarations
	    in a function are effective ...

Now let's assume the implementor of an unknown% compiler has read his K&R-1,
though it might not be alike K&R's PDP-11 compiler, but James McCosh's for
the 6809, which has only *two* free register variables and I determine the
five most heavily used variables in my function as `a', `b', `c', `d', `e'
(in that order, i.e. `e' is less frequently accessed). Further we may have
the following block structure:


	func()
	{
		...d, e, used here ....

		for (......)
		{
			...b, d, used here
			{
				... c used here
			}

			for (......)
			{
				... a, b used here

			}
			...b, d, used here
		}
		... e, used here again
	}

If I follow the simple rule to place all register declarations outside
(at function block level) and to depend not on more than two beeing
effective, I can easily verify that the "right" ones are given. If I
further trust the compiler that it gets K&R-1 right in only obbeying the
declarations that come first, I may even declare all the five variables
with sorage class register (in decreasing order of their access
frequency), not risking that the most important ones are missed. This
would work for the 6809 (2 registers) and the PDP-11 (3 registers).

On the other hand, if I declare the variables at the inner blocks (as
the required scope allows), it may be possible for PCC-like compilers
to share registers between blocks, but it is not possible for me to find
the set of variables which should receive the register attribut, without
knowing the number of available registers: For McCosh's-6809 compiler
(only two registers) I should declare `a', `b', and `c' as register (and
hope that the compiler is PCC-ish enough to share the register for
`a' and `c' between the blocks). On the PDP-11 I could (and propably
should) try to use the third register by also declaring `d' as register,
but that would on the 6809 force the most important variable (`a') out
of the available registers.

----------------------------
%: Hand-optimizing register declarations for a compiler which I know
   very well is quite another topic ...
-- 
Martin Weitzel, email: martin at mwtech.UUCP, voice: 49-(0)6151-6 56 83



More information about the Comp.lang.c mailing list