Experiences using "register"

W

Willem

Ioannis wrote:
) Willem wrote:
)> 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.

And I think that cases exist where the smallest scope possible is such that
it will touch multiple code paths, making one more efficient at the cost of
others. It is the programmers discretion to make this choice.
These cases are counterexamples to your claim.


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:
)> 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.

And I think that cases exist where the smallest scope possible is such that
it will touch multiple code paths, making one more efficient at the cost of
others. It is the programmers discretion to make this choice.
These cases are counterexamples to your claim.


Can you provide some code example where this happens, since I am not a
compiler writer?
 
R

Richard

Ioannis Vranos said:
Can you provide some code example where this happens, since I am not a
compiler writer?

I think the onus of proof is on you since you are the one making claims
against the situation backed by fairly reasonable assertions and common
sense.
 
I

Ioannis Vranos

Richard said:
I think the onus of proof is on you since you are the one making claims
against the situation backed by fairly reasonable assertions and common
sense.


I am not sure about the "fairly reasonable assertions and common sense"
you are mentioning. I said "register" should be used in as small scopes
as possible and I provided a code example where a "register" variable is
used in a for-loop scope only.

A "register" variable is usually not needed where it is not accessed
intensively.


Willem mentioned some counter-examples for small scopes. I would like to
see some actual code demonstrating one or more of those.
 
K

Keith Thompson

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

That was its original intent, and it's why it's spelled that way, but
that's not how it's defined now.

The "register" keyword does exactly two things. First, as C99 6.7.1p4
says:
A declaration of an identifier for an object with storage-class
specifier register suggests that access to the object be as fast
as possible. The extent to which such suggestions are effective is
implementation-defined.
(C90 has the same requirement.)

Second, it causes any attempt to take the address of the object to be
a constraint violation.

The obvious way to accomplish both of these things is to store the
object in a CPU register, but that's not the only possibility, and the
authors of the standard were deliberately vague about how, and even
whether, to satisfy the suggestion.

