Register parameters

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Sensei

    Register parameters

    Hi again!

    I have recently encountered an old code in K&R style which declared
    function parameters as register. That puzzles me, and I know this is
    implementation defined, and depends on architecture, compiler and so
    on. But my question is, how can be a parameter made "faster" (6.7.1,
    with note 103).

    On Intel and similar implementations all parameters are, as far as I
    know, pushed into the stack. On PPC they can be passed through
    registers.

    Can someone clarify the use of this keyword and how it may work for
    function parameters in a real-world example with "faster access" than a
    normal parameter?

    I know this is, again, implementation defined, but I am not interested
    in a particular compiler or architecture, I am curious about *some*
    examples of them, if any.

    Thanks!


    --

    Sensei <Sensei's e-mail is at Mac-dot-com>

    Three things are certain
    Death, taxes, and lost data
    Guess which has occurred. (Computer's Haiku)


  • Ian Collins

    #2
    Re: Register parameters

    Sensei wrote:
    Hi again!
    >
    I have recently encountered an old code in K&R style which declared
    function parameters as register. That puzzles me, and I know this is
    implementation defined, and depends on architecture, compiler and so on.
    But my question is, how can be a parameter made "faster" (6.7.1, with
    note 103).
    >
    On Intel and similar implementations all parameters are, as far as I
    know, pushed into the stack. On PPC they can be passed through registers.
    >
    Can someone clarify the use of this keyword and how it may work for
    function parameters in a real-world example with "faster access" than a
    normal parameter?
    >
    I know this is, again, implementation defined, but I am not interested
    in a particular compiler or architecture, I am curious about *some*
    examples of them, if any.
    >
    I effect, a function parameter is just another local variable in the
    function.

    Older compiler were not as good as modern ones at deducing which
    variables should be assigned to registers, hence you will see "register"
    more often in old code.

    --
    Ian Collins.

    Comment

    • santosh

      #3
      Re: Register parameters

      Sensei <Sensei's e-mail is at Mac-dot-comwrote:
      Hi again!
      >
      I have recently encountered an old code in K&R style which declared
      function parameters as register. That puzzles me, and I know this is
      implementation defined, and depends on architecture, compiler and so
      on. But my question is, how can be a parameter made "faster" (6.7.1,
      with note 103).
      >
      On Intel and similar implementations all parameters are, as far as I
      know, pushed into the stack.
      Only when optimisations are disabled.
      On PPC they can be passed through registers.
      They can be passed through registers on any machines that has sufficient
      amounts of them.
      Can someone clarify the use of this keyword and how it may work for
      function parameters in a real-world example with "faster access" than
      a normal parameter?
      Qualifying a parameter with register indicates to the compiler that such
      an object will not be aliased. This can enable it perform some
      optimisations that it might otherwise not be able to do. Remember that
      the restrict keyword was not available back then.

      <snip>

      Comment

      • Richard Tobin

        #4
        Re: Register parameters

        In article <g5n1f6$1dk$1@r egistered.motza rella.org>,
        santosh <santosh.k83@gm ail.comwrote:
        >On Intel and similar implementations all parameters are, as far as I
        >know, pushed into the stack.
        >Only when optimisations are disabled.
        Is it practical to change the calling convention for different
        optimisation levels? This would make modules compiled with different
        optimisation incompatible.

        -- Richard

        --
        Please remember to mention me / in tapes you leave behind.

        Comment

        • Eric Sosman

          #5
          Re: Register parameters

          santosh wrote:
          [...]
          Qualifying a parameter with register indicates to the compiler that such
          an object will not be aliased.
          Have you mistaken the storage-class specifier `register'
          for the type-qualifier `restrict'? A function with two pointer
          arguments of compatible type and `register' storage class can
          be called with two arguments pointing to the same thing, and
          the code must operate correctly anyhow.

          int my_strcmp(regis ter char *p, register char *q) {
          ...
          }
          ...
          if (my_strcmp(argv[i], argv[j]) /* aliased if i==j */
          ...
          This can enable it perform some
          optimisations that it might otherwise not be able to do. Remember that
          the restrict keyword was not available back then.
          I still don't see how `register' and `restrict' are related.

          --
          Eric Sosman
          esosman@ieee-dot-org.invalid

          Comment

          • Keith Thompson

            #6
            Re: Register parameters

            Eric Sosman <esosman@ieee-dot-org.invalidwrit es:
            santosh wrote:
            >[...]
            >Qualifying a parameter with register indicates to the compiler that such
            >an object will not be aliased.
            >
            Have you mistaken the storage-class specifier `register'
            for the type-qualifier `restrict'?
            [snip]
            I still don't see how `register' and `restrict' are related.
            A register-qualified object can't have its address taken. This
            eliminates the possibility of aliasing.

            For example:

            register int reg = 42;
            int *ptr = <whatever>;
            /* more code */
            *ptr = 43;
            /* still more code */

            We know that the assignment to *ptr didn't affect the value of reg.

            --
            Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
            Nokia
            "We must do something. This is something. Therefore, we must do this."
            -- Antony Jay and Jonathan Lynn, "Yes Minister"

            Comment

            • Keith Thompson

              #7
              Re: Register parameters

              Sensei <Sensei's e-mail is at Mac-dot-comwrites:
              I have recently encountered an old code in K&R style which declared
              function parameters as register. That puzzles me, and I know this is
              implementation defined, and depends on architecture, compiler and so
              on. But my question is, how can be a parameter made "faster" (6.7.1,
              with note 103).
              [...]

              Typically the "register" keyword doesn't actually do much good; modern
              optimizing compiler are better at determining which variables should
              be stored in registers than you or I, and can re-determine this on
              every compilation without having to update the source code. (At least
              that's the common wisdom.)

              But consider a system where arguments are passed on "the stack". In a
              naive implementation, they stay there, and the function accesses each
              of its parameters from its stack location, which is in memory. If one
              particular parameter is used a lot inside the function, it might be
              profitable to *copy* its initial value from the stack into a register,
              and just use the register in the body of the function. Specifying the
              "register" keyword on a parameter would let you suggest this
              optimization.

              Of course, this kind of thing was more relevant back when we wrote in
              K&R style, optimizing compilers were dumb or nonexistent, and
              dinosaurs roamed the Earth.

              --
              Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
              Nokia
              "We must do something. This is something. Therefore, we must do this."
              -- Antony Jay and Jonathan Lynn, "Yes Minister"

              Comment

              • Kenneth Brody

                #8
                Re: Register parameters

                Sensei wrote:
                >
                Hi again!
                >
                I have recently encountered an old code in K&R style which declared
                function parameters as register. That puzzles me, and I know this is
                implementation defined, and depends on architecture, compiler and so
                on. But my question is, how can be a parameter made "faster" (6.7.1,
                with note 103).
                [...]

                On platforms I used "way back when", using register on a parameter
                told the compiler to generate code to take the parameter off the
                stack and load it into a register, and then use it just as it would
                use any register-qualified variable.

                It would basically be as if you had something like:

                int foo(bar)
                register int bar;
                {
                ...
                }

                versus

                int foo(_bar)
                int _bar;
                {
                register int bar = _bar;
                ...
                }


                --
                +-------------------------+--------------------+-----------------------+
                | Kenneth J. Brody | www.hvcomputer.com | #include |
                | kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer .h|
                +-------------------------+--------------------+-----------------------+
                Don't e-mail me at: <mailto:ThisIsA SpamTrap@gmail. com>

                Comment

                • Kaz Kylheku

                  #9
                  Re: Register parameters

                  On 2008-07-17, Sensei <Sensei'swrot e:
                  Hi again!
                  >
                  I have recently encountered an old code in K&R style which declared
                  function parameters as register. That puzzles me, and I know this is
                  implementation defined, and depends on architecture, compiler and so
                  on.
                  It's not implementation-defined. The register specifier means that
                  if the address of the declared object is taken, it is a constraint violation
                  (and thus the situation requires a diagnostic).

                  {
                  register int x;
                  int *p = &x; /* diagnosable error */
                  }

                  If you think that in some situation it is useful to be able to
                  diagnose that the address of some object is being taken, then use
                  register.
                  But my question is, how can be a parameter made "faster" (6.7.1,
                  with note 103).
                  >
                  On Intel and similar implementations all parameters are, as far as I
                  know, pushed into the stack. On PPC they can be passed through
                  registers.
                  The register keyword cannot affect calling conventions. How we know that is
                  that two function type signatures which only differ in the use of the register
                  keyword are equivalent. For instance, these two function declarations
                  specify the same type:

                  int foo(register int x);

                  int foo(int x);

                  It's possible that the first declaration is visible to callers, but the second
                  declaration is what is visible at the definition.

                  So it follows that this cannot make a difference to calling conventions.

                  Comment

                  • christian.bau

                    #10
                    Re: Register parameters

                    On Jul 17, 4:58 pm, Keith Thompson <ks...@mib.orgw rote:
                    A register-qualified object can't have its address taken.  This
                    eliminates the possibility of aliasing.
                    >
                    For example:
                    >
                        register int reg = 42;
                        int *ptr = <whatever>;
                        /* more code */
                        *ptr = 43;
                        /* still more code */
                    >
                    We know that the assignment to *ptr didn't affect the value of reg.
                    It would be much better to just check whether the address of an object
                    is taken or not. This will in practice cover many more cases.

                    Comment

                    • Keith Thompson

                      #11
                      Re: Register parameters

                      "christian. bau" <christian.bau@ cbau.wanadoo.co .ukwrites:
                      On Jul 17, 4:58 pm, Keith Thompson <ks...@mib.orgw rote:
                      >A register-qualified object can't have its address taken.  This
                      >eliminates the possibility of aliasing.
                      >>
                      >For example:
                      >>
                      >    register int reg = 42;
                      >    int *ptr = <whatever>;
                      >    /* more code */
                      >    *ptr = 43;
                      >    /* still more code */
                      >>
                      >We know that the assignment to *ptr didn't affect the value of reg.
                      >
                      It would be much better to just check whether the address of an object
                      is taken or not. This will in practice cover many more cases.
                      Sure. In other words, the compiler can usually figure out whatever
                      information the "register" keyword provides.

                      This wasn't the case when "register" was first introduced, and
                      compilers didn't have the time or space to do that kind of analysis.

                      --
                      Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
                      Nokia
                      "We must do something. This is something. Therefore, we must do this."
                      -- Antony Jay and Jonathan Lynn, "Yes Minister"

                      Comment

                      • Jack Klein

                        #12
                        Re: Register parameters

                        On Thu, 17 Jul 2008 09:01:05 +0200, Sensei <Sensei's e-mail is at
                        Mac-dot-comwrote in comp.lang.c:
                        Hi again!
                        >
                        I have recently encountered an old code in K&R style which declared
                        function parameters as register. That puzzles me, and I know this is
                        implementation defined, and depends on architecture, compiler and so
                        on. But my question is, how can be a parameter made "faster" (6.7.1,
                        with note 103).
                        >
                        On Intel and similar implementations all parameters are, as far as I
                        know, pushed into the stack. On PPC they can be passed through
                        registers.
                        >
                        Can someone clarify the use of this keyword and how it may work for
                        function parameters in a real-world example with "faster access" than a
                        normal parameter?
                        >
                        I know this is, again, implementation defined, but I am not interested
                        in a particular compiler or architecture, I am curious about *some*
                        examples of them, if any.
                        >
                        Thanks!
                        There are actually a few cases, beyond the realm of standard C, where
                        they are either absolutely necessary, or at least necessary as the
                        only possible alternative to writing a function in assembly language.

                        --
                        Jack Klein
                        Home: http://JK-Technology.Com
                        FAQs for
                        comp.lang.c http://c-faq.com/
                        comp.lang.c++ http://www.parashift.com/c++-faq-lite/
                        alt.comp.lang.l earn.c-c++

                        Comment

                        • Sensei

                          #13
                          Re: Register parameters

                          On 2008-07-17 09:01:05 +0200, Sensei <Sensei's e-mail is at Mac-dot-comsaid:
                          Hi again!
                          >
                          I have recently encountered an old code in K&R style which declared
                          function parameters as register. That puzzles me, and I know this is
                          implementation defined, and depends on architecture, compiler and so
                          on. But my question is, how can be a parameter made "faster" (6.7.1,
                          with note 103). [...]

                          I thank you all, you clarified a lot! (as you always do)

                          --

                          Sensei <Sensei's e-mail is at Mac-dot-com>

                          Error. Keyboard not attached. Press F1 to continue...
                          (Real BIOS Error)

                          Comment

                          Working...