Experiences using "register"

I

Ioannis Vranos

polas said:
Is it possible at all that, variables declared to be register located
might confuse the compiler's optimisation methods and actually reduce
the efficiency of the overall code?


"register" is part of the language standard, if a compiler considers it
unneeded, it is free to ignore it. Proper uses of "register" making the
compiler to produce less-efficient code than not using it at all, is a
compiler-defect.
 
I

Ioannis Vranos

Spiros said:
How do you choose which variables to declare register ?
Is it on a general feel on which gets used the most or
do you keep precise statistics or a different method ?


About the same way of using inline statements for functions. When you
know a variable is going to be used as a counter in a for-loop for many
iterations, then it is a good candidate for "register". However for user
application code under a PC using Windows/Linux, "register" will
probably be ignored (under Linux GCC I am not sure the circumstances
under which it gets ignored and under which it isn't).
 
W

Willem

Ioannis wrote:
) "register" is part of the language standard, if a compiler considers it
) unneeded, it is free to ignore it. Proper uses of "register" making the
) compiler to produce less-efficient code than not using it at all, is a
) compiler-defect.

I disagree.

It's possible that there are multiple code-paths through a function, and
that declaring a certain variable makes one of those code-paths more
efficient at the expense of the others. In this case, the compiler in
essence takes the register declaration as a hint that that specific code
path is more important to be optimized.

So, if a programmer then mistakenly declares such a variable register when
in fact the other code paths are more in need of optimization, the code
will be less-efficient, but I would not call that a compiler-defect.


SaSW, Willem
--
Disclaimer: I am in no way responsible for any of the statements
made in the above text. For all I know I might be
drugged or something..
No I'm not paranoid. You all think I'm paranoid, don't you !
#EOT
 
I

Ioannis Vranos

Willem said:
Ioannis wrote:
) "register" is part of the language standard, if a compiler considers it
) unneeded, it is free to ignore it. Proper uses of "register" making the
) compiler to produce less-efficient code than not using it at all, is a
) compiler-defect.

I disagree.

It's possible that there are multiple code-paths through a function, and
that declaring a certain variable makes one of those code-paths more
efficient at the expense of the others. In this case, the compiler in
essence takes the register declaration as a hint that that specific code
path is more important to be optimized.

So, if a programmer then mistakenly declares such a variable register when
in fact the other code paths are more in need of optimization, the code
will be less-efficient, but I would not call that a compiler-defect.


"register" has only one purpose. As a hint to compiler to place a
specific variable in a register.

A compiler shouldn't make more assumptions about the rest of the code
using the register variable, than it should make if the register keyword
was not used.
 
R

Richard

Ioannis Vranos said:
"register" has only one purpose. As a hint to compiler to place a
specific variable in a register.

A compiler shouldn't make more assumptions about the rest of the code
using the register variable, than it should make if the register keyword
was not used.

Incorrect. Clearly the compiler will change the code generated if the
value is in a register. This will alter other code which touches on the
relevant section because (e.g.) there might be one less register to play
with for a start.
 
W

Walter Roberson

"register" has only one purpose. As a hint to compiler to place a
specific variable in a register.
A compiler shouldn't make more assumptions about the rest of the code
using the register variable, than it should make if the register keyword
was not used.

The placement of the variable into a register would often reduce the
number of registers available, lowering the compiler's freedom to place
other variables into the same register. If (perhaps only
for that particular combination of data) placing the variable
in a register (as requested by the program author) was not the optimal
thing to do, then the program can end up running more slowly.

Most machines have a relatively limited number of registers; using
a register for one purpose may preclude it being used for another.
 
W

Willem

Ioannis wrote:
) "register" has only one purpose. As a hint to compiler to place a
) specific variable in a register.

Perhaps at the expense of other variables.

) A compiler shouldn't make more assumptions about the rest of the code
) using the register variable, than it should make if the register keyword
) was not used.

Putting one variable in a register could mean moving another variable
to a less-efficient position, thus affecting other code.


SaSW, Willem
--
Disclaimer: I am in no way responsible for any of the statements
made in the above text. For all I know I might be
drugged or something..
No I'm not paranoid. You all think I'm paranoid, don't you !
#EOT
 
I

Ioannis Vranos

Richard said:
Incorrect. Clearly the compiler will change the code generated if the
value is in a register. This will alter other code which touches on the
relevant section because (e.g.) there might be one less register to play
with for a start.


Yes, if "register" is taken under consideration by the compiler,
obviously there will be one register less for the rest of the program,
and yes it can affect the run-time execution of another part of the
program (as long as the register variable remains in scope *and* at the
same time multiple-variables needing to be stored in registers are used
concurrently) , but this falls in the "proper uses of register" I wrote
above.
 
R

Richard

Ioannis Vranos said:
Yes, if "register" is taken under consideration by the compiler,
obviously there will be one register less for the rest of the program,
and yes it can affect the run-time execution of another part of the
program (as long as the register variable remains in scope *and* at the
same time multiple-variables needing to be stored in registers are used
concurrently) , but this falls in the "proper uses of register" I wrote
above.

