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

leland.f.derbenwick lfd at cbnewsm.att.com
Thu Nov 22 09:19:08 AEST 1990


In article <967 at mwtech.UUCP>, martin at mwtech.UUCP (Martin Weitzel) writes:
> 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.
> 
> Why? Listen:
> 
> If you have a modern, optimizing compiler, it will ignore the register
> keyword and make independent decissions about use of CPU-registers. If
> you have modern hardware with many available registers, most probably
> you also have a modern compiler, so there's no reason to worry about having
> defined "not enough" register variables.

Modern hardware.  Such as the entire VAX line, Intel 80x86, Motorola
680x0, etc.?  None of these have "many" available registers, yet all
are certainly modern in the sense that they are currently used and
sold in large quantities.  And while commonly available compilers for
these do vary in their optimization quality, none that I've used has
come close to the levels of optimization being applied to RISC
architectures.

So let's turn the proposed advice around.  Unless you _know_ that your
code is being written _only_ for a processor with lots of registers and
a smart compiler, and that it will never be ported to any of today's
common processors, use register declarations generously -- typically
from 1 up to 4 or 5 in each function will _help_, not hurt.

(Alternatively, the performance of your code may be irrelevant to you.
This _does_ occur in practice: in some code I worked on several years
ago to run on IBM mainframes, the _only_ relevant optimization was
reducing the number of data base accesses -- everything else was so
fast by comparison that it was lost in the noise.)

Martin Weitzel's advice not to declare inner-block variables as register
is good -- some un-smart compilers ignore them; others limit their
optimizizations near them; some handle them well.  It's a gamble.

Using register declarations will _never_ interfere with a smart
optimizing compiler.  A register declaration is a suggestion, not an
absolute: the compiler is perfectly free to ignore it in order to do
other optimizations.  (It is also a promise: you will never take the
address of a register variable.)  Even as far back as K&R I, "A
register declaration is best thought of as an auto declaration,
together with a hint to the compiler that the variables declared will
be heavily used."  It's a hint that a good compiler can use, but that
it will ignore if better optimizations are available.  (Anyone who
writes a compiler capable of doing optimal register allocation on its
own had _better_ make it ignore register declarations!)

 -- Speaking strictly for myself,
 --   Lee Derbenwick, AT&T Bell Laboratories, Warren, NJ
 --   lfd at cbnewsm.ATT.COM  or  <wherever>!att!cbnewsm!lfd



More information about the Comp.lang.c mailing list