Available No. of Registers

Henry Spencer henry at utzoo.UUCP
Sat Jan 31 06:45:07 AEST 1987


The approach I use to registers was chosen based on three facts:

1. Many of the machines my stuff is going to run on -- including the one
	that is going to be my primary machine soon -- have many registers.

2. Some of the machines, however -- including the one that is my primary
	machine right now -- have few registers.

3. In general you cannot trust the compiler to be predictable in picking
	specific "register" variables to actually go into registers.  There
	are too many complications (e.g. the 68000's which have two flavors
	of registers).

So if you read my code, you'll find me using both "register" and "REGISTER"
in declarations.  You will find "register" on about three variables per
function, which is a not-uncommon number on register-poor machines (e.g.
the pdp11/44 on which I write this).  You will find "REGISTER" on the rest
of the heavily-used variables (or all variables in functions that don't have
many local variables).  Up at the top of the code you'll find:

	#ifndef REGISTER
	#define	REGISTER	register
	#endif

and in the Makefile you'll find instructions saying "on a register-poor
machine, put '-DREGISTER=' in CFLAGS".  (This could be the other way 'round,
but on the whole I prefer to consider "good" machines, e.g. register-rich
ones here, the default and make the "poor" machines go through the hassle
of having to explicitly compensate.)

Which variables get which?  A somewhat ad-hoc decision, normally made during
final review of working code rather than at code-writing time.  Frequently-
used variables, especially ones used in loops, get priority.  Pointers used
with the -> operator generally get priority over numeric variables, since
using -> with a non-register pointer is often relatively expensive.  Longs
get a slight penalty, since my 44 can't put them in registers anyway.
Parameters get a slight penalty, since putting one of them in a register
often involves more startup overhead than putting a local variable in a
register.  Anything whose address is taken, of course, gets neither form
of register prefix.

One could arguably do better with multiple classes of registers, to express
priorities in more detail.  In practice I seldom have enough local variables
to make this worthwhile, and I doubt that it can be done well enough to show
much consistent benefit across a wide range of hardware.
-- 
Legalize			Henry Spencer @ U of Toronto Zoology
freedom!			{allegra,ihnp4,decvax,pyramid}!utzoo!henry



More information about the Comp.lang.c mailing list