register variables

Stew Rubenstein stew at harvard.ARPA
Wed Jun 26 16:10:38 AEST 1985


> In article <472 at crystal.UUCP> shekita at crystal.UUCP writes:
> >1) When is it appropriate to declare a variable as a
> >   register? I have been told that declaring a variable 
> >   as a register can actually result in slower code. Is
> >   there a rule of thumb dictating when and when not to
> >   declare a variable as a register?
> 
> Generally, the only time declaring a variable to be a register
> generates slower code is when you declare a parameter to be a
> register.  This is because, on most systems, this requires a move from
> the stack into a register.  If the variable is rarely used, this
> overhead may cost you more than having it in the register saves.

This is not always true.  On many (most?) systems, the use of a register in a
subroutine requires that the register be saved before use and restored
before return.  So you get:

   Move register onto stack
   If a parameter or initialized auto then move initial value into register
   Do your subroutine
   Move saved value back into register

On the vax, the register is saved and restored by the CALL instruction,
but it still takes time.



More information about the Comp.lang.c mailing list