Use of "register" keyword in modern c compilers

Go To Last Post
3 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Dear all,

A long time ago, it was meant to be good practice to declare a frequently-used variable such as loop counter with the keyword "register". For example:

register unsigned char i;

But of course, it was only ever a hint to the compiler to bind the variable to a register and it could refuse to follow your advice.

I haven't used it for years. But is there any place for it when using a modern compiler like gcc?

Damien.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

On register-rich CPUs (AVR, ARM) the register keyword doesn't do much good, IMO. A good compiler is not only caching locals in registers, and non-volatile globals, as best as it can, but the compiler is transparently dropping code, unrolling loops, and so on. It's pretty hard to beat a good compiler today in ASM, except for a couple of dozen of optimized-for-speed lines of hand written code. This is because juggling optimal in-RAM versus in-register usage for a large program is beyond what a mortal can do.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I agree with Steve but it maybe doesn't hurt to put the hint on the variables you know are the ones that could really benefit from register optimisation. If the optimiser is doing its job well it will spot which are the heavily used ones and register optimise those but you never know and it may help to give it a hint.

BTW GCC does support:

register type varname asm("rN");

where N=0..31 to try and persuade a variable to locate in a specific register but, as the libc has been built without this knowledge, if you start doing this you may find the register is clobbered anyway.

Cliff