Keyword register

I

Ioannis Vranos

Just some thought on it, wanting to see any explanations.


It was advised in this newsgroups that we should avoid the use of
keyword register.

However it is a language feature, and if it offers no help to an
implementation, it is free to ignore it. And this happens widely, all my
C++ compilers in my platform ignore this keyword.

Also the use of register provides the additional guarantee that we do
not get the address of this variable which may indeed help an
implementation to optimise the variable access further, which was the
original purpose of its introduction after all.

So why we should not use register?

An implementation taking it into account means that the keyword helps it
indeed, otherwise the implementation *should* ignore it.
 
A

Alf P. Steinbach

* Ioannis Vranos:
Also the use of register provides the additional guarantee that we do
not get the address of this variable which may indeed help an
implementation to optimise the variable access further, which was the
original purpose of its introduction after all.

Well, the standard says 'register' is typically ignored if the address
of the object is taken.

That indicates that you can take the address of a 'register' variable.

As far as I can see it does not say anywhere that you can't .
 
I

Ioannis Vranos

Alf said:
Well, the standard says 'register' is typically ignored if the address
of the object is taken.

That indicates that you can take the address of a 'register' variable.

As far as I can see it does not say anywhere that you can't .



Yes, the standard says:

"A register specifier has the same semantics as an auto specifier
together with a hint to the implementation that the object so declared
will be heavily used. [Note: the hint can be ignored and in most
implementations it will be ignored if the address of the object is
taken. —end note]".


In any case, my original question why register should be avoided remains.
 
I

Ioannis Vranos

Ioannis said:
Yes, the standard says:

"A register specifier has the same semantics as an auto specifier
together with a hint to the implementation that the object so declared
will be heavily used. [Note: the hint can be ignored and in most
implementations it will be ignored if the address of the object is
taken. —end note]".


For completeness I will mention here what TC++PL 3 says about register:


"Declaring a variable register is a hint to the compiler to optimize for
frequent access; doing so is redundant with most modern compilers".


Redundant doesn't mean "don't use it".
 
A

Alf P. Steinbach

* Ioannis Vranos:
In any case, my original question why register should be avoided remains.

Who says it should be avoided?

But as a practical matter, nothing is gained by using it, and
it's more to type and more to parse (for the human reader).

So in practice we don't use it, except when doing a copy-paste
of Duff's device. ;-)
 
K

Karl Heinz Buchegger

Ioannis said:
Just some thought on it, wanting to see any explanations.

It was advised in this newsgroups that we should avoid the use of
keyword register.

However it is a language feature, and if it offers no help to an
implementation, it is free to ignore it. And this happens widely, all my
C++ compilers in my platform ignore this keyword.

Also the use of register provides the additional guarantee that we do
not get the address of this variable which may indeed help an
implementation to optimise the variable access further, which was the
original purpose of its introduction after all.

So why we should not use register?

An implementation taking it into account means that the keyword helps it
indeed, otherwise the implementation *should* ignore it.

As I see it:

The keyword 'register' originated in the dark ages of C, when compiler
and their optimizer weren't that sophisticated as they are today.
In the good old days, 'register' was a hint to the compiler to concentrate
on that variable to keep it as long as possible in a register.
If you use register today, then you are lucky if the compiler simply
ignores it. If you are unlucky, then your guess about variable usage
was wrong and the compiler tries to 'optimize' according to your hint,
instead of relying on its own data flow analysis. And guess: as with
most low level optimizations, the programmer is usually wrong. Let
the compilers data flow analysis figure out which variable to put into
a register. It can do that task better then most humans. Not to mention
that the data flow analysis might figure out, that optimal register usage in
the first half of the function differs significantly from optimal register usage
in the second half of the function. And that's something a simple 'register'
cannot do.
 
C

chris

Ioannis said:
Just some thought on it, wanting to see any explanations.


It was advised in this newsgroups that we should avoid the use of
keyword register.

However it is a language feature, and if it offers no help to an
implementation, it is free to ignore it. And this happens widely, all my
C++ compilers in my platform ignore this keyword.

Also the use of register provides the additional guarantee that we do
not get the address of this variable which may indeed help an
implementation to optimise the variable access further, which was the
original purpose of its introduction after all.

So why we should not use register?
The very short reason is that nowadays register allocation systems are
very clever, and almost certainly the blunt instrument which is
"register" is highly unlikely to help guide the optimiser to better code
than it could construct simply by itself.

Chris
 
J

Jerry Coffin

[ ... ]
However it is a language feature, and if it offers no help to an
implementation, it is free to ignore it. And this happens widely, all my
C++ compilers in my platform ignore this keyword.