Consider a stack-based CPU that provides a fast addressing mode for
object stored within N bytes of the top of the stack (because the
offset fits within the instruction word) and a slower addressing mode
for for object farther away (because the offset has to be loaded
separately). Then the "register" keyword might just cause the
compiler to attempt to store the object within the first N bytes.
This is inconsistent with the original intent (there's no reason to
forbid taking the object's address), but it's entirely consistent with
the requirement in the standard.
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.

I disagree. Most obviously, it must disallow taking the object's
address, though that's not really an assumpion. But in properly
optimized code, the registers are likely to be used very heavily.
Reserving one of the registers for a specific object is likely to
cause something else to be thrown out and stored in memory, which
could have far-ranging effects on the generated code (though for
properly written C it will have no effect on the behavior except
perhaps for performance).

Code optimization is an imperfect craft, but compilers have gotten
better at it over the years, by doing extensive compile-time analysis
of the code. The "register" keyword is a mechanism for allowing the
programmer to interfere with that analysis: "I don't care what the
compiler thinks, *this* variable is important." The programmer using
"register" has the advantage of knowing more about the expected
behavior of the code ("Given the expected input, this loop is likely
to be the bottleneck."). The optimizer has the advantage of its
authors experience, and of being able to re-do all its static analysis
every time the code is compiled. As a result, "register" can either
help or hurt, and it can have potentially far-reaching effects on the
generated code.
 
I

Ioannis Vranos

Keith said:
I disagree. Most obviously, it must disallow taking the object's
address, though that's not really an assumpion. But in properly
optimized code, the registers are likely to be used very heavily.
Reserving one of the registers for a specific object is likely to
cause something else to be thrown out and stored in memory, which
could have far-ranging effects on the generated code (though for
properly written C it will have no effect on the behavior except
perhaps for performance).

Code optimization is an imperfect craft, but compilers have gotten
better at it over the years, by doing extensive compile-time analysis
of the code. The "register" keyword is a mechanism for allowing the
programmer to interfere with that analysis: "I don't care what the
compiler thinks, *this* variable is important." The programmer using
"register" has the advantage of knowing more about the expected
behavior of the code ("Given the expected input, this loop is likely
to be the bottleneck."). The optimizer has the advantage of its
authors experience, and of being able to re-do all its static analysis
every time the code is compiled. As a result, "register" can either
help or hurt, and it can have potentially far-reaching effects on the
generated code.


As I said else-thread, "register" is to be used in as a small scope as
possible, the same way inline is to be used with as small functions as
possible.


Examples:


C99:


#include <stddef.h>


void somefunc(int *array, const size_t ARRAY_SIZE)
{
/* ... */

for(register size_t i= 0; i< ARRAY_SIZE; ++i)
array*= i;

/* ... */

}




C90:

#include <stddef.h>


void somefunc(int *array, const size_t ARRAY_SIZE)
{
/* ... */

{
register int i;

for(i= 0; i< ARRAY_SIZE; ++i)
array*= i;
}

/* ... */

}
 
I

Ioannis Vranos

Minor correction:




As I said else-thread, "register" is to be used in as a small scope as
possible, the same way inline is to be used with as small functions as
possible.


Examples:


C99:


#include <stddef.h>


void somefunc(int *array, const size_t ARRAY_SIZE)
{
/* ... */

for(register size_t i= 0; i< ARRAY_SIZE; ++i)
array*= i;

/* ... */

}




C90:

#include <stddef.h>


void somefunc(int *array, const size_t ARRAY_SIZE)
{
/* ... */

{
==> register size_t i;

for(i= 0; i< ARRAY_SIZE; ++i)
array*= i;
}

/* ... */

}
 
R

Richard Tobin

Ioannis Vranos said:
"An example of proper use:


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


Is there *any* C compiler out there that accepts this syntax but has
such a useless optimiser that using "register" is worthwhile? A loop
variable has to be the most obvious case where any reasonable compiler
will use a register anyway.

-- Richard
 
I

Ioannis Vranos

Richard said:
Ioannis Vranos said:
"An example of proper use:


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


Is there *any* C compiler out there that accepts this syntax but has
such a useless optimiser that using "register" is worthwhile? A loop
variable has to be the most obvious case where any reasonable compiler
will use a register anyway.



Essentially you are saying that "register" is not needed for application
programming with a modern C compiler, which is true.
 
C

CBFalconer

Richard said:
Ioannis Vranos said:
"An example of proper use:

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


Is there *any* C compiler out there that accepts this syntax but
has such a useless optimiser that using "register" is worthwhile?
A loop variable has to be the most obvious case where any
reasonable compiler will use a register anyway.


I seriously doubt that example. If any element of a[] is greater
than 1 it will cause overflow, and un or implementation defined
action.
 
W

Willem

Ioannis wrote:
) I am not sure about the "fairly reasonable assertions and common sense"
) you are mentioning. I said "register" should be used in as small scopes
) as possible and I provided a code example where a "register" variable is
) used in a for-loop scope only.

That doesn't prove that 'register' should be used only in small scopes.
When you said 'in as small a scope as possible' I simply meant that
sometimes that is not a small scope. Consider the example
of a parameter that is used intensively throughout the function.
In that case 'as small a scope as possible' is 'the entire function'.


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

CBFalconer said:
Richard said:
Ioannis Vranos said:
"An example of proper use:

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

Is there *any* C compiler out there that accepts this syntax but
has such a useless optimiser that using "register" is worthwhile?
A loop variable has to be the most obvious case where any
reasonable compiler will use a register anyway.


I seriously doubt that example. If any element of a[] is greater
than 1 it will cause overflow, and un or implementation defined
action.



Consider sizeof(array)/sizeof(*array)== INT_MAX and the elements of the
array being 1s and 0s.
 
R

Richard

Ioannis Vranos said:
CBFalconer said:
Richard said:
"An example of proper use:

