This is the mail archive of the gcc-patches@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]
Other format: [Raw text]

Re: [tree-ssa] Tail recursion improvement


How does it handle the situation when n is initially less than zero?

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.


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