Keyword register

Discussion in 'C++' started by Ioannis Vranos, Nov 23, 2004.

  1. 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.
    Ioannis Vranos, Nov 23, 2004
    1. Advertisements

  2. * Ioannis Vranos:
    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 .
    Alf P. Steinbach, Nov 23, 2004
    1. Advertisements

  3. 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.
    Ioannis Vranos, Nov 23, 2004

  4. 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".
    Ioannis Vranos, Nov 23, 2004
  5. * Ioannis Vranos:
    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. ;-)
    Alf P. Steinbach, Nov 23, 2004
  6. 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.
    Karl Heinz Buchegger, Nov 23, 2004
  7. Ioannis Vranos

    chris Guest

    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, Nov 23, 2004
  8. Ioannis Vranos

    Jerry Coffin Guest

    [ ... ]
    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.
    That's true in C, but not in C++.
    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.
    Jerry Coffin, Nov 23, 2004
  9. Ioannis Vranos

    Jack Klein Guest

    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.
    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
    Jack Klein, Nov 23, 2004
  10. Can you please give some example where usage of keyword register
    would be appropriate.

    Greetings, Bane.
    Branimir Maksimovic, Nov 23, 2004

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

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

    Do you think the same for inline keyword?

    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.
    Ioannis Vranos, Nov 23, 2004

  12. Not even in some embedded system?
    Ioannis Vranos, Nov 23, 2004

  13. Perhaps in a for loop indexing variable in a Z80 embedded system
    dedicated application?
    Ioannis Vranos, Nov 23, 2004
  14. 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.
    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.
    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.
    Karl Heinz Buchegger, Nov 24, 2004

  15. 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.
    Ioannis Vranos, Nov 24, 2004
  16. 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.
    Branimir Maksimovic, Nov 24, 2004
  17. 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 Lindauer, Nov 24, 2004
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.