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

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

# Re: [tree-ssa] Tail recursion improvement

```On Tue, Dec 09, 2003 at 03:29:18PM -0600, Ken Wolcott wrote:
>
> How does it handle the situation when n is initially less than zero?

Try his transformed version below.  They have the same behavior.

>
> On Tuesday 09 December 2003 15:18, Paolo Carlini wrote:
> > Zdenek Dvorak wrote:
> > >Hello,
> > >
> > >this patch enables tail recursive calls to be optimized in more cases;
> > >for example it turns
> > >
> > >int sum (int n)
> > >{
> > >  if (n == 0)
> > >    return 0;
> > >
> > >  return n + sum (n - 1);
> > >}
> > >
> > >into
> > >
> > >int sum (int n)
> > >{
> > >  int acc = 0;
> > >
> > >  for (; n != 0; n--)
> > >    acc += n;
> > >
> > >  return acc;
> > >}
> >
> > Hey, fantastic Zdenek! I think that all around the world *thousands* of
> > students are told something like "an optimizing compiler is able to turn
> > the recursive, elegant, formulation of the factorial into the iterative,
> > efficient one". Now it's really true for GCC!
> >
> > Now I wonder: what about Fibonacci? Highly inefficient in the reursive
> > formulation, but basically just a sum: fib(n) = fib(n-1) + fib (n-2)??
> >
> > Paolo.
>
>

--
Daniel Jacobowitz
MontaVista Software                         Debian GNU/Linux Developer

```

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