Unfortunately, a number of implementations (e.g. at least some of
Borland's compilers) don't ignore it when they should. The result is
that using it can pessimize the code.
Also the use of register provides the additional guarantee that we do
not get the address of this variable which may indeed help an
implementation to optimise the variable access further, which was the
original purpose of its introduction after all.

That's true in C, but not in C++.
So why we should not use register?

An implementation taking it into account means that the keyword helps it
indeed, otherwise the implementation *should* ignore it.

It inevitably does at least slight harm to readability and may do
slight harm to efficiency, while standing no chance whatsoever of
doing any good at all.
 
J

Jack Klein

Just some thought on it, wanting to see any explanations.


It was advised in this newsgroups that we should avoid the use of
keyword register.

However it is a language feature, and if it offers no help to an
implementation, it is free to ignore it. And this happens widely, all my
C++ compilers in my platform ignore this keyword.

This is true in C++, but is one of the differenced between C++ and C.
It is not legal in C to take the address of an object defined with the
register storage class, whether the compiler ignores the hint or not.
Also the use of register provides the additional guarantee that we do
not get the address of this variable which may indeed help an
implementation to optimise the variable access further, which was the
original purpose of its introduction after all.

So why we should not use register?

An implementation taking it into account means that the keyword helps it
indeed, otherwise the implementation *should* ignore it.

You have already gotten a lot of good replies, and in general I agree
with them. In general, a programmer, particularly an applications
level programmer, should not use the register keyword.

It is in fact much like any other source code optimization. It should
not be used at all initially, unless you doing things like writing
hardware-level drivers or interrupt service routines, and probably not
even then.

Once you have a program that operates correctly but more slowly than
desired, you can apply tools like profilers to find out where the
bottlenecks are. Then, and only then, apply source code optimizations
like selective use of the register keyword to see if it improves
performance.
 
B

Branimir Maksimovic

Ioannis Vranos said:
So why we should not use register?

Can you please give some example where usage of keyword register
would be appropriate.

Greetings, Bane.
 
I

Ioannis Vranos

Karl said:
As I see it:

The keyword 'register' originated in the dark ages of C, when compiler
and their optimizer weren't that sophisticated as they are today.
In the good old days, 'register' was a hint to the compiler to concentrate
on that variable to keep it as long as possible in a register.
If you use register today, then you are lucky if the compiler simply
ignores it.


If there is no benefit of its use in a platform, then an implementation
should ignore it. After all, it is a language feature.

If you are unlucky, then your guess about variable usage
was wrong


Why? For example why an indexing variable in a loop would be a wrong use?

and the compiler tries to 'optimize' according to your hint,
instead of relying on its own data flow analysis. And guess: as with
most low level optimizations, the programmer is usually wrong.


Do you think the same for inline keyword?


Let
the compilers data flow analysis figure out which variable to put into
a register. It can do that task better then most humans. Not to mention
that the data flow analysis might figure out, that optimal register usage in
the first half of the function differs significantly from optimal register usage
in the second half of the function. And that's something a simple 'register'
cannot do.


Yes, in this case the implementation should ignore the keyword register
(as all compilers of mine do), otherwise a compiler lacking such a "data
flow analysis" would benefit from the keyword.
 
I

Ioannis Vranos

Branimir said:
Can you please give some example where usage of keyword register
would be appropriate.


Perhaps in a for loop indexing variable in a Z80 embedded system
dedicated application?
 
K

Karl Heinz Buchegger

Ioannis said:
Why? For example why an indexing variable in a loop would be a wrong use?

You can be very sure, that the current compilers can figure this
one out. Even if they don't contain a full fletched data flow analysis.
Do you think the same for inline keyword?

Under the same circumstances: yes.

But the situation is different. 'inline' not only makes for optimizations
but is also a way to have an implementation in a header file and not
violating the one definition rule. So 'inline' serves a second purpose
here and the keyword cannot be dropped because of that.

You would be astound of what gets inlined under the hood without you
ever knowing it. Eg. calls to strlen(), strcpy() etc. get inlined with
a lot of compilers. And there the implementation sits in a library.
Yes, in this case the implementation should ignore the keyword register
(as all compilers of mine do), otherwise a compiler lacking such a "data
flow analysis" would benefit from the keyword.

All professional compilers contain such a thing. Without it, a company can
not sell a compiler any more. It is like constant folding. You can rely
on your compiler to do it.
 
I

Ioannis Vranos

Karl said:
All professional compilers contain such a thing. Without it, a company can
not sell a compiler any more. It is like constant folding. You can rely
on your compiler to do it.


I presume you have high-end systems in mind (including Home PCs)
together with commercial compilers.

However what about some plain (perhaps free) ISO C++ compiler either for
such a "high end" system, or for some Z80 embedded system that only
places a variable in a register only when you tell it to do so.

In particular, having some very small background on the Z80 case (used
some Z80 emulator that permitted to place commands directly in memory,
some time ago), this one makes sense there. (We are talking about a
system board having a Z80, 8/16 KB of memory, with only one program in
this memory running as a dedicated application).


Or some other system which permits you to enforce some particular
variable to a register (that is it accepts the keyword).


In any case, doing

for(register int i=0; i<10; ++i)
// ...


shouldn't cause any harm, since a system not needing to take this under
consideration, should ignore it (and again all my C++ compilers *do
ignore* it.

That is in my compilers, the use of register above has no effect.
 
B

Branimir Maksimovic

Ioannis Vranos said:
Perhaps in a for loop indexing variable in a Z80 embedded system
dedicated application?

You don't have to tell that explicitelly. z80 can indirectly
address memory only through pair of hl, or ix and iy index registers.
ix and iy can be offseted in -128, +127 range.
If compiler is stupid enough to load index value at some memory location
in hl every loop cycle, then register keyword will probaly help.:)

Greetings, Bane.
 
D

David Lindauer

Ioannis said:
Just some thought on it, wanting to see any explanations.

It was advised in this newsgroups that we should avoid the use of
keyword register.

However it is a language feature, and if it offers no help to an
implementation, it is free to ignore it. And this happens widely, all my
C++ compilers in my platform ignore this keyword.

Also the use of register provides the additional guarantee that we do
not get the address of this variable which may indeed help an
implementation to optimise the variable access further, which was the
original purpose of its introduction after all.

So why we should not use register?

An implementation taking it into account means that the keyword helps it
indeed, otherwise the implementation *should* ignore it.

the main thing is, it was originally added to the "C" language well before
the advent of good optimizing compilers. In those days being able to
explicitly tell the compiler what to put in registers could really help the
program out. In these days those kinds of decisions are better left to the
optimizer, and most optimizing compilers will work on this basis and ignore
the keyword.

David
 

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

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top