for (register int i= 0; i < INT_MAX; ++i)
array *= i;
Is there *any* C compiler out there that accepts this syntax but
has such a useless optimiser that using "register" is worthwhile?
A loop variable has to be the most obvious case where any
reasonable compiler will use a register anyway.


I seriously doubt that example. If any element of a[] is greater
than 1 it will cause overflow, and un or implementation defined
action.



Consider sizeof(array)/sizeof(*array)== INT_MAX and the elements of the
array being 1s and 0s.


Don't worry, you're just witnessing the wondrous Chuck being obstructive
and pedantic as usual. Everyone else knew what you were driving at.
 
I

Ioannis Vranos

Willem said:
Ioannis wrote:
) I am not sure about the "fairly reasonable assertions and common sense"
) you are mentioning. I said "register" should be used in as small scopes
) as possible and I provided a code example where a "register" variable is
) used in a for-loop scope only.

That doesn't prove that 'register' should be used only in small scopes.
When you said 'in as small a scope as possible' I simply meant that
sometimes that is not a small scope. Consider the example
of a parameter that is used intensively throughout the function.
In that case 'as small a scope as possible' is 'the entire function'.


If the function is more than 5-6 lines of code, "register" should not be
used for the function scope but only for local scopes in the function
(like small loops).
 
W

Willem

Ioannis wrote:
) Willem wrote:
)> Ioannis wrote:
)> ) I am not sure about the "fairly reasonable assertions and common sense"
)> ) you are mentioning. I said "register" should be used in as small scopes
)> ) as possible and I provided a code example where a "register" variable is
)> ) used in a for-loop scope only.
)>
)> That doesn't prove that 'register' should be used only in small scopes.
)> When you said 'in as small a scope as possible' I simply meant that
)> sometimes that is not a small scope. Consider the example
)> of a parameter that is used intensively throughout the function.
)> In that case 'as small a scope as possible' is 'the entire function'.
)
) If the function is more than 5-6 lines of code, "register" should not be
) used for the function scope but only for local scopes in the function
) (like small loops).

Why not ?


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
 
R

Richard

Ioannis Vranos said:
If the function is more than 5-6 lines of code, "register" should not be
used for the function scope but only for local scopes in the function
(like small loops).

Where do you get these "facts" that you maintain are true with zero to
back them up? It all depends on the compiler and also depend, in a
larger function, on how often your register variable was used and how it
was used. Length in lines has absolutely nothing to do with it.
 
I

Ioannis Vranos

Richard said:
Where do you get these "facts" that you maintain are true with zero to
back them up? It all depends on the compiler and also depend, in a
larger function, on how often your register variable was used and how it
was used. Length in lines has absolutely nothing to do with it.


The smaller the scope, the less effect it has on the rest of code. Where
do you disagree with this?
 
W

Willem

Ioannis wrote:
) Richard wrote:
)>> If the function is more than 5-6 lines of code, "register" should not be
)>> used for the function scope but only for local scopes in the function
)>> (like small loops).
)>
)> Where do you get these "facts" that you maintain are true with zero to
)> back them up? It all depends on the compiler and also depend, in a
)> larger function, on how often your register variable was used and how it
)> was used. Length in lines has absolutely nothing to do with it.
)
) The smaller the scope, the less effect it has on the rest of code. Where
) do you disagree with this?

You're arguing in circles again.
Why should 'register' be used only when it has minimal effect on the 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

Willem said:
Ioannis wrote:
) Richard wrote:
)>> If the function is more than 5-6 lines of code, "register" should not be
)>> used for the function scope but only for local scopes in the function
)>> (like small loops).
)>
)> Where do you get these "facts" that you maintain are true with zero to
)> back them up? It all depends on the compiler and also depend, in a
)> larger function, on how often your register variable was used and how it
)> was used. Length in lines has absolutely nothing to do with it.
)
) The smaller the scope, the less effect it has on the rest of code. Where
) do you disagree with this?

You're arguing in circles again.
Why should 'register' be used only when it has minimal effect on the code ?


Why "inline" should be used with small functions only?
 

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,014
Latest member
BiancaFix3

Latest Threads

Top