[tree-ssa] Tail recursion improvement

Paolo Carlini pcarlini@unitus.it
Tue Dec 9 21:21:00 GMT 2003


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.



More information about the Gcc-patches mailing list