0

From what I have read, I know that the register keyword is almost always optimised by the modern compilers. Some sources claim that it shouldn't be used anymore, as the compiler will optimise the code better than the programmer in most cases.

Therefore I have these three questions:

  1. Is tat keyword still being used in some applications?
  2. How does GCC handle register keyword?
  3. If it has effect on the performance of my code, how can I investigate the use of register? (Can I see where it the register variable stored? Can I force the compiler the interpret the keyword the way I want declared the variable, without any optimisations?)
syntagma
  • 23,346
  • 16
  • 78
  • 134
  • 1
    I think the dupe-linking by @paulr is correct. To answer your q's 1. Maybe. 2. Just like all other compilars. 3. you cannot check the address of a register. – Sourav Ghosh Nov 24 '14 at 10:37
  • "Maybe" is not the answer for question 1 I was looking for. – syntagma Nov 24 '14 at 10:45
  • 1
    register will not, generally speaking, do anything to the generated code. – perh Nov 24 '14 at 11:11
  • 2
    I recommend exploring things like this on gcc.godbolt.org, as an example http://goo.gl/Zq5o0g – perh Nov 24 '14 at 11:20
  • 1
    As well as numerous duplicates, the question has other problems, e.g. it breaks the "one question per question" rule, and there doesn't seem to be a specific problem here that you are trying to solve, just the beginning of an open-ended discussion on the history, rationale and internal workings of the register keyword. Perhaps you could narrow the question down to a specific problem and try again? – Paul R Nov 24 '14 at 11:37
  • @PaulR Can you please delete the question then? I will ask more specific question(s) on the same topic somewhere in the future. – syntagma Nov 24 '14 at 12:16
  • @REACHUS: I'm not a moderator, so I can't delete the question, and it has a useful answer now, so it wouldn't be a good idea to delete it anyway. It should probably just be put "on hold" for posterity, and if you have any remaining specific question(s) then post it/them as new question(s). – Paul R Nov 24 '14 at 12:19

1 Answers1

2

Today, with optimizing compilers (like GCC with -O1 at least), the register keyword is indeed deprecated. Its only meaning is to forbid taking the address of such a declared variable.

In other words,

  register int r;
  printf("r@%p\n", &r); // WRONG: address of register variable

should not compile.

GCC also provides as an extension (also understood by Clang/LLVM) variables in specified register.

You might look inside the assembler code (file *.s), e.g. produced with gcc -Wall -O2 -S -fverbose-asm...

BTW, on current machines, the CPU cache and the locality of data matters a lot regarding to performance, probably much more than register usage (of course, assuming optimizations are enabled in your compiler). Read also about register renaming, and this answer (related to prefetching).

Community
  • 1
  • 1
Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547