No. You are wrong. Even if other variables do not need to be stored the
code can vary and efficiency suffer.
 
S

santosh

Ioannis said:
About the same way of using inline statements for functions. When you
know a variable is going to be used as a counter in a for-loop for
many iterations, then it is a good candidate for "register". However
for user application code under a PC using Windows/Linux, "register"
will probably be ignored (under Linux GCC I am not sure the
circumstances under which it gets ignored and under which it isn't).

Generally these days, register allocators are good enough that what you
flag with the register qualifier is probably already placed in a
register.
 
I

Ioannis Vranos

Walter said:
The placement of the variable into a register would often reduce the
number of registers available, lowering the compiler's freedom to place
other variables into the same register. If (perhaps only
for that particular combination of data) placing the variable
in a register (as requested by the program author) was not the optimal
thing to do, then the program can end up running more slowly.

Most machines have a relatively limited number of registers; using
a register for one purpose may preclude it being used for another.


.... as long as the "register" variable remains in scope.
 
R

Richard

Ioannis Vranos said:

The key word here is "need" - I took this to mean you meant other
register declarations.

But regardless its common sense. If you stick ONE value in a register
that is one less register to play with for other compiler generated
optimizations.
 
I

Ioannis Vranos

Ioannis said:
... as long as the "register" variable remains in scope.


An example of proper use:


for(register int i= 0; i< ULONG_MAX; ++i)
array*= i;


Here the scope of register variable i is in the for-loop. It can affect
other portions of the program usually only in multithreading applications.
 
I

Ioannis Vranos

Correction:




An example of proper use:


for(register int i= 0; i< INT_MAX; ++i)
array*= i;


Here the scope of register variable i is in the for-loop. It can affect
other portions of the program usually only in multithreading applications.
 
W

Willem

Ioannis wrote:
) Yes, if "register" is taken under consideration by the compiler,
) obviously there will be one register less for the rest of the program,
) and yes it can affect the run-time execution of another part of the
) program (as long as the register variable remains in scope *and* at the
) same time multiple-variables needing to be stored in registers are used
) concurrently) , but this falls in the "proper uses of register" I wrote
) above.

Ah, so now you're claiming that if a use of 'register' makes the code
run less efficiently, then it's not "proper use" ?

Where I come from we call that a circular argument.


SaSW, Willem
--
Disclaimer: I am in no way responsible for any of the statements
made in the above text. For all I know I might be
drugged or something..
No I'm not paranoid. You all think I'm paranoid, don't you !
#EOT
 
I

Ioannis Vranos

Willem said:
Ioannis wrote:
) Yes, if "register" is taken under consideration by the compiler,
) obviously there will be one register less for the rest of the program,
) and yes it can affect the run-time execution of another part of the
) program (as long as the register variable remains in scope *and* at the
) same time multiple-variables needing to be stored in registers are used
) concurrently) , but this falls in the "proper uses of register" I wrote
) above.

Ah, so now you're claiming that if a use of 'register' makes the code
run less efficiently, then it's not "proper use" ?

Where I come from we call that a circular argument.


In general, "register" should be used in small scopes. Consider an
example I posted in another message in the thread:


"An example of proper use:


for(register int i= 0; i< INT_MAX; ++i)
array*= i;


Here the scope of register variable i is in the for-loop. It can affect
other portions of the program usually only in multithreading applications".
 
W

Willem

Ioannis wrote:
) Willem wrote:
)> Ah, so now you're claiming that if a use of 'register' makes the code
)> run less efficiently, then it's not "proper use" ?
)>
)> Where I come from we call that a circular argument.
)
) In general, "register" should be used in small scopes. Consider an
) example I posted in another message in the thread:

And what about the not 'in general' cases where there are valid reasons
for variables to be declared register in large scopes ?

Those are the interesting cases that are counterexamples to your claim
that a compiler is defective if it generates less-efficient code when
'register' is used. Therefore those are the cases under discussion.

Or are you claiming that *all* those cases are 'improper use' ?


SaSW, Willem
--
Disclaimer: I am in no way responsible for any of the statements
made in the above text. For all I know I might be
drugged or something..
No I'm not paranoid. You all think I'm paranoid, don't you !
#EOT
 
I

Ioannis Vranos

Willem said:
Ioannis wrote:
) Willem wrote:
)> Ah, so now you're claiming that if a use of 'register' makes the code
)> run less efficiently, then it's not "proper use" ?
)>
)> Where I come from we call that a circular argument.
)
) In general, "register" should be used in small scopes. Consider an
) example I posted in another message in the thread:

And what about the not 'in general' cases where there are valid reasons
for variables to be declared register in large scopes ?

Those are the interesting cases that are counterexamples to your claim
that a compiler is defective if it generates less-efficient code when
'register' is used. Therefore those are the cases under discussion.

Or are you claiming that *all* those cases are 'improper use' ?


I think "register" should be used in as small scopes as possible, the
same way that "inline" should be used with as small functions as possible.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,012
Latest member
RoxanneDzm

Latest Threads

Top