Question

I learned about register

sample:

register int a;

Says the compiler to use a variable faster (save my int variable in register) but if non have registers in processor - for my variable set auto

iPhone ARM processors can have this special registers?

Était-ce utile?

La solution

Any processor has registers (except very rare and now dead stack-based architectures). The keyword register in C is just a hint for the compiler to map that variable in a register and not on the stack. This comes from "ancient" times when the algorithms to map variables to registers weren't that efficient. Nowadays these algorithms map better than a human could, so there is really no need to use the register hint.

I really don't know about iPhone ARM compilers, but I really don't think they don't use modern algorithms to map variables to registers.

Also note that what I have told you is valid for compiling with optimization flags.


Register are the fastest memory a processor has, it resides inside the CPU, they run at the speed of the processor and have no time penalty to access it. However, registers are limited and very few, from 4-6 general purpose registers (on CISC architectures) to 10-20 registers (on RISC architectures). A compiler will try to map the most used accessed variables to these registers. When he runs out of registers he has to map the variables on the stack, which is much slower (it is in RAM, but mostly it is cached in cache L1 or L2).
So the decision as which variables are mapped to registers and which variables are mapped on the stack has significant impact on performance, but, as I have said, nowadays the compilers are doing a better job than a human could at mapping variables to registers.

Autres conseils

Bolov provides a fairly good answer. However, there are couple of additional points on register.

No pointers

First, you may not take the address of a register variable. With most compilers, the auto variables are tracked and annotated as being referenced by a pointer and it won't usually make a difference. However, there may be cases where a compiler can not determine that a pointer is not aliased to a variable and in this case a register variable may actually be better. As the variable is declared register, there is no way it can be aliased via a pointer. See info on the restrict keyword for more on aliasing.

Ironically, this will probably come into play with lower optimizations levels.

Inline assembler

If you explicitly declare a register to use with clang or gcc syntax you need something like,

 register unsigned int *stack asm("sp");

register is needed in order to have a variable stack which points to the top of the stack. This is an extension to the 'C' language, but it is relevant for iOS and its common compilers.

Tie breaker

In an exceedingly complex algorithm, using register can give a hint to the compile to keep the value in memory (in favor of other variables). Usually, like inline, it is better to just let the compiler decide. Unless you have done extensive profiling and found that the compiler was saving a commonly used variable on the stack.

If you don't over use the keyword, it can also serve as documentation that the variable is performance critical; although a comment might be just as good in this case.

For the register keyword to make a difference on the ARM (r0-r15) you need a very complex algorithm and large function bodies. It maybe more useful in thumb mode where less registers are available; For iOS ARM devices this does not apply.

Usually register is never needed. It is also conceivable that over-use could actually result in worse performing code. It shouldn't be an automatic reflex to add this keyword and should only be done after code is completely functional and an optimization phase has begun.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top