0

My question, Consider CPU has 4 register and in my C code i have declared 10 register variable

How it handled by compiler ? and how it handled during process execution ?

  • 5
    The compiler is allowed to ignore any `register` keyword anyway. Nowadays the compilers are rather good in finding best allocation for your variables. – Gerhardh Apr 18 '23 at 11:10
  • What exactly is your concern? The presence of 4 registers doesn't mean you can use all 4 for variables. Some might be used for passing parameters or return values. The rules for using them are defined by the compiler. – Gerhardh Apr 18 '23 at 11:16
  • 1
    The `register` keyword is only a hint. The compiler might ignore it. The compiler might place some of your `register` variables in registers, or all of them if it can. The compiler might even place non-`register` variables in registers. – Steve Summit Apr 18 '23 at 11:49
  • 1
    @SteveSummit, it adds some useful constraints for a compiler. For example it is not allowed to take an address of `register` object – tstanisl Apr 18 '23 at 11:50
  • 2
    @shankar In modern C programming, the `register` keyword is not used. If someone is trying to teach you about it, or assigning exercises about it, they're teaching you 1970's C, or maybe 1980's, but not the C that anyone is using today. My point is that, in my opinion, you should feel free to completely ignore the `register` keyword, to not devote any time or brain cells to it. – Steve Summit Apr 18 '23 at 12:08
  • In restricted ISA with few registers, it won't allocate all variables in registers before the point where they are actually used. – Lundin Apr 19 '23 at 14:24
  • @SteveSummit One single use remains: if a local variable was not initialized and _could_ have been declared as `register` because it never has its address taken, then accessing that variable's value invokes UB. Whereas accessing local variables that have their addresses taken only invokes unspecified behavior, given that there's no trap representation for the given type. – Lundin Apr 19 '23 at 14:27

1 Answers1

2

You can use as many register variables as you want. The only guaranteed effect is that it stops you from taking the address.

This makes it possible for a compiler to keep it in a register even with optimization disabled, but doesn't require it.

With optimization enabled, the register keyword is fully useless. That's why C++ deprecated and then removed it. Ancient compilers needed the help; compilers for the past 2 decades at least have been able to figure out when they can keep variables in registers on their own. (Doing escape analysis and alias analysis on a whole function before emitting any asm for any of its statements.)

Except for TCC, the Tiny C Compiler, which compiles on the fly in one pass. Perhaps that would benefit from the register keyword, since it doesn't look ahead in the function or do any analysis. Or for simplicity it might just ignore register, other than erroring if you try to take the address.


There's a GNU C extension that also uses the register keyword, like register int foo asm("r2");. For local vars, that forces the compiler's choice of register when you use it as an operand in asm("..." : "=r"(foo)). And with GCC at least (but not clang), the compiler does tend to keep that variable in that register even outside of asm statements. But that's no longer a documented (supported) guarantee; GCC removed that part of the docs a few years ago.

For global vars, an explicit-register global var will tie up that register for the whole program. This would be a super bad idea on a machine with only 4 registers, using up 1/4 of the total on one variable, even in code that doesn't touch it.

This GNU C extension makes you specify which register to use, so obviously you are limited to the number of registers in the target you're compiling for. Unless you use the same register for multiple variables, which I was surprised to find isn't an error for local vars, unless you actually try to use them both with asm statements. (https://godbolt.org/z/K463bcf8c). It is an error for global vars.

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847