This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: Recursion Optimization


Andreas Schwab wrote:

> |> > |> TWO:
> |> > |>
> |> > |> In a function call like this:
> |> > |>
> |> > |> int sum(int i, int stop) {
> |> > |> }
> |>
> |> > |> Will gcc recognize that the value of the second parameter never changes
> |> > |> so that it can avoid allocating multiple copies of it on the stack.
> |> >
> |> > This cannot be avoided.
> ...
> |> Gcc can handel nested functions, so with a smart enough optimization
> |> technoligy it out to be able to rewrite it as
> |>
> |> int sum (int i, int stop) {
> |>   int sumh(int i) {
> |>     if (i == stop) return i;
> |>     else return sum(i+1, stop) + i;
> |>   }
> |>   return sumh(i);
> |> }
> 
> In which way is this different from the previous version?  You still have
> the same call to sum which requires a contiguous argument list on the stack.

Stop only gets allocated on the stack once as it is not a parameter of
sumh.  Each call to sumh will only allocated a new copy of i, not stop.

Now back to my orignal question:

Will gcc recognize that the value of the __second__ parameter (IE stop)
never change so that it can avoid allocating multiple copies of it on
the stack.



-- 
Kevin Atkinson
kevinatk@home.com
http://metalab.unc.edu/kevina/

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]