gcc: Local Register Variables

1 
1 6.45.5.2 Specifying Registers for Local Variables
1 .................................................
1 
1 You can define a local register variable and associate it with a
1 specified register like this:
1 
1      register int *foo asm ("r12");
1 
1 Here 'r12' is the name of the register that should be used.  Note that
1 this is the same syntax used for defining global register variables, but
1 for a local variable the declaration appears within a function.  The
1 'register' keyword is required, and cannot be combined with 'static'.
1 The register name must be a valid register name for the target platform.
1 
1  As with global register variables, it is recommended that you choose a
1 register that is normally saved and restored by function calls on your
1 machine, so that calls to library routines will not clobber it.
1 
1  The only supported use for this feature is to specify registers for
11 input and output operands when calling Extended 'asm' (⇒Extended
 Asm).  This may be necessary if the constraints for a particular
1 machine don't provide sufficient control to select the desired register.
1 To force an operand into a register, create a local variable and specify
1 the register name after the variable's declaration.  Then use the local
1 variable for the 'asm' operand and specify any constraint letter that
1 matches the register:
1 
1      register int *p1 asm ("r0") = ...;
1      register int *p2 asm ("r1") = ...;
1      register int *result asm ("r0");
1      asm ("sysint" : "=r" (result) : "0" (p1), "r" (p2));
1 
1  _Warning:_ In the above example, be aware that a register (for example
1 'r0') can be call-clobbered by subsequent code, including function calls
1 and library calls for arithmetic operators on other variables (for
1 example the initialization of 'p2').  In this case, use temporary
1 variables for expressions between the register assignments:
1 
1      int t1 = ...;
1      register int *p1 asm ("r0") = ...;
1      register int *p2 asm ("r1") = t1;
1      register int *result asm ("r0");
1      asm ("sysint" : "=r" (result) : "0" (p1), "r" (p2));
1 
1  Defining a register variable does not reserve the register.  Other than
1 when invoking the Extended 'asm', the contents of the specified register
1 are not guaranteed.  For this reason, the following uses are explicitly
1 _not_ supported.  If they appear to work, it is only happenstance, and
1 may stop working as intended due to (seemingly) unrelated changes in
1 surrounding code, or even minor changes in the optimization of a future
1 version of gcc:
1 
1    * Passing parameters to or from Basic 'asm'
1    * Passing parameters to or from Extended 'asm' without using input or
1      output operands.
1    * Passing parameters to or from routines written in assembler (or
1      other languages) using non-standard calling conventions.
1 
1  Some developers use Local Register Variables in an attempt to improve
1 gcc's allocation of registers, especially in large functions.  In this
1 case the register name is essentially a hint to the register allocator.
1 While in some instances this can generate better code, improvements are
1 subject to the whims of the allocator/optimizers.  Since there are no
1 guarantees that your improvements won't be lost, this usage of Local
1 Register Variables is discouraged.
1 
1  On the MIPS platform, there is related use for local register variables
11 with slightly different characteristics (⇒Defining coprocessor
 specifics for MIPS targets (gccint)MIPS Coprocessors.).
1