Register variables

S

sowmiyakc18

Please clear my doubt. When do we declare a variable to be a register
variable? What are the conditions to be adhered to when register
variables are passed between functions?
 
C

Chris Dollin

Please clear my doubt. When do we declare a variable to be a register
variable?

Almost never.

It's an optimisation-for-speed hint that's unnecessary for
many modern compilers. So you only require it when (a) the
variable appears in a bottleneck function [1] and the
compiler doesn't implement it in a register and the hint
will make the compiler put it in a register and won't
simultaneously make the rest of the function too slow.
Or perhaps (b) the compiler needs the hint to generate
half-way decent code anyway. Or (c, OT) to force the compiler
to generate assembly code in a style amenable to hacking
for even more speed than (a) supplied.
What are the conditions to be adhered to when register
variables are passed between functions?

What do you mean by "variables passed between functions"?

[1] One whose performance is critical to the performance of
the application.
 
E

Eric Sosman

Chris said:
Please clear my doubt. When do we declare a variable to be a register
variable?

Almost never.

It's an optimisation-for-speed hint that's unnecessary for
many modern compilers. [...]

Worse, it can turn out to be a pessimization -- even for
older compilers that (as a rule) paid more heed to `register'
than do their modern successors.

I recall encountering two kinds of situations where over-
enthusiastic use of `register' actually slowed things down
(there are probably others, but these are the two types I
found myself debugging):

1) An "obedient" compiler would dedicate registers to the
tagged variables, thus making those registers unavailable for
other purposes. This could make the generated code slower than
it would have been otherwise; for example, intermediate results
of sub-expressions might be forced into memory due to shortage
of available registers to hold them.

2) In order to make enough registers available for `register'
variables, the function prologue and epilogue might need more
register-save and register-restore operations than would have
been the case otherwise. Entering and leaving the function
would then take longer, and this could in some cases outweigh
the gain of, er, registering all those variables.

The long and short was that `register' is/was a portable
expression of a non-portable intent, with implementation-specific
effects. It could be valuable if used with care in code intended
for a specific compiler on a specific machine, but was problematic
in code intended for wider portability.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,074
Latest member
StanleyFra

Latest Threads

Top