[cfe-users] What option can make clang generate tail call IR?

Dmitri Gribenko gribozavr at gmail.com
Wed Jan 1 05:06:09 PST 2014


On Fri, Dec 20, 2013 at 8:04 AM, gamma_chen <gamma_chen at yahoo.com.tw> wrote:
> The following recursive example cannot generate "tail call". It generate
> "call" only. Anyone knows how to make "tail call"?
>
> // clang -c 1.c -emit-llvm -o 1.bc
> // llvm-dis 1.bc -o -
>
> // File 1.c
> int factorial(int x)
> {
>   x--;
>   if (x <= 0)
>     return 1;
>   return x*factorial(x-1);

This function is not tail-recursive.  A tail-recursive variant would
be something like:

int factorial(int x, int accum) {
  if (x == 1)
    return accum;
  else
    return factorial(x-1, accum*x);
}

Dmitri

-- 
main(i,j){for(i=2;;i++){for(j=2;j<i;j++){if(!(i%j)){j=0;break;}}if
(j){printf("%d\n",i);}}} /*Dmitri Gribenko <gribozavr at gmail.com>*/



More information about the cfe-users mailing list