Hi ,
I'm Fabio from Italy.
Is it possible to know if a variable declared as register is really allocated in the register?
Thanx a lot!
Fabio
Hi ,
I'm Fabio from Italy.
Is it possible to know if a variable declared as register is really allocated in the register?
Thanx a lot!
Fabio
Most compilers have the option of outputting assembly code. Typically, you should not use the register keyword and just let the compiler do its thing.
Following are examples of how to output assembly using GCC and MSVC respectively (with O2 optimization):
Code:gcc.exe -S -O2 test.c cl /FAs /O2 test.c
In order to minimize the computation time, I want to force the allocation in the register of a lot of index variables in CPU-demanding cycles,Typically, you should not use the register keyword and just let the compiler do its thing.
If I write a declaration as follow:
... I hope that "i" variable will be allocated in the register, but, if the register is full, "i" will be allocated in RAM.Code:register int i
The compiler makes always the right choice , but are you sure that this choice is demanded to the compiler and not to the operative system?
In other words I think (but I'm not really sure!) that the allocation in the register is a run-time choiche of the O.S. and it depends on the state of the register.
If the register is full the "i" variable is allocated in RAM, but, if the register is free, the "i" variable is allocated in the register.
What do you think about it?
I've just tried to explain this concept in the best way, but I don't speak English very well... excuse me !
Fabio
>I want to force the allocation in the register of a lot of index variables in CPU-demanding cycles
How many is a lot? Do you have enough registers and are they all devoted to your program? Does your compiler even honor the hint?
>What do you think about it?
If a variable is specified as being register, the compiler is free to take that as a hint that it's going to be used heavily and optimize accordingly. But a lot of compilers completely ignore the register keyword because the compiler is in a much better position to know how and where to optimize your code than you are. The common guideline these days is to avoid the register keyword and let your compiler do its job. If the variable is really a hog then it'll probably be placed in a register anyway.
Most modern compilers will optimise code far better then the average programmer. Trying to force your own optimisation onto code will rarely have a positive effect, and often a negative one. Compilers allow the use of the "register" storage modifier to be compliant with standards, but most will simply ignore it.
Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.
I wrongly said "I want to force the allocation in the register of a lot of index variables in CPU-demanding cycles".How many is a lot? Do you have enough registers and are they all devoted to your program?
This is my corrected sentence:"I want to force the allocation in the register of an index variable used in a for-statement (10000000 cycles)"
I need a single index variable and I would test the time difference between register variable and RAM variables... but I think I'll follow your subsequent suggestions!
1)2)Trying to force your own optimisation onto code will rarely have a positive effect, and often a negative one.Thanks to all for your support!The common guideline these days is to avoid the register keyword and let your compiler do its job. If the variable is really a hog then it'll probably be placed in a register anyway.
Bye Fabio
Last edited by pollopinolo; 12-28-2005 at 07:33 AM. Reason: missing
No, register allocation (at least on all of the architectures that I've worked on) is done by the compiler at final code generation time. (Actually, I know of at least one system where this was done at image link time, but the linker in that case was doing major code optimization, so I consider it a deferred stage of the code generation from the compiler.)Originally Posted by pollopinolo
The register keyword is a hint to the compiler that, when deciding between candidates for binding to registers with otherwise equal weight, the programmer thinks this is a better candidate. With modern code generators this hint is of little value, since the optimizations within the compiler are far more sophisticated than they were back in the 1970s, when the C language was put together.
[edit]The original K&R C compiler was written for the PDP-11 which had 8 general purpose registers (only 6 of these could be used arbitrarily); these could be used to hold pointers, indexes, or data. The optimizer in this compiler was very primitive (if it existed at all). In those days, the register qualifier was almost always honored unless there were too many variables declared as register or the type was incompatible with being a register. Taking the address of a register variable generated an error message from some compilers, other compilers simply forgot the qualifier in these cases.[/edit]
Last edited by filker0; 12-28-2005 at 08:07 AM. Reason: Add a short historical point and fix grammer
Insert obnoxious but pithy remark here
Could you be thinking of the CPU cache (also wikipedia) rather than registers?In other words I think (but I'm not really sure!) that the allocation in the register is a run-time choiche of the O.S. and it depends on the state of the register.
A loop variable will almost certainly be placed in a register. You can confirm this by looking at the assembly output of programs."I want to force the allocation in the register of an index variable used in a for-statement (10000000 cycles)"
If your code is running too slow, putting a variable into a register is not going to fix it. To get real speed improvements, you need to start by reducing calls to expensive functions (for example, output to the console is extremely expensive) and most importantly reducing the number of loop iterations.
Using time testing is a always a good idea rather than just using what "should" work.
filker0, thanks for another informative post.