Available No. of Registers

Mike Meyer mwm at eris.BERKELEY.EDU
Mon Jan 19 09:52:22 AEST 1987


In article <83 at ucdavis.UUCP> cccmark at deneb.UUCP (Mark Nagel) writes:
>On the machines I've worked on, the register declaration will use up to
>the total available registers on the CPU and then it is ignored (i.e. no
>error, just no register declaration either).  Depending on the compiler,
>the register declaration will do anything from telling the compiler to put
>this variable in an available register or else (Macintosh w/Lightspeed C)
>to strongly advising the compiler to possibly put the variable in a register
>if it wouldn't be too much trouble (VAX/VMS C).  I am not sure of exact
>numbers offhand, but they will vary according to compiler as well as CPU.

There is a false implication in the above: that it doesn't hurt to add
register declerations. There is at least one compiler out there that
effectively allocates registers from the last declared instead of the
first, so that blindly adding registers to code can slow the generatred
code down.

The algorithm I use for allocating registers is as follows: Assign one
register to each heavily-used variable. While there are fewer registers
than N, and there are variables that are touched in loops, or more than
a few times outside of loops, repeat for the next least heavily-used
variables.

N depends on the expected target machines. Since I'm writing for 68K's
and VAXen these days, it's 6. When I wrote for 11's and 4004 family
machines, it was 3. Three isn't enough; I don't very often need more
than 6, though.

If you feel that you need speed badly enough to want to KNOW which
variables go into registers, and can't afford to pull the overhead of
a subroutine, you probably oughta be hand-coding that routine in
assembler for speed anyway.

	<mike



More information about the Comp.lang.c mailing list